Error when running terminal command through Applescript or KM

Hey Peter,

Nyet! Don't do that!  :sunglasses:

You're running a do shell script_ AppleScript command inside a Terminal.app script.

tell application "Terminal"
   try
      do shell script "gifsicle --scale 0.8 -i /Users/pp/PostHere/startfile.gif > outputfile.gif"
   on error
      say "got that gifsicle error"
   end try
end tell

That will never work...

You can use AppleScript to run the shell command IN the Terminal.

Or you can run the shell command embedded in an AppleScript.

try
   
   do shell script "
   export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/bin:$PATH;
   
   gifsicle --scale 0.8 -i /Users/pp/PostHere/startfile.gif > outputfile.gif
   "
   
on error e number n
   set e to e & return & return & "Num: " & n
   if n ≠ -128 then
      try
         say "got that gifsicle error"
         tell application (path to frontmost application as text) to set ddButton to button returned of ¬
            (display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
               default button "OK" giving up after 30)
         if ddButton = "Copy Error Message" then set the clipboard to e
      end try
   end if
end try

But I don't know why you'd consider this the preferred method.

Rule-of-the-thumb – never run a shell script from an AppleScript IF you have convenient access to the shell – unless of course you need the do shell script output as part of a larger script.

See this to understand why your script wasn't working:

Create a PATH Environment Variable for Keyboard Maestro and Add /usr/local/bin to the Default Path

Also see this:

Path in Shell Scripts

Neither Keyboard Maestro's Execute a Shell Script action nor AppleScript's do shell script command are able to see any changes that have been made to the macOS system $PATH environment variable.

The USER is responsible for educating the script to look in the right place for command line apps.

-Chris

2 Likes