Get Active Document's Index

Help with writing and running scripts

Get Active Document's Index

Postby Skwerm » Thu Apr 12, 2007 4:10 pm

How can I access the active document's index number? I can set the active document by using UltraEdit[x].setActive(); but how can I get the index of the currently active document?

Thanks.
User avatar
Skwerm
Basic User
Basic User
 
Posts: 14
Joined: Mon Aug 08, 2005 11:00 pm

Re: Get Active Document's Index

Postby jorrasdk » Fri Apr 13, 2007 7:30 am

Include this function that I have written. It should be called in the start of your script before any ...setActive is performed

Code: Select all
UltraEdit.activeDocument.write("Active document has index="+getActiveDocumentIndex());

/* Find the tab index of the active document */
function getActiveDocumentIndex() {
   var tabindex = -1; /* start value */

   for (var i = 0; i < UltraEdit.document.length; i++)
   {
      if (UltraEdit.activeDocument.path==UltraEdit.document[i].path) {
         tabindex = i;
         break;
      }
   }
   return tabindex;
}


Edit 2010-03-02: In UE version 16.00 a new property was added to the scripting environment: var docIx = UltraEdit.activeDocumentIdx; (See more UE 16.00 scripting enhancements...).
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: Get Active Document's Index

Postby Skwerm » Fri Apr 13, 2007 6:40 pm

I knew it was an array when I saw the example script using the .length property. I didn't remember seeing anything in the docs that actually said that .document is an array object, but I did go back and find that after reading your post.
User avatar
Skwerm
Basic User
Basic User
 
Posts: 14
Joined: Mon Aug 08, 2005 11:00 pm

Re: Get Active Document's Index

Postby jorrasdk » Sat Jun 16, 2007 1:38 pm

I just wanted you to know, that I have sent the feature request below to IDM Support. Cheers ! Jorrasdk

Jorrasdk wrote:Hi IDM Support

Feature request: UltraEdit.activeDocumentIdx
New scripting read only property which returns the tab index of the currently active file.
Should return -1 if there are no open files in the editor.


Up to now I have used a function shown below ( getActiveDocumentIndex() ) to find the index of the active document, but it is tedious to always have this function in my script (as we can't include other scripts yet ;-).

I need UltraEdit.activeDocumentIdx to know which document to return to if I have performed some newFile's or setActive's on other files. It is also nice to create aliases to simplify code like this:

Code: Select all
var mySourceDoc = UltraEdit.document[UltraEdit.activeDocumentIdx];
UltraEdit.newfile();
var myNewWorkDoc = UltraEdit.document[UltraEdit.activeDocumentIdx];
mySourceDoc.selectLine();
myNewWorkDoc.write(mySourceDoc.selection);

Regards
jorrasdk


Edit 2010-03-02: In UE version 16.00 a new property was added to the scripting environment: var docIx = UltraEdit.activeDocumentIdx; (See more UE 16.00 scripting enhancements...).
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: Get Active Document's Index

Postby jbeck » Tue Dec 02, 2008 10:53 am

Hello

Has anyone had any trouble with UltraEdit.document.length misreporting the number of documents open?

I am trying to use this function and have been getting:
An error occurred on line 81:
if (UltraEdit.activeDocument.path==UltraEdit.document[i].path) {
Script failed.

I have discovered that the reason for this is UltraEdit.document.length is sometimes returning a number one greater than the number of documents open, therefore UltraEdit.document[i].path is trying to reference a file that isn't there. In my script I am opening and closing several text files - maybe UltraEdit.document.length is getting confused?
User avatar
jbeck
Newbie
 
Posts: 6
Joined: Wed Apr 11, 2007 11:00 pm

Re: Get Active Document's Index

Postby danne » Thu Mar 12, 2009 7:44 am

I'm using UltraEdit v14.20.1.1008. UltraEdit.document.length seems to only hold the number of documents if you use open file tabs, which I don't (I use the file tree view instead). It returns 0, even though I have several documents open. Any other ideas about getting the index of the current document in this case?

Is this something that will be remedied in the next version? Ie. getting an UltraEdit.activeDocumentIndex.

Edit:
After a closer look this must be a bug. UltraEdit.document[index] doesn't seem to work at all without having open file tabs, any reference to it results in a script fail. I doubt this is the intended behavior. I'm going to file a bug report on it, or at least ask about the behavior.

Edit2: The reply from IDM suggests that this is indeed a bug. Hopefully a fix will be out soonish =)

Edit3: This bug isn't fixed in UE15, I have however made the Open Files a Tab together with File View so it doesn't take any space at least.
User avatar
danne
Basic User
Basic User
 
Posts: 36
Joined: Mon Feb 07, 2005 12:00 am

Re: Get Active Document's Index

Postby Mofi » Sun Nov 01, 2009 12:11 pm

I have an update on the issue with document array management not working correct when the open file tabs bar is not enabled. It looks like the problem is fixed with UE v15.20.0.1017. I have not fully tested it, but one of my scripts written for IDM to demonstrate the wrong behavior of document array management when the open file tabs bar is not enabled works now correct and produces the same result as when executing it while the open file tabs bar is visible.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4066
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Get Active Document's Index

Postby danne » Thu Nov 05, 2009 7:05 am

Yes, it was fixed. I forgot I posted about it, so sorry for not updating this thread. IDM sent me a release to test a few weeks ago, and it was fixed in that one (think it was 1017 as you said, currently on 1021 testing another issue).
User avatar
danne
Basic User
Basic User
 
Posts: 36
Joined: Mon Feb 07, 2005 12:00 am

Re: Get Active Document's Index

Postby jorrasdk » Tue Mar 02, 2010 2:55 am

In UE version 16.00 a new property was added to the scripting environment: var docIx = UltraEdit.activeDocumentIdx; (See more UE 16.00 scripting enhancements...).
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark


Return to Scripts