user2167582
user2167582

Reputation: 6368

how to interpret bash variables in applescript command

I am trying to write a bash script that reloads a given chrome tab, and I am passing the variable POSITION_STRING to applescript to dynamically determine the definition of the statement (as I thought thats what heredocs notations are used to do).

But it seems applescript rejects this type of connotation, help?

declare -A POSSIBLE_POSITIONS
POSSIBLE_POSITIONS=(
  ["1"]="first"
  ["2"]="second"
  ["3"]="third"
  ["4"]="fourth"
  ["5"]="fifth"
  ["6"]="sixth"
  ["7"]="seventh"
  ["8"]="eighth"
  ["9"]="ninth"
  ["10"]="tenth"
)

# echo "${POSSIBLE_POSITIONS[$1]}"
POSITION=$1
POSITION_STRING=${POSSIBLE_POSITIONS[$POSITION]}
# echo $POSITION_STRING

/usr/bin/osascript <<EOF
log "$POSITION_STRING" # this works!
tell application "Google Chrome"
  tell the "$POSITION_STRING" tab of its first window
    # reload
  end tell
end tell
EOF

Upvotes: 0

Views: 1333

Answers (3)

TYPKRFT
TYPKRFT

Reputation: 326

osascript -e can accept newlines and variables. There may be reasons to use HEREDOCs, but I'm not aware of them and I find this a bit easier to work with.

Example:

TEST='Hello, World!'
osascript -e '
  on run argv
    display dialog item 1 of argv
  end run
' $TEST

Upvotes: 0

Chris N
Chris N

Reputation: 949

Quoting rules are different in here docs than in the body of a bash script. This line in your here doc:

tell the "$POSITION_STRING" tab of its first window

…expands to (for example) this:

tell the "first" tab of its first window

…which is not syntactically valid. Remove the quotes around $POSITION_STRING on that line and it should work. However:

  1. As @foo says, AppleScript understands numeric indexes just fine; there is no need to use English word indexes. Use tab $POSITION instead and it will work with any number, not just one through ten, and your script will be shorter besides.

  2. Again echoing @foo, trying to substitute variables into a script text is usually a losing game. (For integers you can get away with it, but it becomes problematic for strings because quoting.) Make your script take arguments of its own and then pass them to osascript(1) — the man page shows how to do this. Note that arguments will always come in as strings, so you will need to coerce them if you need a number:

    /usr/bin/osascript - "$POSITION" <<'EOF'
    on run argv -- argv is a list of text
        set n to item 1 of argv as integer
        tell application "Google Chrome"
            tell tab n of window 1 …
    
  3. If your bash script is just turning around and calling osascript(1), you can go one step further: osascript functions perfectly well as an Unix interpreter, so you could rewrite your script like this:

    #!/usr/bin/osascript
    on run argv
        set n to item 1 of argv as integer
        tell application "Google Chrome"
            tell tab n of window 1 …
    

    Save that in a file by itself, mark it executable, and Bob's your uncle. (Unless you needed a bash function, in which case a here doc is a reasonable choice.)

Upvotes: 0

foo
foo

Reputation: 3259

  1. AppleScript's object specifiers accept integer-based indexes just fine. There's absolutely no need to use first, second, etc keywords, and you're digging yourself a hole trying to munge them into AppleScript code.

  2. When using osascript, the correct way to pass arguments into your AppleScript is to put them after the file name (if any). osascript will then pass these arguments to your AppleScript's run handler as a list of text values, which you can then extract, check, coerce, etc. as appropriate.

Example:

POSITION=1

/usr/bin/osascript - "$POSITION" <<'EOF'
  on run argv -- argv is a list of text
    -- process the arguments
    set n to item 1 of argv as integer
    -- do your stuff
    tell application "Google Chrome"
      tell tab n of window 1
        reload
      end tell
    end tell
  end run
EOF

Upvotes: 3

Related Questions