Error when running terminal command through Applescript or KM

Hello, all. I'm trying to run a gifsicle script to resize gifs that I need to make smaller. While the script works fine in Terminal directly, I get a "Permission denied" error when I try it in this Applescript:

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

end if

So, I thought, I'd try running the shell script command in an Execute Shell Script command through KM, but I end up with an error, which was

/var/folders/[and a bunch of garbage I couldn't read]

I can get the script to work if I use the Execute a Script in Terminal (from here Execute a Script in Terminal), which is like opening it and typing it, but this is less satisfying since I want to make a script that runs more reliably and elegantly.

Can anyone tell me why I can't run gifsicle commands from an Applescript (the preferred method), or from a KM shell command (a fallback position)? Thanks!

If you search the forum for shell, a number of threads, including very recent ones, will help you understand and fix this.

The point is that Terminal.app runs shell scripts in its own special environment, whereas KM Execute Shell Script actions run them in a plain vanilla environment.

(i.e. what you are trying to run is a 'shell' command line rather than a 'Terminal' command line, and shell is the search term you need)

1 Like

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

Outstanding, thank you very much!!