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

Event Listner

Guide ,
Jan 16, 2016 Jan 16, 2016

Hi,

My requirement is not to import .docx files, if one user import .docx then the script need to alert for confirmation to proceed further.

My idea is using event listener and place the script in startup scripts folder. Below is the tried code:

#targetengine "session" 

main(); 

function main(){ 

var myEventListener = app.addEventListener("beforePlace", myPlaceFile, false); 

function myPlaceFile(myEvent){ 

var myconfirm=confirm("You are placing docx file? Shall continue?") 

if(myconfirm) 

// here need to place the files 

else{ 

//need to exit 

 

 

Regards,

Karthi

TOPICS
Scripting
1.6K
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
Advisor ,
Jan 19, 2016 Jan 19, 2016

Is this what you are looking for?

InDesign ExtendScript API (8.0)

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 ,
Jan 19, 2016 Jan 19, 2016

When you place something you trigger four (relevant to your question) events: beforeImport, afterImport, beforePlace, afterPlace.

When you consider the Place Gun (i.e. the loaded cursor you get when you manually place a file) the meanings are these:

  • beforeImport: ID ist about to load the place gun
  • afterImport: ID has loaded the place gun
  • beforePlace: the user has clicked with a loaded place gun
  • afterPlace: ID has placed the file at the location of the click

Problem: Unless you create links for text files the beforeImport/afterImport event does not know what it is that its importing. (Someone please correct me if this is wrong).

The only solution I can currently think of would be to hook into the call of the menu item "Place" with MenuAction.afterInvoke and handle the file-selection etc yourself.

FWIW, here is a snippet that installs most of InDesign's event listeners to help you see, when what happens.

//EventListenersOn.jsx
//An InDesign CS5 JavaScript
//
//Installs event listeners for many supported events; displays a
//message when each event occurs.
#targetengine "session"
main();
function main(){
    mySetup();
    mySnippet();
    myTeardown();
}
function mySetup(){
}
function mySnippet(){
     var myEventNames = [
          "beforeQuit", "afterQuit",
          "beforeNew", "afterNew",
          "beforeOpen", "afterOpen",
          "beforeClose", "afterClose",
          "beforeSave", "afterSave",
          "beforeSaveAs", "afterSaveAs",
          "beforeSaveACopy", "afterSaveACopy",
          "beforeRevert", "afterRevert",
          "beforePrint", "afterPrint",
          "beforeExport", "afterExport",
          "beforeImport", "afterImport",
          
          "afterContextChanged",
          "afterDelete", "beforeDelete", 
          "beforeDisplay",
          "afterEmbed", "beforeEmbed",
          "afterInvoke", "beforeInvoke",
          "afterLinksChanged", 
          "afterMove", "beforeMove",
          "afterPlace", "beforePlace",
     /*     "afterSelectionAttributeChanged", 
          "afterSelectionChanged",     */
          "afterUnembed", "beforeUnembed",
          "afterUpdate", "beforeUpdate"
     ] ;
     for (var myCounter = 0; myCounter < myEventNames.length; myCounter ++){
          app.activeDocument.addEventListener(myEventNames[myCounter], myEventInfo);
     }
}
function myTeardown(){
}
function myEventInfo(myEvent){
     try{
          var myString = "Application Event: " +myEvent.eventType;
          myString += "\r\rTarget: " + myEvent.target.constructor.name + ", " +myEvent.target.name;
          myString += "\rCurrent: " +myEvent.currentTarget.constructor.name + ", " + myEvent.currentTarget.name;
          myString += "\r\rPhase: " + myGetPhaseName(myEvent.eventPhase );
          myString += "\rBubbles: " + myEvent.bubbles;
          myString += "\r\rCancelable: " +myEvent.cancelable;
          myString += "\rStopped: " +myEvent.propagationStopped;
          myString += "\rCanceled: " +myEvent.defaultPrevented;
          myString += "\r\rTime: " +myEvent.timeStamp;
          alert(myString);
          function myGetPhaseName(myPhase){
               switch(myPhase){
                    case EventPhases.atTarget:
                         myPhaseName = "At Target";
                         break;
                    case EventPhases.bubblingPhase:
                         myPhaseName = "Bubbling";
                         break;
                    case EventPhases.done:
                         myPhaseName = "Done";
                         break;
                    case EventPhases.notDispatching:
                         myPhaseName = "Not Dispatching";
                         break;
                    default:
                         myPhaseName = "no phase defined";
               }
               return myPhaseName;
          }
     }
     catch(myError){
          alert("Error getting event properties.");
     }
}
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
Community Expert ,
Jan 19, 2016 Jan 19, 2016

Hi Gerald,

beforeImport does indeed know about the file that will be placed.

The event when fired has the property fullName which is showing the file or it's full path plus name.

I tested this with InDesign CS6 v8.1.0 on Mac OSX.

See also my snippet where I am "exploiting" this:

Re: EventListner

The thread here is a double post of this other one (or vice versa)…

Uwe

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 ,
Jan 19, 2016 Jan 19, 2016
LATEST

Hi Uwe,

oh good. I didn't have time to look that far into it then.

Hopefully tpk1982 can make use of the information

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