I am running a shell script in my Applescript as part of a larger automator action, but I’m having troubles with figuring out how to handle the result when there is an error from the shell script.
Here is what I am using for reading a watermark on an audio file:
do shell script "/usr/local/bin/audiowmark get " & /path/to/file.wav
When an audio file has a watermark (which has been applied previously via a different command with audiowmark), I am able to process the result just fine. The problem comes when an audio file doesn’t have a watermark.
If I run the command directly in terminal, the command escapes without outputting anything…so, I tried:
if result is "" then display dialogue "No Watermark on this Audio File"
This didn’t yield any success.
However, when running as part of my larger automator action, I do get presented with an error in finder:
The actions "Run Applescript" encountered an error: "audiowmark: error parsing commandline args (use audiowmark -h)"
So I also tried:
if result is "audiowmark: error parsing commandline args (use audiowmark -h)" then
display dialog "No Watermark on this Audio File"
That also wasn’t successful, and I am still presented with the same Finder error.
What exactly is in the result when there is no watermark?
It might be return either a null (missing value in AppleScript) or non printable characters instead of an empty string
Set myanswer to do shell script "/usr/local/bin/audiowmark get " & /path/to/file.wav
Set myclass to class of myanswer
Display alert “Class of the result is ” & myclass
Exists several ways to handle shell command errors. In your case, I would use try block. Following will return watermark on the success, or missing value in other case.
my getWatermark()
on getWatermark()
try
set theWatermark to (do shell script "/usr/local/bin/audiowmark get " & "/path/to/file.wav")
return theWatermark
on error errorMessage
display dialog errorMessage
return missing value
end try
end getWatermark
I don’t have the utility installed for testing, but it would seem the OP could set set a variable to the output of audiomark. If there is no output, then an error will occur and it can be assumed that a watermark was not found. This appears to be exactly what KniazidisR’s suggestion does.
As peavine suggested and found in the utility’s docs, there isn’t an error returned by audiowmark…but luckily, KniazidisR’s way of handling it seems to work great - Thanks for the help!