How to Error Handle "do shell script"?

Hello,

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.

I’m not sure if it is helpful or not, but the shell script I am running is: https://github.com/swesterfeld/audiowmark

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.

Anyone have any thoughts or suggestions?

Thanks in advance!

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

Thanks Robert,

There doesn’t ‘seem’ to be anything in the result if there is no watermark (at least in terminal). Is there somewhere else I am able to check this?

How did you verify what’s in the result?

Try this after the do shell script


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

Tell me what the dialog says

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

The following is from the do shell script technical note:

https://developer.apple.com/library/archive/technotes/tn2065/_index.html#//apple_ref/doc/uid/DTS10003093-CH1-TNTAG2-HOW_DOES_DO_SHELL_SCRIPT_REPORT_ERRORS_

So, it would appear that the audiomark utility does not return an error under the circumstances specified by the OP.

The following is from the “Retrieving a Watermark” section of the audiomark readme document:

https://github.com/swesterfeld/audiowmark#readme

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.

Sorry for the delay.

Thanks to everyone for the responses!

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!