Sh.Result return issue

I have a method with a simple shell command to see whether or not Photoshop is running before continuing on.
If it is, it continues on with another method, and if it’s not it quits.

When Photoshop is open, sh.result returns true. As it should.
However, the if condition never runs. It goes straight to the else condition.

Code:

sh = new shell
dim cmd as string 
cmd =  "/usr/bin/osascript -e 'running of app id ""com.adobe.photoshop""'"
sh.Execute(cmd)
s = sh.Result

if s = "true" then //if sh.result = "true" then
    ProcessImages()
else 
    MsgBox("Please open at least 1 image in Photoshop before running AutoCrop.")
    quit
end if

sh.Result isn’t what you think it is, and my best guess is that it contains an EOL character.

Try s.Trim first.

1 Like

I figured it out.
I encoded the response to UTF-8 and checked the response in unicode and saw that there was an End Of Line character at the end.

1 Like

Haha. Beat me before I could post my comment.
Thanks Kem.

1 Like

I’ve been working with shell.Result recently and found some other issues, hope this is helpful. In addition to the encoding issues and end-of-line issues above, also watch for these:

  • Some utilities return 1 or more blank lines before and/or after the actual results.
  • Tabular results may have 1 or more header lines.
  • Default timeout of 2 seconds is often OK but not always - I’ve been using 5 seconds. OK to use more for a background task that a user isn’t waiting on.

Be prepared to have your application break if the utility changes the format of it’s output for any reason.

1 Like

Thanks for the info @Eric_Bloom

Agreed. I don’t understand how it’s safe to use the result of a shell command in several situations; when an API is available for the same task, I’d always prefer it over some formatted strings.

To check whether an application is running, there are other ways (including an API, the MBS plugin (the Process class) and “System Event”).