13 Replies Latest reply on May 2, 2011 11:06 AM by MarkWalsh

    Error trapping for 'Save' dialog open?

    MarkWalsh Level 4

      I'm writing a program which will do an extensive amount of work in Photoshop, and I'm trying to make sure it's as idiot-proof as possible. Before starting the image editing code, I want to make sure the user hasn't left any dialogs open in Photoshop, so I can show them a warning and cancel the application. I have found that attempting to get any properties of the application (e.g. version, document count, build, etc.) will throw an error if the user has the 'Open' dialog showing, but if they have the 'Save' dialog open, there is no error. Is there any reliable way to determine if the user has left Photoshop in a state where it will cause problems executing code?

       

      My script is currently written in Applescript, but will hopefully be someday rewritten in Javascript, so I'd be interested in any suggestions in either language with preference leaning towards Javascript.

        • 1. Re: Error trapping for 'Save' dialog open?
          Muppet Mark-QAl63s Level 4

          Mark, I may be the only one here that uses both languages although using the ESTK more than AppleScript these days. Shifting the main body of your process to JavaScript shouldn't be a problem. You will get plenty of help if needed from others with regards to that… You may only have me listening though with regard the AppleScript. I would expect that you could find out more from AppleScript than you could JavaScript simply because you have a wider amount of access to the system… I would not expect either language to be able to do anything while a dialog is open thats the nature of a dialog isn't it? With AppleScript you may be able to check for the existence of a dialog then dismiss it… with a keystroke or such…

          • 2. Re: Error trapping for 'Save' dialog open?
            MarkWalsh Level 4

            Thanks for the response. I also use Javascript more for scripting CS programs as well. It's more of an issue of time than of questions that is keeping me from converting the script to Javascript. The original applescript is very long (about 40 pages worth), and I have been converting it to be used as a feature of an application I have been working on in RealBasic. Since it was originally written in applescrirpt, and I can easily call the applescript with parameters from Realbasic, I have kept it in Applescript for now.

             

            As for the main question; I'm not concerned with dismissing the dialog or anything, I just want to be able to alert the user and prevent the part of the program which processes the images from running if there is a dialog open, or something else happening that would prevent the code from running properly. I was happy when I found that it threw an error when one dialog was open, so I was assuming it would do so for any other dialog. Unfortunately, I have since found that is not the case, and I am looking for a more reliable way to detect such a circumstance. It is entirely possible that Applescript may have a better chance at detecting this than Javascript (or the converse could also be the case too - I haven't tried to see if I get an error in Javascript yet, I'll try that later)

            • 3. Re: Error trapping for 'Save' dialog open?
              Muppet Mark-QAl63s Level 4

              Mark, what version are you going to be using this with? I have just tried a very basic test with a bunch of different app dialogs open. I have no problem accessing anything regards the app version etc. with any dialog open. What does be consistent is trying to access any document info such as a basic count… This does throw the error with all the brief checks I made… I presume you mean the 'save as' dialog, save has no dialog… This is all I did to try this out…

               

              tell application "Adobe Photoshop CS5"

              try

              set psv to version

              log psv

              on error

              display dialog psv

              end try

              try

              get count of every document

              on error

              display dialog "Can't access any document stuff…"

              end try

              end tell

              • 4. Re: Error trapping for 'Save' dialog open?
                MarkWalsh Level 4

                Sorry, yes I do mean the 'Save As' dialog. I am currently using CS4 (although it is possible some of my coworkers could be using CS3). I ran your code with both the 'Open file' dialg as well as the 'Save As' - I got the "Can't access any document stuff…" message when the Open dialog was active, but when the 'Save as' dialog was open, it retreived the correct document count.

                 

                Are you getting different results in CS5? (A few coworkers have it installed here, I'll try running tests here as well) Are you getting the error with either dialog open? I guess they probably 'fixed' the problem in CS5, but as I am still working with CS4 and earlier, it's possible that this might not even be possible for me.

                • 5. Re: Error trapping for 'Save' dialog open?
                  MarkWalsh Level 4

                  Just tried on a coworker's machine, and it does appear to work properly in CS5. That's really annoying that it doesn't work consistently across the versions. I'll continue to look for a solution that also works in CS4, but maybe I'll just have to use what I can, and eventually the rest of our company will be moved to CS5.

                  • 6. Re: Error trapping for 'Save' dialog open?
                    MarkWalsh Level 4

                    Wow.

                     

                    You can even change the current document while the 'Save as' dialog is open in CS4. At least it does manage to save the correct document when you close the dialog (although that document will no longer be the active one once it closes), but IMHO, you should not be able to do that!

                    • 7. Re: Error trapping for 'Save' dialog open?
                      Muppet Mark-QAl63s Level 4

                      I ran with about 30 different app dialogs open… new doc, open doc, some adjustment ones and the save as… I got the same error thrown with all. You may be upgrading to CS5.5? where things may change again… Such things are done just to test us…

                      1 person found this helpful
                      • 8. Re: Error trapping for 'Save' dialog open?
                        MarkWalsh Level 4

                        Yeah, I am quite often amazed at the lengths that Adobe and Microsoft will go to to specifically vex me.

                         

                         

                        ...and it gets worse! You can even ALTER the current document while the save as dialog is open!! I figured doing something destructive like 'Flatten' would surely throw an error, but no! You are totally allowed to do that in CS4.

                        • 9. Re: Error trapping for 'Save' dialog open?
                          Muppet Mark-QAl63s Level 4

                          I not got CS4 but that would sound like a bug to me… They don't have to go to any great lengths to stump me… I can do that by myself…

                          • 10. Re: Error trapping for 'Save' dialog open?
                            MarkWalsh Level 4

                            Well, they just keep on amazing me. And it doesn't seem like they went to any great lengths to do so either.

                             

                            During this 'exercise' I just kept thinking I'd eventually find something that would throw an error while the dialog was up...

                             

                            "Well, I shouldn't be able to do _____."

                            "...well, I certainly won't be able to do _____..."

                            "...ok, there's absolutely no way I would be able to ____."

                             

                             

                            ...I thought being able to alter the document was bad enough - but you can even CLOSE the document you are trying to save from an external script!! ...and you are guaranteed a crash when you click the 'save' button at that point.

                             

                             

                             

                            The only thing I have come across so far which doesn't seem to work with the save as dialog open is opening another document. And even that doesn't throw an error - it just hangs the script (no change with 'try/on error...", no change with 'with timeout...")

                             

                             

                            ...oh, well. It's almost 5:00 on a Friday. I think I'll just go home feeling defeated.

                            • 11. Re: Error trapping for 'Save' dialog open?
                              MarkWalsh Level 4

                              Persistence finally pays off. I found that, while Applescript can access application properties and doesn't produce an error when certain dialogs are open, Javascript can't access those properties. Combined with a very short 'timeout' in Applescript, this appears to be the solution I was looking for:

                               

                               

                              on run

                              tell application "Adobe Photoshop CS4"

                              try

                              with timeout of 0.5 seconds

                              do javascript "var docCount = documents.length;"

                              set appbusy to false

                              end timeout

                              on error

                              set appbusy to true

                              end try

                              end tell

                              return appbusy

                              end run

                               

                               

                              edit - Found out that "Activate" is also not available when a dialog is open, so this will work also:

                               

                              on run

                              tell application "Adobe Photoshop CS4"

                              try

                              with timeout of 0.5 seconds

                              activate

                              set appbusy to false

                              end timeout

                              on error

                              set appbusy to true

                              end try

                              end tell

                              return appbusy

                              end run

                               

                               

                              The only problem being in both cases, if Photoshop is NOT running when the script is run, it will return a false negative, so I have to make sure to test whether the application is running before I test if it is busy.

                              • 12. Re: Error trapping for 'Save' dialog open?
                                Muppet Mark-QAl63s Level 4

                                I've used a very short time out to catch a file error dialog before now… That is pretty much the same thing… You could have system events look at the processes…

                                 

                                tell application "System Events"

                                try

                                set psRunning to first process whose name contains "Adobe Photoshop"

                                display dialog "Yep"

                                on error

                                display dialog "Nop"

                                end try

                                end tell

                                1 person found this helpful
                                • 13. Re: Error trapping for 'Save' dialog open?
                                  MarkWalsh Level 4

                                  Thanks, I had gotten the check working not long after I posted my last reply (It's been a while since I've done too much applescript, I've gotten a bit rusty). This is what I ended up using for now, and it seems to be working as well as I need it to (although I may just throw in a few more 'Try' blocks to be absolutely sure nothing goes wrong):

                                   

                                  on run

                                  tell application "System Events"

                                  set appCount to count (every process whose creator type is "8BIM")

                                  end tell

                                  if appCount > 0 then

                                  tell application "Adobe Photoshop CS4"

                                  try

                                  with timeout of 0.5 seconds

                                  activate

                                  set appbusy to false

                                  end timeout

                                  on error

                                  set appbusy to true

                                  end try

                                  end tell

                                  else

                                  set appbusy to false

                                  end if

                                  return appbusy

                                  end run