Saturday, November 27, 2021

#1 2021-11-22 11:59:41 am

darbison
Member
Registered: 2021-11-19
Posts: 2

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:

Applescript:

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:

Applescript:

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:

Applescript:

The actions "Run Applescript" encountered an error: "audiowmark: error parsing commandline args (use audiowmark -h)"

So I also tried:

Applescript:

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!

Last edited by darbison (2021-11-22 12:00:55 pm)

Offline

 

#2 2021-11-22 04:50:32 pm

robertfern
Member
Registered: 2011-11-29
Posts: 117

Re: How to Error Handle "do shell script"?

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

Offline

 

#3 2021-11-22 04:53:51 pm

darbison
Member
Registered: 2021-11-19
Posts: 2

Re: How to Error Handle "do shell script"?

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?

Offline

 

#4 2021-11-22 06:02:20 pm

robertfern
Member
Registered: 2011-11-29
Posts: 117

Re: How to Error Handle "do shell script"?

How did you verify what’s in the result?

Try this after the do shell script

Applescript:


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

Last edited by robertfern (2021-11-22 06:04:48 pm)

Offline

 

#5 2021-11-22 11:08:15 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2154

Re: How to Error Handle "do shell script"?

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.

Applescript:

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

Last edited by KniazidisR (2021-11-22 11:09:34 pm)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#6 2021-11-23 07:19:39 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1133

Re: How to Error Handle "do shell script"?

The following is from the do shell script technical note:

How does do shell script report errors?
All shell commands return an integer status when they finish: zero means success; anything else means failure. If the script exits with a non-zero status, do shell script throws an AppleScript error with the status as the error number. (The man page for a command should tell you what status codes it can return. Most commands simply use 1 for all errors.) If the script printed something to the standard error stream, that text becomes the error message in AppleScript. If there was no error text, the normal output (if any) is used as the error message.



https://developer.apple.com/library/arc … RT_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:

The software was designed under the assumption that you - the user - will be able to decide whether a message is correct or not. To do this, on watermarking song files, you could list each message you embedded in a database. During retrieval, you should look up each pattern audiowmark get outputs in the database. If the message is not found, then you should assume that a decoding error occurred.



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.

Last edited by peavine (2021-11-23 07:27:59 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)