10 Replies Latest reply on Sep 21, 2009 3:17 AM by Kasyan Servetsky

    Start script automatically after place Word text in indesign document

    Marcos Suárez Level 1

      I need start a script, automatically, after plece Word text in indesign document...

       

      It's possible ???

        • 1. Re: Start script automatically after place Word text in indesign document
          Bob Stucky Adobe Employee

          It is; however, you will need a plug in to do so.

           

          APID - www.rorohiko.com supports an event that can execute a script when something is placed.

           

          There might be others as well.

           

          Regards

           

          Bob

          • 2. Re: Start script automatically after place Word text in indesign document
            Kasyan Servetsky Level 5

            APID - www.rorohiko.com supports an event that can execute a script when something is placed.

                                               

            I am not sure if APID can do this — I rechecked its reference and couldn't find such an event. However, it supports many "text" events: when text is reflown, recomposed, overset, typed, etc.

             

            On the other hand, InDesign supports afterImport event that appears after a file is imported but

            before the file is placed on a page.

             

            Here is an example of using it:

             

            1. place the following script into Startup Scripts folder

             

            #targetengine "session"

            myAfterImportScriptPath = app.filePath + "/Scripts/Scripts Panel/AfterImportScript.jsx";

            var myAfterImportScript = new File (myAfterImportScriptPath);

            if (myAfterImportScript.exists) {

                  var myAfterImportEventListener = app.addEventListener( "afterImport", myAfterImportScript, false );

            }

             

             

            2. create AfterImportScript.jsx in Scripts Panel folder — it will be executed every time you place something.

            3. restart InDesign

             

             

            But there are two problems right from the start:

            • This event is triggered every time you place something into InDesign document, making no difference which kind of file you are placing: graphics, excel, incopy, word file.
            • It is triggered after you choose a file in the dialog box and before you place the loaded cursor.

             

            Kasyan

            1 person found this helpful
            • 3. Re: Start script automatically after place Word text in indesign document
              RorohikoKris-u5pUJw Level 2

              Hi Kasyan,

               

              APID can do it, but you need to use the 'modified' or 'subjectModified' event, plus probably the 'created' event. You also need some additional code to differentiate between 'normal' modifications and the placing of images.

               

              The way to do it is wait for the modified event, and then check whether the modification caused a link to appear. If not, do nothing. If it does, act on it.

               

              The trick is to store some data that encodes the presence or non-presence of a link in the dataStore of the page item on each modified or created event - so on each modified event you can compare the stored data with the actual data, and decide whether anything changed with regards to the linked file.

               

              Cheers,

               

              Kris

              • 4. Re: Start script automatically after place Word text in indesign document
                Kasyan Servetsky Level 5

                 

                 

                Hi Kris,


                Thank you for your advice.

                But this approach would work if you place only linked files. And what if you place a word or excel file? They are embedded (by default at least). How to find out that you placed a word file — not some other kind of file?


                Kasyan

                • 5. Re: Start script automatically after place Word text in indesign document
                  RorohikoKris-u5pUJw Level 2

                  This is how I'd approach it - it captures a 'place' or 'paste' of a blob of text. It is not perfect - it will also 'fire' if you simply copy some text from one frame in the document to an empty frame, but I don't think that would be an issue in most cases.

                   

                  Create a page item on the pasteboard; use the Active Page Item Developer palette to set the List of Subjects to * (just an asterisk), and the Event Filter to subjectModified* ('subjectModified' with an asterisk appended). Set the attached ExtendScript to the script shown below.

                   

                  I've not tested this heavily - it's only a proof of concept...

                   

                  (function(theItem)

                  {

                    const kKeyForSavedTextLength = "com.rorohiko.savedTextLength";

                   

                    do

                    {

                      var theFrame = theItem.eventSource;

                      if (! (theFrame instanceof TextFrame))

                      {

                        // Not a text frame - bail out

                        break;

                      }


                      var previousTextLength =

                        theFrame.getDataStore(

                          kKeyForSavedTextLength);

                         

                      if (previousTextLength == null)

                      {

                        previousTextLength = 0;

                      }

                     

                      if (previousTextLength > 0)

                      {

                        // Already has text in it - bail out

                        break;

                      }

                     

                      var curTextLength = theFrame.contents.length;

                     

                      theFrame.setDataStore(

                        kKeyForSavedTextLength,

                        curTextLength);

                       

                      if (curTextLength == 0)

                      {

                        // No text in it - bail out

                        break;

                      }

                     

                      alert("Pasted or placed a blob of text");

                       

                    }

                    while (false);

                   

                  }

                  (theItem));

                  1 person found this helpful
                  • 6. Re: Start script automatically after place Word text in indesign document
                    Kasyan Servetsky Level 5

                    Your concept works great, Kris. There is so much I can learn from you! One more illustration of new capabilities that APID provides.

                    I just want to put in my two cents' worth since the original poster asked about how to start a script automatically after placing a Word file.

                    To make it 'fire' only when a text file is placed we can check the last undo action:

                     

                    if (app.activeDocument.undoName == "Place")

                    {

                      alert("Placed a blob of text");

                    }

                    • 7. Re: Start script automatically after place Word text in indesign document
                      Harbs. Level 6

                      Kasyan,

                       

                      I was thinking along those lines as well. For that to work as a all-

                      purpose solution, you're going to have to test on every localized 

                      version of InDesign out there... But, it's probably enough for the 

                      OP's purposes.

                       

                      Harbs

                      • 8. Re: Start script automatically after place Word text in indesign document
                        RorohikoKris-u5pUJw Level 2

                        Well, the feeling is mutual! I hadn't thought of that - using the undo name, that's a cool trick. I'll put it in my personal 'bag of tricks'!

                        • 9. Re: Start script automatically after place Word text in indesign document
                          Harbs. Level 6

                          Another idea would be to write some data to a data store using an 

                          afterImport event (a time stamp?). Then in the APID event:

                           

                          var theData = theDataStore.getDataStore("importData",1);

                          if(!theData)

                           

                          // maybe check a time stamp for within the last half a minute or 

                          something as a safeguard...

                           

                          alert("we just imported something!");

                           

                          theDataStore.setDataStore("importData",undefined,1);

                           

                          Harbs

                          • 10. Re: Start script automatically after place Word text in indesign document
                            Kasyan Servetsky Level 5

                            Collective intelligence is a great thing!

                            I'd never hit upon these ideas alone.