Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Trigger script on the opening of new InDesign doc CS4 js

Participant ,
Feb 13, 2010 Feb 13, 2010

Hi,

Is it possible to trigger a java script on the double click of the InDesign doc in finder, so the script runs as soon as doc is open. How it can be done.

Thank you very much for your help.

Yulia

TOPICS
Scripting
2.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 13, 2010 Feb 13, 2010

Yulia,

yes this is possible with EventListener.

Please find attached file for example.

Cheers,

Shonky

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Feb 14, 2010 Feb 14, 2010

Hi there,

To me InDesign’s after/beforeOpen events are at the very least useless, since they are triggered before the document’s layout window is opened. This is why you can do nothing useful with the document.

The workaround is to use APID Toolkit’s docLoaded event instead, which works perfectly. If you are interested, here is an example of using it: check out source code for Check profile plug-in.

Kasyan

P.S. This was the case with CS3, may be with CS4 things took a turn for the better — I can't check it right now.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 14, 2010 Feb 14, 2010

Hi Kasyan,

You are absolutely right and your attached script is good example of docLoad event.

I read afterOpen event in Indesign Javascript Reference Guide but not used it. This event not work how they write in reference guide, after/before working same.

Thanks for your information.

Shonky

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 09, 2010 Apr 09, 2010

Hi, Kasyan.

I finally had a chance to look into your script and adjusted it to what I need, but I am not sure how to make it work. The script that I need to trigger works on it's own. Now I want it to be triggered as document gets open from Finder.  Here some of what I have:

if (theItem.eventCode == "docLoaded") {
     var myDoc = app.activeDocument;
    var myDocName = app.activeDocument.name;
    placeFilesOnDoubleClick();
}

function placeFilesOnDoubleClick(){

// places single page 'front_' and 'back _' files from the same as InDesign file folder and centres them

myDoc = app.activeDocument;
myDocPath = myDoc.filePath;
var myFolder = new Folder(myDocPath);
myGB_Y2 = myDoc.documentPreferences.pageHeight;
myGB_X2 = myDoc.documentPreferences.pageWidth;

...

}

Thank you very much for your help.

Yulia

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Apr 11, 2010 Apr 11, 2010
LATEST

Hi Yulia,

To use the script with docLoaded event you should compile a scripted plug-in using the APIDTemplate, place this plug-in to the folder of the same name and restart InDesign.

I asserted that before/afterOpen events are useless all the time here on the forum, but I was wrong. I talked to Peter Truskier on InDesign forum and he opened my eyes on this issue. The trick is to use app.documents[0] instead of app.activeDocument.

Below is a sample code that should work for you. Don't forget to use persistent engine, e.g. "session", place the script to "Startup Scripts" folder. I think It is good idea to define variables using var inside placeFilesOnDoubleClick function to make them local.

Regards,

Kasyan

#target indesign
#targetengine "session"

var myEventListener = app.eventListeners.add("afterOpen", placeFilesOnDoubleClick, false);

function placeFilesOnDoubleClick(){
     var myDoc = app.documents[0];
     var myDocPath = myDoc.filePath;
     var myFolder = new Folder(myDocPath);
     var myGB_Y2 = myDoc.documentPreferences.pageHeight;
     var myGB_X2 = myDoc.documentPreferences.pageWidth;
     ...
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines