问题
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"
回答1:
Probably a PATH
problem - use the full path for lame, e.g.
do shell script "/usr/local/bin/lame -m m ~/Downloads/recording.aiff ~/Downloads/recording.mp3"
回答2:
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 singledo 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 invokeeval $(/usr/libexec/path_helper -s);
as the first statement in your command string.
Other important considerations with do shell script
:
bash
is invoked assh
, 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.
- process substitution (
- The locale is set to the generic
"C"
locale instead of to your system's; to fix that, manually prependexport 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
回答3:
I have been struggling to get the path of an installed BASH command via Applescript for a long time. Using the information here, I finally succeeded.
tell me to set sox_path to (do shell script "eval $(/usr/libexec/path_helper -s); which sox")
Thanks.
回答4:
Url:http://sourceforge.net/project/showfiles.php?group_id=290&package_id=309
./configure
make install
来源:https://stackoverflow.com/questions/23722559/applescript-error-sh-lame-command-not-found-number-127