如何解释 applescript 命令中的 bash 变量
how to interpret bash variables in applescript command
我正在尝试编写一个 bash 脚本来重新加载给定的 chrome 选项卡,并且我正在将变量 POSITION_STRING
传递给 applescript 以动态确定语句的定义(因为我认为这就是 heredocs 符号用来做的事情)。
但 applescript 似乎拒绝这种类型的内涵,求助?
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[]}"
POSITION=
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
AppleScript 的对象说明符可以很好地接受基于整数的索引。绝对没有必要使用 first
、second
等关键字,您正在为自己挖一个坑,试图将它们拼凑成 AppleScript 代码。
使用 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
此处文档中的引用规则与 bash 脚本正文中的引用规则不同。您此处文档中的这一行:
tell the "$POSITION_STRING" tab of its first window
…扩展为(例如)这个:
tell the "first" tab of its first window
…这在语法上是无效的。删除该行 $POSITION_STRING
周围的引号,它应该可以工作。然而:
正如@foo 所说,AppleScript 可以很好地理解数字索引;无需使用英文单词索引。使用 tab $POSITION
代替,它适用于任何数字,而不仅仅是一到十,而且您的脚本会更短。
再次呼应@foo,尝试将变量替换为脚本文本通常是一场失败的游戏。 (对于整数,你可以不用它,但它会因为引用而成为字符串的问题。)让你的脚本接受它自己的参数,然后将它们传递给 osascript
(1) - 手册页显示了如何做到这一点.请注意,参数将始终以字符串形式出现,因此如果您需要数字,则需要强制转换它们:
/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 …
如果您的 bash 脚本只是转身并调用 osascript
(1),您可以更进一步:osascript
作为Unix 解释器,所以你可以像这样重写你的脚本:
#!/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 …
将其单独保存在一个文件中,将其标记为可执行文件,Bob 就是你的叔叔。 (除非你需要一个 bash 函数 ,在这种情况下,这里的文档是一个合理的选择。)
osascript -e
可以接受换行符和变量。可能有使用 HEREDOC 的原因,但我不知道它们,我发现它更容易使用。
示例:
TEST='Hello, World!'
osascript -e '
on run argv
display dialog item 1 of argv
end run
' $TEST
我正在尝试编写一个 bash 脚本来重新加载给定的 chrome 选项卡,并且我正在将变量 POSITION_STRING
传递给 applescript 以动态确定语句的定义(因为我认为这就是 heredocs 符号用来做的事情)。
但 applescript 似乎拒绝这种类型的内涵,求助?
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[]}"
POSITION=
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
AppleScript 的对象说明符可以很好地接受基于整数的索引。绝对没有必要使用
first
、second
等关键字,您正在为自己挖一个坑,试图将它们拼凑成 AppleScript 代码。使用
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
此处文档中的引用规则与 bash 脚本正文中的引用规则不同。您此处文档中的这一行:
tell the "$POSITION_STRING" tab of its first window
…扩展为(例如)这个:
tell the "first" tab of its first window
…这在语法上是无效的。删除该行 $POSITION_STRING
周围的引号,它应该可以工作。然而:
正如@foo 所说,AppleScript 可以很好地理解数字索引;无需使用英文单词索引。使用
tab $POSITION
代替,它适用于任何数字,而不仅仅是一到十,而且您的脚本会更短。再次呼应@foo,尝试将变量替换为脚本文本通常是一场失败的游戏。 (对于整数,你可以不用它,但它会因为引用而成为字符串的问题。)让你的脚本接受它自己的参数,然后将它们传递给
osascript
(1) - 手册页显示了如何做到这一点.请注意,参数将始终以字符串形式出现,因此如果您需要数字,则需要强制转换它们:/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 …
如果您的 bash 脚本只是转身并调用
osascript
(1),您可以更进一步:osascript
作为Unix 解释器,所以你可以像这样重写你的脚本:#!/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 …
将其单独保存在一个文件中,将其标记为可执行文件,Bob 就是你的叔叔。 (除非你需要一个 bash 函数 ,在这种情况下,这里的文档是一个合理的选择。)
osascript -e
可以接受换行符和变量。可能有使用 HEREDOC 的原因,但我不知道它们,我发现它更容易使用。
示例:
TEST='Hello, World!'
osascript -e '
on run argv
display dialog item 1 of argv
end run
' $TEST