5

I have a file with a list of URL's. I need to pass each URL from a bash script to be used in an AppleScript.

while read urls
do 
    osascript -e \
    'tell application "Safari"
        tell window 1 
            tell current tab to open location "$urls"
        end tell
    end tell'
done <file.txt

This doesn't work. It passes the literal string $urls as the location to be opened in Safari. How can I pass the bash variable $urls to AppleScript?

2

This should work:

while read urls
do 
    osascript -e "tell application \"Safari\" to open location \"$urls\""
done<file.txt
2

The quotes don't expand the variable in AppleScript. What you need to do is ESCAPE the quotes:

'tell current tab to open location \"$urls\"`

However, a better way to do this is using run argv. For example:

#!/bin/bash
url="foobar.com"
osascript -e 'on run {myurl}' -e 'tell application "Safari" to set the URL of the document to myurl' -e 'end run' $url 

My preference is to use a separate script for the AppleScript. In bash, this would look like:

#!/bin/bash
osascript -e 'do scriptname \"$url\"'
  • The double quotes do expand, the single ones around the whole script don't. – nohillside Dec 29 '16 at 16:39
  • @patrix - I've never run into the issue where what was in the single quote didn't work as expected - at least from what I recall. – Allan Dec 29 '16 at 16:56
  • I'm getting an error - syntax error: A “,” can’t go after this command name. – I0_ol Dec 29 '16 at 17:16
  • That comma was a typo; it shouldn't be there. – Allan Dec 29 '16 at 17:19
  • Ok I see that now. Will this work to open a list of multiple url's? – I0_ol Dec 29 '16 at 17:23

You must log in to answer this question.

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