Message Board for Rorohiko Workflow Resources
https://www.rorohiko.com/cgi-bin/yabb2/YaBB.pl
General Category >> General Board >> docClose event is not working for me
https://www.rorohiko.com/cgi-bin/yabb2/YaBB.pl?num=1243164978

Message started by Kasyan on 05/24/09 at 23:36:18

Title: docClose event is not working for me
Post by Kasyan on 05/24/09 at 23:36:18
Hi there,

I am trying to make a simple scripted plug-in which saves/restores the current document's layout window position when it is closed/opened.
Event Filter: docClose, docLoaded

Code (]if (theItem.eventCode == "docLoaded") {
     var gKasMyDoc = app.activeDocument
     var gKasDocumentName = gKasMyDoc.name;
     KasRestoreDocumentsPosition();
}
else if (theItem.eventCode == "docClose") {
     var gKasMyDoc = app.activeDocument
     var gKasDocumentName = gKasMyDoc.name;
     KasSaveDocumentsPosition();
}

function KasSaveDocumentsPosition() {
     alert("about to close");
     var myCurrentBounds = app.layoutWindows[0):

.bounds;
     app.setDataStore(gKasDocumentName, myCurrentBounds);
     alert(app.getDataStore(gKasDocumentName));
}

function KasRestoreDocumentsPosition() {
     var myRestoredBounds = app.getDataStore(gKasDocumentName);
     if (myRestoredBounds != undefined) {
           app.layoutWindows[0].bounds = myRestoredBounds;
           alert(app.getDataStore(gKasDocumentName));
     }
}

docLoaded works as expected, but docClose doesn't.

I tried to make it simpler:
[code]if (theItem.eventCode == "docClose") {
           alert(theItem.eventCode);
}
else if (theItem.eventCode == "docLoaded") {
           alert(theItem.eventCode);
}[/code]

When a document is open an alert appears, but when closed not. What am I missing?
Thanks in advance.

Kasyan

Title: Re: docClose event is not working for me
Post by Kris Coppieters on 05/25/09 at 07:05:01
Hi Kasyan,

I'll check it out - it might be a bug; it's been a while I revisited that code and maybe APID simply does not work properly. I know docClose has been a hard one to get it working right - I don't remember, need to check my documentation, but maybe I have disabled it on purpose.

One reason for that is that docClose is not really a good event, and I don't personally use it - in many cases, I found it's better to latch on to docSave instead (and not docSaved nor docClose).

docClose happens very late in the game, the document is in the process of closing, and there's not much you can do - any processing you do can only sensibly affect something outside of the document.

Title: Re: docClose event is not working for me
Post by Harbs on 05/25/09 at 07:13:53
It does look like docClose is not working.

You can use docDeselected instead...

Title: Re: docClose event is not working for me
Post by Kasyan on 05/25/09 at 19:53:43
Thank you both for the quick response.

I remember that I tried to use docClose event when I just started using APID. I just installed an older version -- 1.0.45 -- and it works. However, it is useless in my case: the event triggers the script AFTER the layout window is closed, so attempt to get its bound generates an error.

The docDeselected works when more then one document is opened, but while the last document is being closed, no event appears.

Kasyan

Title: Re: docClose event is not working for me
Post by Kris Coppieters on 05/25/09 at 20:53:38
P.S. APID 1.0.47 should have this fixed - i.e. for what it's worth, docClose works again in the current 'work-in-progress'. I'll check on docDeselected to make sure it also works when the document is the sole document.

Have you tried using docSave instead?

Cheers,

Kris

Title: Re: docClose event is not working for me
Post by Kasyan on 05/26/09 at 20:54:23
Replacing docClose with docSave did the trick -- now the script does what I want.
But I found a weird phenomenon: I added the following line at the start of the script to check events:
[code]alert("Event Code is " + theItem.eventCode);[/code]
and found out that when I close a document docSave event appears (my current version is 1.0.47-pre3.0.rel)

