Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppleScript : error "sh: lame: command not found" number 127

I am trying to create an AppleScript with commands below. An issue I am having is there is an error at the third line. I have no problem using the lame command in the terminal directly. In addition, lame is not a native Mac utility; I installed it on my own. Does anybody have a solution?

do shell script "cd ~/Downloads"

do shell script "say -f ~/Downloads/RE.txt -o ~/Downloads/recording.aiff"

do shell script "lame -m m ~/Downloads/recording.aiff ~/Downloads/recording.mp3"

-- error "sh: lame: command not found" number 127

do shell script "rm recording.aiff RE.txt"
like image 721
Evan S Avatar asked May 18 '14 13:05

Evan S


1 Answers

To complement Paul R's helpful answer:

The thing to note is that do shell script - regrettably - does NOT see the same $PATH as shells created by Terminal.app - a notable absence is /usr/local/bin.

On my OS X 10.9.3 system, running do shell script "echo $PATH" yields merely:

/usr/bin:/bin:/usr/sbin:/sbin

There are various ways around this:

  • Use the full path to executables, as in Paul's solution.

  • Manually prepend/append /usr/local/bin, where many non-system executables live, to the $PATH - worth considering if you invoke multiple executables in a single do shell script command; e.g.:

do shell script "export PATH=\"/usr/local/bin:$PATH\"
 cd ~/Downloads
 say -f ~/Downloads/RE.txt -o ~/Downloads/recording.aiff
 lame -m m ~/Downloads/recording.aiff ~/Downloads/recording.mp3
 rm recording.aiff RE.txt"

Note how the above use a single do shell script command with multiple commands in a single string - commands can be separated by newlines or, if on the same line, with ;.
This is more efficient than multiple invocations, though adding error handling both inside the script code and around the do shell script command is advisable.

  • To get the same $PATH that interactive shells see (except additions made in your bash profile), you can invoke eval $(/usr/libexec/path_helper -s); as the first statement in your command string.

Other important considerations with do shell script:

  • bash is invoked as sh, which results in changes in behavior, most notably:
    • process substitution (<(...)) is not available
    • echo by default accepts no options and interprets escape sequences such as \n.
    • other, subtle changes in behavior; see http://www.gnu.org/software/bash/manual/html_node/Bash-POSIX-Mode.html
    • You could address these issues manually by prepending shopt -uo posix; shopt -u xpg_echo; to your command string.
  • The locale is set to the generic "C" locale instead of to your system's; to fix that, manually prepend export LANG='" & user locale of (system info) & ".UTF-8' to your command string.
  • No startup files (profiles) are read; this is not surprising, because the shell created is a noninteractive (non-login) shell, but sometimes it's handy to load one's profile by manually by prepending . ~/.bash_profile to the command string; note, however, that this makes your AppleScript less portable.

do shell script command reference: http://developer.apple.com/library/mac/#technotes/tn2065/_index.html

like image 192
mklement0 Avatar answered Nov 08 '22 00:11

mklement0