6

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.

2
  • why don't you write everything in either bash or applescript? why this complexity? Commented Aug 17, 2010 at 13:36
  • Simply because I have no idea how to use a GUI window window and get it's argument (selected file) back to my shell script, without using apple script. I have read about Cocoadialog, but that would need additional tools to be installed which does not work for me, because co-workers should also be able to use it. Commented Aug 17, 2010 at 14:25

3 Answers 3

8

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
Sign up to request clarification or add additional context in comments.

7 Comments

+1 for the reasoning. though I already had hacked my way around this, using: mkdir -p foo.app/Contents/MacOS mv foo.sh foo.app/Contents/MacOS/foo chmod +x foo.app/Contents/MacOS/foo
accepted this one because it was closest to what I used in the end
$? -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.
Can I wrap this in a if clause like if [$1 =''] use what you said else use the $1 as file name?
Use if [-z "$myfile" ] to test if $myfile holds an empty string. See topherdotcom.com/scribble/bash/bash-test-operators
|
2

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.)

5 Comments

That does not work, simply because the oascript that opens the dialogbox is not executed. I need it to select the file
@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)?
it says: ++ oascript ./test2: line 7: oascript: command not found + myfile=
@ran2: that was a typo on my part, fixed.
Thx no_ext="${myfile%.*}" really helps. :) That's what the python was for :) .
2

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

1 Comment

This also works for me since I had circumvent the problem sakra mentioned (see comment)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.