27 Replies Latest reply on May 31, 2016 6:11 PM by Johnwhite

    Why will this not work on startup?

    cbishop01 Level 2
      #targetengine "session"   
          var myDocument = app.documents.item(0);
          var myStory = myDocument.stories.item(0);
              myStory.trackChanges = true;  
      

      I placed this script in the Startup Script folder but it gives me an error on line 3.  When i have the script in the Normal Script folder and Click on it it works.

        • 1. Re: Why will this not work on startup?
          cbishop01 Level 2

          I was hoping to get this to Turn on Track Changes On every document i open.  I got the error to quit but it still does not turn on automatically. Here's what i did.

           

          #targetengine "session"   
          
            function trackChanges()
           {
             var myDocument = app.documents.item(0);
             var myStory = myDocument.stories.item(0);
                 myStory.trackChanges = true;  
           }
          
          • 2. Re: Why will this not work on startup?
            Laubender Adobe Community Professional & MVP

            You'll need an event listening mechanism to check, if a document is opened and react on that.

            You have to realise, that a script in the startup scripts folder is executed immediately when starting InDesign.


            At exactly this point in time no document is open. So your code will return an error.

            Even if you launch InDesign by double-clicking a document.

             

            Btw. You are asking this in the wrong forum, I think.

            A moderator should move this question over to InDesign Scripting

             

            Regards,
            Uwe

            1 person found this helpful
            • 3. Re: Why will this not work on startup?
              tpk1982 Level 4

              try this,

               

              save this code in startup folder

               

              var
                myEventListener = app.eventListeners.item("xx"); //xx is script name whatever you give
              
                if (!myEventListener.isValid) {
                myEventListener = app.addEventListener( "afterOpen", doJob );
                myEventListener.name = "xx"; //replace xx with script name
                }
              
              function doJob() {
                var myDocument = app.documents.item(0); 
                var myStory = myDocument.stories.item(0); 
                    myStory.trackChanges = true; 
              }
              
              1 person found this helpful
              • 4. Re: Why will this not work on startup?
                vinothr Level 3

                You can put the below in startup script:

                 

                app.addEventListener(Document.AFTER_OPEN, afterDocOpen);

                 

                function afterDocOpen(event) {

                    if(event.parent.constructor.name == "Document") {

                        var myDocument = event.parent;

                         var myStory = myDocument.stories.item(0); 

                         myStory.trackChanges = true;   

                    }

                }

                 

                Above solution by tpk1982 will always pick the document at index 0. It might not work when you open multiple documents at same time.

                1 person found this helpful
                • 5. Re: Why will this not work on startup?
                  cbishop01 Level 2

                  Just a quick question so when i make a stratup script will i always need a EventListener?  or is it on a per script basis.  Meaning depends on the script.  

                  • 6. Re: Why will this not work on startup?
                    vinothr Level 3

                    It is not mandatory to have eventlistener in your startup script. It depends on what you what to implement.

                    In your case, you always want to run your script on document-open.

                    So, we need to listen to afterOpen event.

                    • 7. Re: Why will this not work on startup?
                      cbishop01 Level 2

                      Sorry i thought i was in the scripting Forum

                      • 8. Re: Why will this not work on startup?
                        cbishop01 Level 2

                        So far neither one works I still Have to turn on the Track CHanges manually.  By clicking the other script i use that i want to auto start.Where am i going wrong?  I Tried first to type in the codes you guys gave above so that way I get used to it.  But i figrued i typed something wrong.  Then i Tried to copy and paste.  ANd nither turn on Track Changes When a document is Opened or Created. 

                        1. var myDocument = app.documents.item(0); 
                        2.    var myStory = myDocument.stories.item(0); 
                        3.        myStory.trackChanges = true;  
                        • 9. Re: Why will this not work on startup?
                          cbishop01 Level 2

                          It wont turn on when the document is created or opened.  I tried my script name with and with out the file extension at the end. Here's what i added..  The TurnTCon is the name of the script that you see below.  I also tried my TCon in the main scripts folder that works but it will not run still.  Does the script need to have a delay on it so it knows when the document is opened?

                          var  
                            myEventListener = app.eventListeners.item("TurnTCon"); //This is the name of the script.  
                            
                            if (!myEventListener.isValid) {  
                            myEventListener = app.addEventListener( "afterOpen", doJob );  
                            myEventListener.name = "TurnTCon"; //This is the name of the Script  
                            }  
                            
                          function doJob() {  
                            var myDocument = app.documents.item(0);   
                            var myStory = myDocument.stories.item(0);   
                                myStory.trackChanges = true;   
                          } 
                          
                          • 10. Re: Why will this not work on startup?
                            cbishop01 Level 2

                            I think i may see the problem.  Each file has multiple Text boxes.  Even the script i use only allows me to turn the Track Chages on (per text box that my cursor is in.) could this be the problem?

                            • 11. Re: Why will this not work on startup?
                              vinothr Level 3

                              I only tried to troubleshoot the issue you were facing with the script.

                              With the code you have written recently, it will always turn on track changes for the FIRST story of the FIRST document in the InDesign session. It seems you want for all stories of any document.

                               

                              Let me know the exact requirement. Hope you understood the issue now.

                              • 12. Re: Why will this not work on startup?
                                tpk1982 Level 4

                                change the line 11 to

                                 

                                var myStory=myDocument.stories.everyItem();

                                 

                                so it will take all stories in the active document

                                1 person found this helpful
                                • 13. Re: Why will this not work on startup?
                                  cbishop01 Level 2

                                  Ok this is working.  for the most part.  Just one last thing on this one. When i create a new document it works just fine.  But when i open an existing document it does not work.  Is there an extra line i need to add to get this to work?  Or since the other documents are already created is this just not possible?

                                  • 14. Re: Why will this not work on startup?
                                    cbishop01 Level 2

                                    Ah i was trying var myStory=myDocument.stories.Item();  thats why it would not work when i was doing it.  Thank you so much.

                                    • 15. Re: Why will this not work on startup?
                                      cbishop01 Level 2

                                      Its mostly working as stated above.  I'm wanting this to script to turn Track Changes on for every Document. The documents i already have created as well as the ones I Create from scratch.  So far its working for the Creation of a file.  But as for The documents that i already have built (Which is ALOT) it will not turn it on. 

                                      • 16. Re: Why will this not work on startup?
                                        vinothr Level 3

                                        Please share again the script you have in place. So, I can see what is wrong with it.

                                        It seems you are having: var myDocument = app.documents.item(0);

                                         

                                        1 person found this helpful
                                        • 17. Re: Why will this not work on startup?
                                          tpk1982 Level 4

                                          try this

                                           

                                          var myDocument = app.activeDocument;    

                                          var myStory=myDocument.stories.everyItem();

                                          myStory.trackChanges = true; 

                                          1 person found this helpful
                                          • 18. Re: Why will this not work on startup?
                                            vinothr Level 3

                                            Yes, that is true.

                                            If you have app.documents.item(0) then replace it with app.activeDocument.

                                            This should work. If not, kindly share the script you have in place.

                                            • 19. Re: Why will this not work on startup?
                                              cbishop01 Level 2
                                              var  
                                                      myEventListener = app.eventListeners.item("TurnTCon");   
                                                
                                                  if (!myEventListener.isValid) {  
                                                      myEventListener = app.addEventListener( "afterOpen", doJob );  
                                                      myEventListener.name = "TurnTCon";   
                                                }  
                                                
                                                  function doJob() {  
                                                  var myDocument = app.activeDocument();    
                                                  var myStory=myDocument.stories.everyItem();
                                                      myStory.trackChanges = true;    
                                              }
                                              

                                              This is the Full Script.  Its still doing the same thing.  If i create a document from scratch Every story will have Track Changes enabled.  But again if it is a file i created say 1 year ago and Track changes was never turned on for it the script does not turn it on. 

                                              • 20. Re: Why will this not work on startup?
                                                vinothr Level 3

                                                I think your script is fine. I am wondering if any modal dialog gets opened when you open an existing document.

                                                This script may fail if any modal dialog opens up when opening your old docs.

                                                Do see any such modal dialog opening up?

                                                • 21. Re: Why will this not work on startup?
                                                  cbishop01 Level 2

                                                  They Some of the files I'm working with when i open them Have links that need updated.  Could this cause the problem?  If so can i add a delay to this code to wait till all documents are updated and fully loaded?

                                                  • 22. Re: Why will this not work on startup?
                                                    vinothr Level 3

                                                    Possibly yes. Adding delay would not be the right solution.

                                                    Missing links is just one of such dialog. There could many be others like missing fonts, etc.

                                                    A more prudent approach is required. Check if there is a way to get into Preflight listeners after doc open.

                                                     

                                                    Missing fonts, missing links, all would lead to preflight errors...

                                                    Will let you know if I can think of any other robust solution.

                                                    • 23. Re: Why will this not work on startup?
                                                      vinothr Level 3

                                                      Please use the following code and let me know if all is well. I have made some modifications and explained below:

                                                       

                                                      #targetengine session
                                                      
                                                      var myEventListener = app.eventListeners.item("TurnTCon");
                                                         
                                                      if (!myEventListener.isValid) {   
                                                          myEventListener = app.addEventListener("afterOpen", doJob1 );   
                                                          myEventListener.name = "TurnTCon";   
                                                      }   
                                                         
                                                      function doJob(event) {
                                                          try{
                                                              if(event.parent.constructor.name == "Document") {
                                                                  var myDocument = event.parent;
                                                                  if(myDocument.stories.length > 0) {
                                                                      var myStory = myDocument.stories.everyItem().trackChanges = true; 
                                                                  }
                                                              }
                                                          }catch(e) {  }
                                                      } 
                                                      

                                                       

                                                      Comments:

                                                      1. Better to include #targetengine session in first line of your script

                                                       

                                                      2. The event "afterOpen" is triggered not only for documents but also for windows (layoutwindow, storywindow, etc.).

                                                      So, you have to filter out DocumentEvent only and proper error handling is required as I have done above.

                                                      Your previous code could have probably misinterpreted the activeDocument when responding to a Window's afterOpen event.

                                                      So, activeDocument does not suit in all places.

                                                       

                                                      3. One of your line says: var myDocument = app.activeDocument(); A possible runtime error in your code.

                                                      It should be app.activeDocument as it is NOT a function. By the way, do not use activeDocument.

                                                      Consider the above code that gets rid of it.

                                                       

                                                      4. The missing links dialog should not be an issue. I have checked it.

                                                       

                                                      5. NOTE: Your document will always open as MODIFIED as you are making modification during opening of document (by turning on track changes). Hope you are getting my point here.

                                                      2 people found this helpful
                                                      • 24. Re: Why will this not work on startup?
                                                        cbishop01 Level 2

                                                        This works perfect.  I had to change one thing.  let me know if this is going to mess up the Script.  I changed doJob1 to doJob because when i opened indesign without a file opened it gave me an error on line 6.  When i deleted the 1 it fixed the error.  I only got the error when i open indesign without a file opening right away.  And thank you so much for your explanations.  Yeah i know it will always open as modified.  I'm more worried about seeing the changes that were made.   And Seeing who saved it when it was last saved and Output( those are on a different script.

                                                        1 person found this helpful
                                                        • 25. Re: Why will this not work on startup?
                                                          vinothr Level 3

                                                          Oh, That was a typing mistake...it should be doJob and not doJob1 (line 6). There is no problem.

                                                          • 26. Re: Why will this not work on startup?
                                                            cbishop01 Level 2

                                                            Ok thank you again for you work on this. 

                                                            • 27. Re: Why will this not work on startup?
                                                              Johnwhite Level 1

                                                              yes, need to changed doJob1 to doJob.