how to write an applescript and ignore er…
apple Verified ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
I'm writing an applescript that uses a shell command that will always return an error. Applescript stops as soon as the error occurs. I want the applescript to continue running. How should I tell Applescript to ignore errors?
8 replies
Sign in or join Beachball Help to reply.
- 0
red_menace ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Normally, AppleScript waits until the shell script is complete before continuing. In Camelot's example, the last "&" runs the shell script in the background, so AppleScript doesn't wait. Removing the "&" should make your script wait - see do shell script in AppleScript for more information.
- 0
dev_sleidy ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
01. Provide specifics when requesting assistance - such as the actual UNIX command called via 'do shell script'
02. ...
try
set tResults to do shell script (...)
.
.
.
on error
-- Your code here.
end... or ...
try
set tResults to do shell script (...)
.
.
.
end... However, without specifics, the above may also not be a valid solution.
- 0
adamb529 ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
set pst_input to choose file with prompt "Select .pst file:"
set mbox_output to choose folder with prompt "Select empty folder for .mbox files:"
set shell_script to "usr/local/bin/readpst -o " & mbox_output & " " & pst_input
do shell script shell_script with administrator privilegesI'm using libpst to convert pst to mbox. Because of the format of pst files, probably because they're a Microsoft format, an error is always returned: debug_fp is NULL. When I run the script in Terminal, it continues, and converts the file. Applescript stops when it encounters the error. Any ideas?
- 0
Camelot ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
The basic construct is a try/end try block which runs your own code when an error occurs rather than stopping your script.
An alternative when using
do shell script is to suppress stdout and stderr so that errors are not returned to the application. Which you can do by appending ' > /dev/null 2>&1 &' to the end of the shell command.Note, though, that your command will not work as written because 'choose folder' will return an alias object which can not be passed to a shell command as-is. Shell commands require unix-style paths for all file/folder parameters. Fortunately, AppleScript makes this easy by providing a
POSIX path command which will convert an alias to a unix path. Additionally, since Mac paths can commonly include spaces and other non-shell safe characters, you may want to quote the path, too, like:<pre class=command>set shell_script to "usr/local/bin/readpst -o " & quoted form of POSIX path of mbox_output & " " & pst_input & " > /dev/null 2>&1 &"</pre>
- 0
adamb529 ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
That worked perfectly! Thanks so much.
One last thing...how do I tell the script to wait until the command has finished to display a dialog. I tried this, but the dialog appears immediately, while the script is still running. Any ideas? Also, what tags do you need to create the gray background for code?
set pst_input to choose file with prompt "Select .pst file:"
set mbox_output to choose folder with prompt "Select empty folder for .mbox files:"
set shell_script to "usr/local/bin/readpst -o " & quoted form of POSIX path of mbox_output & " " & quoted form of POSIX path of pst_input & " > /dev/null 2>&1 &"do shell script shell_script with administrator privileges
display dialog "Your files have been successfully converted!"
- 0
adamb529 ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Does somebody know how to tell the script to wait until the shell script has completed? I need to know how to do this. I can't use a wait command because there is no set time interval.
- 0
red_menace ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Normally, AppleScript waits until the shell script is complete before continuing. In Camelot's example, the last "&" runs the shell script in the background, so AppleScript doesn't wait. Removing the "&" should make your script wait - see do shell script in AppleScript for more information.
- 0
Joe White ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Hey Adam,
did you get it working? Which version of libpst are you using? I've had trouble compiling any version that would support Outlook 2003.
You can email me at the address in my public profile.