2016-09-28 97 views
0

我正在尝试编写一个bash脚本来重新载入给定的chrome选项卡,并且我将变量POSITION_STRING传递给applescript以动态确定该语句的定义(因为我认为这是什么heredocs符号被用来做)。如何解释applescript命令中的bash变量

但似乎苹果拒绝这种类型的内涵,帮助?

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 

回答

1
  1. 的AppleScript的对象符接受基于整数的索引就好了。绝对不需要使用first,second等关键字,并且您正在挖掘一个洞,试图将它们嵌入到AppleScript代码中。

  2. 使用osascript时,将参数传递到AppleScript的正确方法是将它们放在文件名后面(如果有的话)。然后osascript会将这些参数作为文本值列表传递给AppleScript的run处理程序,然后您可以根据需要提取,检查,强制等。

例子:

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 
+0

多感谢你,阿尔法 – user2167582