Skip to main content
MarkWalsh
Inspiring
April 28, 2011
Question

Error trapping for 'Save' dialog open?

  • April 28, 2011
  • 2 replies
  • 1737 views

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.

This topic has been closed for replies.

2 replies

MarkWalsh
MarkWalshAuthor
Inspiring
May 2, 2011

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.

Muppet_Mark-QAl63s
Inspiring
May 2, 2011

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

MarkWalsh
MarkWalshAuthor
Inspiring
May 2, 2011

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

Muppet_Mark-QAl63s
Inspiring
April 29, 2011

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…

MarkWalsh
MarkWalshAuthor
Inspiring
April 29, 2011

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)

Muppet_Mark-QAl63s
Inspiring
April 29, 2011

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