15 Replies Latest reply on Aug 8, 2016 9:54 AM by Silly-V

    PromptToSaveChanges / ScriptUI Default Highlighted Button

    zertle Level 2

      Hi!

       

      For some reason when I try to close a document using SaveActions.PROMPTTOSAVECHANGES, the prompt never appears and it autosaves and closes. As a workaround, I've just built my own dialog using ScriptUI, but for some reason I can't figure out how to set the OK button to be highlighted by default like the AI prompt.

       

      Untitled-2.jpg

      Untitled-1.jpg

      var dlg = new Window('dialog', 'Save Changes?');

      dlg.orientation = 'column';

       

      dlg.add( 'staticText', undefined, 'Save changes to ' + doc.name + ' before closing?' );

       

      var btnGrp = dlg.add( 'group' );

      var yes = btnGrp.add( 'button', undefined, 'Yes' );

      var no = btnGrp.add( 'button', undefined, 'No' );

      btnGrp.add( 'button', undefined, 'Cancel' );

      dlg.defaultElement = yes;

       

      dlg.show;

       

       

      If you have could point me in the right direction in either, I'd really appreciate it!  Thanks!

        • 1. Re: PromptToSaveChanges / ScriptUI Default Highlighted Button
          pixxxel schubser Level 5

          Try

          yes.active = true;
          

           

          And do not forget the brackets after show()

          • 2. Re: PromptToSaveChanges / ScriptUI Default Highlighted Button
            zertle Level 2

            Thank you so much!  I don't know how I missed that!

            • 3. Re: PromptToSaveChanges / ScriptUI Default Highlighted Button
              zertle Level 2

              Follow up q:

              That did the trick in terms of highlighting the Yes button, but for some reason I still have to hit Tab twice before it will move the selection to the 'No' button. I'm currently running Illustrator CC 2015.2.0, 19.2.0 64bit on Windows 7. Thanks again for your help!

              • 4. Re: PromptToSaveChanges / ScriptUI Default Highlighted Button
                pixxxel schubser Level 5

                no idea.

                But this could be a workaround:

                yes.active = true;
                yes.active = false;
                yes.active = true;
                

                 

                 

                • 6. Re: PromptToSaveChanges / ScriptUI Default Highlighted Button
                  Silly-V Adobe Community Professional
                  SaveActions.PROMPTTOSAVECHANGES

                  Should it not be instead "SaveOptions" ?

                  1 person found this helpful
                  • 7. Re: PromptToSaveChanges / ScriptUI Default Highlighted Button
                    zertle Level 2

                    Man, two for two on silly mistakes 9.9. SaveOptions did the trick, thanks!

                    • 8. Re: PromptToSaveChanges / ScriptUI Default Highlighted Button
                      zertle Level 2

                      Have you noticed the PROMPTTOSAVECHANGES acting buggy at all? For some reason it will work correctly once or twice and then just bypass the dialog and auto-save. ¯\_(ツ)_/¯

                      • 9. Re: PromptToSaveChanges / ScriptUI Default Highlighted Button
                        Silly-V Adobe Community Professional

                        One question - was there any chance that the document in question in such a state that there were no unsaved changes at the time of closing?  But yea I never used that option in all my years yet.

                        • 10. Re: PromptToSaveChanges / ScriptUI Default Highlighted Button
                          zertle Level 2

                          Nope, I've got a conditional

                           

                          if( doc.saved ) {
                              doc.close( SaveOptions.DONOTSAVECHANGES );
                          }
                          else {
                              doc.close( SaveOptions.PROMPTTOSAVECHANGES );
                          }
                          

                           

                          C'est la vie~

                          • 11. Re: PromptToSaveChanges / ScriptUI Default Highlighted Button
                            Silly-V Adobe Community Professional

                            I do agree that this makes sense what you put forth, however in the conditional there's really no way to tell if one statement was executed rather than the other successfully, or vice-versa with error. Could it be that some of the preceding actions did change the document but its status was not updated yet? When this kind of stuff comes up, I generally stick an alert to make sure I'm sure.

                            Hmm, not sure if UserInteractionLevel would affect this either, if that is in play.

                            And, I also notice some strange bugs in my work when I have been using my application instance for testing and getting errors- then sometimes following runs will be affected by my previous work, so I also try to restart my AI every once in a while and I find some of the errors are gone with a fresh instance. Just some thoughts- always good to know about more weird Illustrator stuff!

                            • 12. Re: PromptToSaveChanges / ScriptUI Default Highlighted Button
                              zertle Level 2

                              Interesting... I feel like the 'saved' property usually corresponds to whether the filename has an asterisk next to it in the tab or not.

                               

                              Untitled-1.jpg

                               

                              I've found that the SAVECHANGES and DONOTSAVECHANGES both seem to work pretty consistently, and when the prompt doesn't pop up the file gets overwritten, but I'll try sticking some alerts in there just to make sure. Still v strange...

                               

                              I haven't gotten to the point of restarting AI (yet), but I have added in chunks of code to try cutting and pasting everything and rerunning the script when it encounters a weird error haha.

                              • 13. Re: PromptToSaveChanges / ScriptUI Default Highlighted Button
                                Silly-V Adobe Community Professional

                                The nature of bugs is so tricky, it could be anything- for example, I just thought of this - could your "doc" variable be perhaps referencing any older instance of a document? Also, I wrap each and every one of my scripts, including any snippets ran, into a function-closure to help avoid global variables. If there's a global doc variable from scripts ran previously, maybe it affects the saving.

                                Well I finally tested this for myself and I did so by hiding/showing a layer in script to make sure the saved status really changes - and it does appear to correctly reflect this every time. However when the Cancel button is pressed, an error is returned. I guess they want to wrap it into a try/catch to give you ability to continue with the flow of script if someone does a Cancel.

                                • 14. Re: PromptToSaveChanges / ScriptUI Default Highlighted Button
                                  zertle Level 2

                                  Sorry I should have mentioned that, I stripped down that snippet for clarity. I have that inside a function and this is the full code. I hit that Cancel error too, interesting way to do it but whatever works.

                                   

                                  function DeleteAndClose() {
                                      if( !app.documents.length ) return;
                                      var doc = app.activeDocument;
                                      
                                      if( doc.name == 'PO.pdf' ) {
                                          doc.selectObjectsOnActiveArtboard();
                                          
                                          if( doc.selection.length > 0 )
                                              app.cut();
                                          
                                          if( app.documents.length > 1 )
                                              app.documents[1].close( SaveOptions.DONOTSAVECHANGES );
                                      }
                                      else {
                                          if( doc.saved ) {
                                              doc.close( SaveOptions.DONOTSAVECHANGES );
                                          }
                                          else {
                                              /*try {
                                                  doc.close( SaveOptions.PROMPTTOSAVECHANGES );
                                              }
                                              catch( err ) { }
                                              */
                                              
                                              var dlg = new Window('dialog', 'Save Changes?');
                                              dlg.orientation = 'column';
                                              
                                              dlg.add( 'staticText', undefined, 'Save changes to ' + doc.name + ' before closing?');
                                              
                                              var btnGrp = dlg.add( 'group' );
                                              var yes = btnGrp.add( 'button', undefined, 'Yes' );
                                              var no = btnGrp.add( 'button', undefined, 'No' );
                                              btnGrp.add( 'button', undefined, 'Cancel' );
                                              dlg.defaultElement = yes;
                                              yes.active = true;
                                              yes.active = false;
                                              yes.active = true;
                                              
                                              yes.onClick = function() {
                                                  dlg.close();
                                                  
                                                  var fullPath = doc.fullName.toString();
                                                  var parDir = fullPath.substr( 0, fullPath.lastIndexOf('/') ) + "/";
                                                  
                                                  if( parDir == '/c/Program%20Files/Adobe/Adobe%20Illustrator%20CC%202015/Support%20Files/Contents/Windows/' ) {
                                                      var fileName = fullPath.split( '/' );
                                                      fileName = fileName[ fileName.length-1 ];
                                                      
                                                      var savePath = new File( '~/' + fileName ).saveDlg("Choose PDF output folder:");
                                                      if( savePath == null ) return;
                                                      
                                                      pdfSaveOpts.generateThumbnails = true;
                                                      pdfSaveOpts.preserveEditability = true;
                                                      doc.saveAs( savePath, pdfSaveOpts );
                                                      
                                                      doc.close( SaveOptions.DONOTSAVECHANGES );
                                                      
                                                  }
                                                  else {
                                                      doc.close( SaveOptions.SAVECHANGES );
                                                  }
                                  
                                              }
                                              
                                              no.onClick = function() {
                                                  dlg.close();
                                                  doc.close( SaveOptions.DONOTSAVECHANGES );
                                              }
                                              
                                              dlg.show();
                                          }
                                      }
                                  }
                                  
                                  DeleteAndClose();
                                  

                                   

                                  I haven't had any issues with the dialog window yet which leads me to think that it is something weird about the PROMPT option.

                                  • 15. Re: PromptToSaveChanges / ScriptUI Default Highlighted Button
                                    Silly-V Adobe Community Professional

                                    Okay, I see you are closing app.documents[1], which is not the active document, but it really shouldn't matter due to where it's located. Hmm oh well, same as you: shrug!