I have another question: my script saves position of the first layout window for each document saved by a user using setDataStore(), and all this information is stored somewhere. With time, the information about billions of files can accumulate. Can this degrade the performance?

Title: Re: docClose event is not working for me
Post by Kris Coppieters on 05/27/09 at 14:56:16
Hi Kasyan,

The docSave might be normal - if a document is modified, closing a document automatically makes InDesign attempt to save it before closing.

Yes, if you keep track of all that stuff in the app dataStore, it will accumulate - it actually resides inside your InDesign preferences folder. I'd look into a scheme where I would maybe try to save that data into the document's data store instead - but there's more to it than at first is apparent.

For example, you don't want one user's window position to be inherited by another when the document is opened on another workstation. Also, what happens if the screen resolution is changed since last time you've opened the document? You might move it 'out of reach' of the user.

Maybe you can use a slightly more convoluted setup, where you remove older window positions from the app datastore - e.g. anything more than a week old, or only keep the last 100 window positions - as new entries are made, older entries are deleted.

Or alternatively, you could write it to a file instead of storing it in the app's datastore - making it easier to erase the file at regular times.


Title: Re: docClose event is not working for me
Post by Kasyan on 05/28/09 at 03:43:36
Thank you, Kris. I will follow your advice without fail.

Title: Re: docClose event is not working for me
Post by Harbs on 05/28/09 at 04:27:51
You can also save the data in a document dataStore. You can pair the data with the user name to keep the window position for each user unique.

There will be a limited number of users per document...  :)

Title: Re: docClose event is not working for me
Post by Kasyan on 05/31/09 at 02:33:31
Since I couldn’t find dataStore property of document, I used insert/extractLabel instead. Here is the current version of the script. It stores data with a document and for each user, as you advised me.


Code (]try {
     var gKasMyDoc = app.activeDocument;
     var gKasDocumentName = gKasMyDoc.name;
     if (File.fs == "Windows") {
           var gKasUserName = $.getenv("USERNAME");
     }
     else if (File.fs == "Macintosh") {      
           var gKasUserName = $.getenv("USER");
     }
     var myLabel = "KasSavedBounds_" + gKasUserName;
} catch(err) {}
     
if (theItem.eventCode == "docLoaded") {
     KasRestoreDocumentsPosition();
}
else if (theItem.eventCode == "docSave") {
     KasSaveDocumentsPosition();
}

function KasSaveDocumentsPosition() {
     try {
           var myCurrentBounds = app.layoutWindows[0):

.bounds.join(",");
           gKasMyDoc.insertLabel(myLabel, myCurrentBounds);
     } catch(err) {}
}

function KasRestoreDocumentsPosition() {
     var myRestoredBounds = gKasMyDoc.extractLabel(myLabel).split(",");
     if (myRestoredBounds != "") {
           for (i = 0; i < myRestoredBounds.length; i++) {
                 myRestoredBounds[i] = parseInt(myRestoredBounds[i]);
           }
           app.layoutWindows[0].bounds = myRestoredBounds;
     }
}


The idea to make such a plug-in came to me when I read, in the Adobe forum,  about a problem that appeared in InDesign CS4: unlike CS3, it doesn’t save a document’s position, which irritates some users – I assume there are quite a number of them. I thought it was quite easy to solve this with APID.

I have yet another question: If I want to generate a plug-in for all possible versions — CS, CS2, CS3, CS4, both for Mac and PC, should I have all 8 copies of InDesign installed?

Title: Re: docClose event is not working for me
Post by Harbs on 05/31/09 at 08:36:24
Hi Kas,

Cool idea.

There is no document dataStore property. There is a dataStore property for scripted plugins though.

For your purposes a script label should work fine, though.

A compile from any version of InDesign works. That's the great thing about APID! :)

Message Board for Rorohiko Workflow Resources » Powered by YaBB 2.5.2!
YaBB Forum Software © 2000-2024. All Rights Reserved.