I've got a two part problem.

  1. I am trying to pass a variable from AppleScript to be used as a variable inside a bash shell script. I cannot figure out how to this work. I have read about concatenating the variable and the do shell script command with & inside AppleScript. But that doesn't really work with a longer script.

Here is the short AppleScript to select a file and (hopefully) pass it along to the shell as a variable.

tell application "Finder"
    choose file
    set myFile to result as text
end tell
do shell script "/path/to/script.sh"

And then the shell script itself:

while read lines
    echo ${lines##*:}
    grep "string" 
done <$myFile | awk '!x[$0]++' |\
trans -b :en # this is where the second problem occurs

This is a shortened version of the actual script but I think the intent is clear. I am unsure how to pass the myFile variable FROM Applescript to the shell.

  1. The second problem is that the trans command (which is a rather handy command line translator) uses the gawk command which of course is installed along with the trans program itself. But when run from Applescript I get the error message:

error "/usr/local/bin/trans: line 4990: gawk: command not found" number 127

I know that AppleScript needs the fullpath in order for this to work. But how do I provide the full path for a command embedded deep within nearly 5000 lines of code? Do I alter the code of the trans program itself to provide the full path to gawk?

Also I realize that I could do the whole thing from within the shell and that a lot of this is adding complexity where none is needed. But I think it is still worthwhile to know how to do this.

Thanks for reading.

  • I'll take a stab at calling a script with arguments from AppleScript to bash. You'll probably want to chop this down into at least three different questions and possibly think about looking on Stack Overflow for more coding support. By doing the work to isolate each issue - you are more likely to get someone to explain and less likely to have a question closed as too broad. – bmike Jun 21 '16 at 10:52

The first problem can be solved by exporting the path inside the do shell script part:

tell application "Finder"
    choose file
    set myFile to result as text
end tell
do shell script "export MYFILE=" & quoted form of myFile & " ; /Users/user_name/bin/sh/echo_var_myfile.sh"

In my example "echo_var_myfile.sh" is

echo $MYFILE >> /Users/user_name/echo_var_myfile.txt

The executable bit has to be set:

chmod +x /Users/user_name/bin/sh/echo_var_myfile.sh

The result is the path to the chosen file.

You mustn't add a second do shell script for the shell script because as soon as the first one runs it dies and no values are exported.

  • I'm getting this error error "/bin/sh: /Users/username/echo_myfile.sh: Permission denied" number 126 from the applescript console. I tried adding with administrator privileges password "my_password"but I still get the same error message either way. I've enabled applescript editor in the privacy / security settings. I'm confused as to what is causing the issue. SIP protection maybe? – I0_ol Jun 24 '16 at 4:28
  • Ah never mind. I forgot to make the script executable before running it. Oops.. >_< – I0_ol Jun 24 '16 at 5:00

The first problem with the first script is you aren't using the set myFile variable.

Here's a general guide to getting help:

Open Script editor - use the help menu to launch the AppleScript Language Guide and search for the command you have. In this case "do shell script"

That takes you to: https://developer.apple.com/library/prerelease/content/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/CallCommandLineUtilities.html

The example there shows how to set a variable for a host name and then call ping:

set theHostName to "www.apple.com"
do shell script "ping -c1 " & theHostName

So, in your case you'll need:

do shell script "/path/to/script.sh"  & myFile

At that point, you'll likely run into path issues so running small snippets of code in the editor, looking at the result and iterating should get you to solutions as inevitable errors in path, parsing, logic crop up. I left a typo in the second "do shell script" example above - pay attention to spaces and quotes :-)

  • The reason I left the typo is you will have to think about file names and paths containing spaces to program this in general. Apple historically has Macintosh HD with a space whereas bash uses spaces to indicate a new variable - if your code doesn't expect, test and handle spaces - sticking to AppleScript to handle file might be easier. – bmike Jun 21 '16 at 10:55
  • But it seems you've neglected the issue of passing the result of the AppleScript to the shell. All the & myFile does is add the literal "myFile" onto the location of the shell script. That's not right! – Seamus Apr 19 '18 at 15:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .