22 Replies Latest reply on Oct 13, 2016 10:55 AM by cbishop01

    Save Code?

    cbishop01 Level 2

      is there an easy save active document code? 

        • 1. Re: Save Code?
          Loic.Aigon Adobe Community Professional

          Every day better and better.

           

          You may be interested in idml if my black voodoo power isn't gone.

          • 2. Re: Save Code?
            cbishop01 Level 2

            I like the one you made for me the other day.  I'm just having issues getting it to only activate for the document i'm working on and then restarting the count if i switch documents.  And it saves until i close indesing(I'm trying to figure how to only have this script active until the document/s are closed.).  This is great the only down side is we sometimes open a document to use as a template for our new documents and the save script saves over that one too.  We've had no problems yet however I have another script running that tracks the last person to modify and save a file.  And with it saving every time i open the files up its messing with that one. While i was playing around with the last save Script you made i just happened to see the event listener "afterSelectionChanged" that activates the Script you made before only when changes are made.  And my thinking is if there was just a script that Only saves.  If i used that event listener it would be the save as auto save every X seconds.

            • 3. Re: Save Code?
              cbishop01 Level 2

              and i'm way better at scripting than when i started.  I no where near as good as you guys

              • 4. Re: Save Code?
                Trevorׅ Adobe Community Professional

                Well, I can't be bothered to search for for the code that Loic wrote you the other day so it's very hard to know what you are after.

                If you want the script to run only on the document that it was originally executed on, i.e. the one that was the activeDocument at the time of execution, you can try something like:

                 

                On run

                `doc = app.activeDocument;`

                 

                In the eventListner stage

                if (myEvent.target === doc) do something else don't do something.

                 

                HTH

                 

                Trevor

                 

                P.s. don't just copy and paste the provided 'code'

                1 person found this helpful
                • 5. Re: Save Code?
                  cbishop01 Level 2

                  Hi Trevor, Doesnt the var doc = app.properties.activeDocument specify only the active document?  Below's the code he made.  I did change the sleep to 60000 / around 1 min 15 sec's give or take.

                   

                  main();
                      var filter = function() {  
                        var w = "InDesign files:*.indd;",  
                        m = function(f) {return /\.indd$/.test(f.name); };  
                         
                        return $.os[0]=="W"?w:m;  
                      }  
                        function main() {  
                         
                          var t = (new Date()).getTime();  
                          var onIdleEventHandler = function(idleEvent) {  
                          var doc = app.properties.activeDocument;  
                            if ( !doc ) return;  
                            if ( !doc.properties.fullName ) {  
                          var f = File.saveDialog("You need to save your document now !", filter);  
                            if ( f ) {  
                         doc.save ( f );  
                        }  
                        return;  
                        }  
                         var t2 = (new Date()).getTime();  
                          if ( t2-t > 1000)  {  // was 1000
                        t = t2;  
                        doc.save();  
                        }  
                        }  
                         var myIdleTask = app.idleTasks.item("autoSave");  
                          if ( !myIdleTask.isValid ) {  
                        myIdleTask = app.idleTasks.add({name:"autoSave", sleep:60000});  // was 100
                        myIdleTask.addEventListener(IdleEvent.ON_IDLE, onIdleEventHandler, false);  
                        }  
                      }
                  
                  • 6. Re: Save Code?
                    Trevorׅ Adobe Community Professional

                    Hi Chris,

                     

                    I made a few changes that might help you out.

                    No the script will only work on the document you run it on and not new or non-active ones.

                    I'm not sure about the 60000 sleep setting  but from what I understand you want that connected to the t and t2 values and not the sleep.

                    Note the changes I made regarding the activeDocument and try understand them.

                    Sorry I can't explain properly, I'm very busy now.

                     

                    Didn't run the below even once so don't be surprised if .....

                     

                    HTH

                     

                    Trevor

                     

                    main(app.properties.activeDocument); // CHANGED
                    var filter = function() {
                        var w = "InDesign files:*.indd;",
                            m = function(f) {
                                return /\.indd$/.test(f.name);
                            };
                    
                        return $.os[0] == "W" ? w : m;
                    };
                    
                    function main(doc) {
                    
                        var t = (new Date()).getTime();
                        var onIdleEventHandler = function(idleEvent) {
                            if (!doc) return;
                            if (doc !== app.properties.activeDocument) return; // ADDED
                            if (!doc.properties.fullName) {
                                var f = File.saveDialog("You need to save your document now !", filter);
                                if (f) {
                                    doc.save(f);
                                }
                                return;
                            }
                            var t2 = (new Date()).getTime();
                            if (t2 - t > 60000) { // CHANGED
                                t = t2;
                                doc.save();
                            }
                        };
                        var myIdleTask = app.idleTasks.item("autoSave");
                        if (!myIdleTask.isValid) {
                            myIdleTask = app.idleTasks.add({
                                name: "autoSave",
                                sleep: 1000 // CHANGED
                            });
                            myIdleTask.addEventListener(IdleEvent.ON_IDLE, onIdleEventHandler, false);
                        }
                    }
                    
                    1 person found this helpful
                    • 7. Re: Save Code?
                      cbishop01 Level 2

                      The 60000 aka: (1min 15sec) timer was just to give us a delay so if we opened a document on accident it wouldn't just Save.  It gave us a little bit of a buffer.. 

                      • 8. Re: Save Code?
                        cbishop01 Level 2

                        Never mind.  After i restarted Indesign. It started working.  I'll Test it more monday.  IF all works like it did for the past 5 min's i'll mark it correct. 

                        • 9. Re: Save Code?
                          cbishop01 Level 2

                          Ok it is not working.  I've tried several different settings and nothing.  I found the reason it was working Friday. I forgot to delete the old version of this Save script off my autostart Script.  NOw that its gone this one does not save.

                          • 10. Re: Save Code?
                            Trevorׅ Adobe Community Professional

                            Was that with the sleep set to 60000 or 1000?

                            • 11. Re: Save Code?
                              cbishop01 Level 2

                              the only reason that the 60000 sleep was so the save feature would give me 1min 15 seconds between saves.  I had to do this so when i added it to my script it delayed from running for that amount of time.  That way if we open up just any old file on accident it didn't save over it(That would interfere with my Save changes Script).  as for that 1000 i'm not real sure what that did.  I just left that one alone. 

                              • 12. Re: Save Code?
                                cbishop01 Level 2

                                Sorry i mis read that.  Yes it was with the 60000 and 1000 I've also tried smaller numbers to see if the code was working and just taking along time.  Cant get the code to autosave. for some reason. 

                                • 13. Re: Save Code?
                                  cbishop01 Level 2

                                  This is working! I think something else is going on with my Computer.  It started working again. 

                                  • 14. Re: Save Code?
                                    cbishop01 Level 2

                                    Thank you again. 

                                    • 15. Re: Save Code?
                                      cbishop01 Level 2

                                      Ok i found out whats happening.  I can only run the Script 1 time until i close Indesign.  So if i have 5 documents open and i want to auto save 3 of them it will not work.  I would have to Run autosave on one close indesign Startup another DOcument and so on.  Is there a way around that?

                                      • 16. Re: Save Code?
                                        Skemicle Level 3

                                        That's what Trevor meant when he said:

                                        Trevor wrote:

                                         

                                        No the script will only work on the document you run it on and not new or non-active ones.

                                        If you want it to use the active document no matter what document was active at the time do not set it to a variable. I would assume changing line 27 in script to "app.activeDocument.save();" would work.

                                         

                                        Theory not tested. Please, correct me if I'm wrong.

                                         

                                         

                                        Edit: If you are 15 seconds until the next save and accidentally open a document it will still save within the 15 seconds. adding an event listener to tell when the activeDocument changes might be a better fix but I'm not sure on that.

                                        • 17. Re: Save Code?
                                          Trevorׅ Adobe Community Professional

                                          Which 3 of the 5 would you want to change?

                                          • 18. Re: Save Code?
                                            cbishop01 Level 2

                                            THanks for your reply guys.  I know he said it will only work on one document.  And thats fine as long as i can activate the script on another document without closing the Program down.  I have this script inserted into a checkbox (in another script) to activate if we want it and not to activate if the Checkbox is unchecked.  As of right now this is heading along the lines i'm needing.  IT only activates for the document i run the script on.  I just need it to allow me to activate the script for more than one document if needed.

                                            • 19. Re: Save Code?
                                              cbishop01 Level 2

                                              I Have this in another script where if i select the check box its in it will Run THis script on the selected files i want to auto save.  That way if we have a document that is all the way finished that we are using for reference it does not auto save over that one unless i select to run this script.  In a nut shell i need this to work on any document that i choose (This is where my check box comes in.) without restarting the program. 

                                              • 20. Re: Save Code?
                                                Trevorׅ Adobe Community Professional

                                                Sorry, I'm going to be unavailable for the next couple of weeks, hopefully someone else can help you in the meantime.

                                                You basically have to monitor the open documents and feed that to your palette. Make a list of the IDs of the documents you want to have the scripts function on check that the current activeDocument is one of those IDs

                                                So instead of using doc use some object like myDocs on checkbox check fill the object

                                                myDocs = {_34: true, _37:true};

                                                where 34 and 37 are the document IDs

                                                on uncheck use delete to remove the entry

                                                docIdToDelete = '_34'

                                                delete myDoc[docIdToDelete];

                                                 

                                                instead of

                                                if (doc !== app.properties.activeDocument) return

                                                 

                                                do something like

                                                 

                                                id = '_' + app.activeDocument.id;

                                                if (!id in myDocs) return;

                                                 

                                                make the other changes as needed

                                                 

                                                Best of luck, I'll see where your holding in a couple of weeks,

                                                 

                                                Trevor

                                                1 person found this helpful
                                                • 21. Re: Save Code?
                                                  cbishop01 Level 2

                                                  @ Trevor

                                                  Thanks for the replay.  And good luck with whatever you have going on for the next few weeks.  I don't know that i'll be able to do that because the Documents we use for templates are random depending on the Type we are creating. I'll test the above to see if it will be more of a hassle than its worth.  Thanks again..

                                                  @Anyone.

                                                  Can someone tell me why this script requires me to Close the Program rather to run it on a different Document?  Why will it run for one document THen when i close that document and Open a different Document WHy can i not run this script again?

                                                  • 22. Re: Save Code?
                                                    cbishop01 Level 2

                                                    Ok i Now have it WHere i can Auto Save 1 file at a time.  without Closing Indesign.  And i can have as many  Documents open that i want.  Now i just need it to allow me to Run this script For any Document That i choose (  ).  The 45000 is just a delay for testing purposes.  Below is the working With test sleep time (Note: to anyone who uses this code. if Sleep is too low it Crashes indesing).  is there a way to maybe make a max number of open documents able to be saved at the same time?  We will not be editing more than 5 at a time. SO if i choose one it will work like now but if i want use the script on 4 more documents it will work for all of them. 

                                                    function cb2CallBack() {
                                                        main(app.properties.activeDocument);  
                                                            var filter = function() {    
                                                              var w = "InDesign files:*.indd;",    
                                                              m = function(f) {return /\.indd$/.test(f.name); };    
                                                                 
                                                              return $.os[0]=="W"?w:m;    
                                                            }    
                                                              function main() {    
                                                                 
                                                                var t = (new Date()).getTime();    
                                                                var onIdleEventHandler = function(idleEvent) {    
                                                                var doc = app.properties.activeDocument;    
                                                                  if ( !doc ) return;    
                                                                  if ( !doc.properties.fullName ) {    
                                                                var f = File.saveDialog("You need to save your document now !", filter);    
                                                                  if ( f ) {    
                                                               doc.save ( f );    
                                                              }    
                                                              return;    
                                                              }    
                                                               var t2 = (new Date()).getTime();    
                                                                if ( t2-t > 1000)  {  // was 1000  
                                                              t = t2;    
                                                              doc.save();    
                                                              }    
                                                              }    
                                                              var myIdleTask = app.idleTasks.item("autoSave");  
                                                            if (!myIdleTask.isValid) {  
                                                                myIdleTask = app.idleTasks.add({  
                                                                    name: "autoSave",  
                                                                    sleep: 45000 // CHANGED  
                                                                });  
                                                                myIdleTask.addEventListener(IdleEvent.ON_IDLE, onIdleEventHandler, false);  
                                                            }  
                                                        }