Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a variable FROM applescript TO a shell script?

I have the following script

 #!/bin/bash
 /usr/bin/osascript << EOT
 set myfile to choose file
 EOT

 no_ext=$(python -c "print '$myfile'.split('.')[0]")

 ### this works - just need to know how to pass the arg
 R CMD Sweave no_ext.Rnw
 pdflatex no_ext.tex
 open no_ext.pdf

Can anyone point me to "how to pass the variable myfile correctly" ?

EDIT Thx for all the suggestions!

Don't know what to accept, all of your answers really helped me since I learned a lot from everybody.

like image 773
Matt Bannert Avatar asked Aug 17 '10 13:08

Matt Bannert


1 Answers

The following problems exist in your script:

A variable set in the AppleScript section does become defined in the enclosing shell script. You have to do the data exchange with the shell script by using command substitution.

AppleScripts invoked from a shell script aren't allowed to do user interaction because they do not have an application context. You can use the helper application "AppleScript Runner" to run user interaction commands.

Here is a revised version of your script where those problems are fixed:

#!/bin/bash

myfile=$(/usr/bin/osascript << EOT
tell app "AppleScript Runner"
    activate
    return posix path of (choose file)
end
EOT)

if [ $? -eq 0 ]
then
    echo $myfile
else
    echo "User canceled"
fi
like image 163
sakra Avatar answered Oct 21 '22 22:10

sakra