views:

171

answers:

3

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 except, all of your answers really helped me since I learned a lot from everybody.

+1  A: 

First, you need to get the contents of the myfile variable from Applescript to bash. I don't know Applescript, so I'll make a shot in the dark as to how to write to its standard output. Then the python part is just unnecessary complexity (and likely wrong anyway, you were throwing away everything after the first . rather than the last). Next you need a $ before the variable name in bash syntax. I think the following script does what you want:

#!/bin/sh
set -e
myfile=$(osascript <<EOT
set myfile to choose file
write myfile to stdout
EOT
)
no_ext="${myfile%.*}"
R CMD Sweave "$no_ext.Rnw"
pdflatex "$no_ext.tex"
open "$no_ext.pdf"

(set -e at the beginning makes the shell exit immediately if an error occurs, instead of trying to execute pdflatex even though no .tex file has been produced or somesuch.)

Gilles
That does not work, simply because the oascript that opens the dialogbox is not executed. I need it to select the file
ran2
@ran2: The shell script I've written does invoke `oascript`, but the Applescript bit may be wrong since I don't know Applescript. What do you see if you replace `set -e` by `set -ex` (this causes the shell to print an execution trace)?
Gilles
it says: ++ oascript./test2: line 7: oascript: command not found+ myfile=
ran2
@ran2: that was a typo on my part, fixed.
Gilles
Thx no_ext="${myfile%.*}" really helps. :) That's what the python was for :) .
ran2
+1  A: 

Realize that applescript paths are colon ":" delimited. You need slash delimited in bash so in applescript terms that's the "posix path". Also, when using osascript it can't open dialog windows. You must tell an application to open the window. Next, you "return" something from the applescript... that's what goes to bash. Finally, in bash to execute a command and assign the result to a variable use `` around the command. So knowing this here's a shell script to use an applescript to get the myFile variable.

#!/bin/bash

myFile=`/usr/bin/osascript << EOT
tell application "Finder"
activate
set myfile to choose file with prompt "Select the file to use in bash!"
end tell
return (posix path of myfile)
EOT`

echo $myFile
regulus6633
This also works for me since I had circumvent the problem sakra mentioned (see comment)
ran2
+1  A: 

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
sakra
+1 for the reasoning. though I already had hacked my way around this, using: mkdir -p foo.app/Contents/MacOSmv foo.sh foo.app/Contents/MacOS/foochmod +x foo.app/Contents/MacOS/foo
ran2
eh sorry, can you explain the $? -eq 0 if clause? thx!
ran2
accepted this one because it was closest to what I used in the end
ran2
$? -eq 0 is bash test clause. It tests if the exit status returned by the osascript command is 0. If the user cancels the file selection dialog you'll get a nonzero status.
sakra
Can I wrap this in a if clause like if [$1 =''] use what you said else use the $1 as file name?
ran2
Use if [-z "$myfile" ] to test if $myfile holds an empty string.See http://topherdotcom.com/scribble/bash/bash-test-operators/
sakra