是否可以初始化一个变量,然后使用 if 语句输出相同的变量?
Is it possible to initialize a variable and then output the same variable using if statements?
我正在使用 "My Touch Bar My Rules" 配置新按钮。预设允许以字符串格式添加代码。我正在尝试为 Spotify 编写一个随机播放按钮,当 Spotify 关闭时它不会消失。
我正在尝试初始化一个新变量 ("msg"),其输出将根据 "shuffle" 布尔值 (true/false) 的状态而变化。随机播放按钮只显示"error",而不显示基于spotify的随机播放变量的状态。
代码如下:
{
{"type": "appleScriptTitledButton", "source":
{"inline": "property msg: \"\"
if application \"Spotify\" is running then
tell application \"Spotify\"
if player state is playing then
if shuffling is true then
set msg to \"on\"
else
set msg to \"off\"
then
return msg
end if
else
return msg
end if
end tell
end if
return \"\""},
}
then 语句的 if 部分需要与条件语句在同一行,因此实际脚本应该是喜欢:
property msg: ""
if application "Spotify" is running then
tell application "Spotify"
if player state is playing then
if shuffling is true then
set msg to "on"
else
set msg to "off"
end if
else
return msg
end if
end tell
end if
return ""
这将翻译(未测试)为:
{"inline": "property msg: \"\" \rif application \"Spotify\" is running then\rtell application \"Spotify\"\rif player state is playing then\rif shuffling is true then\rset msg to \"on\"\relse\rset msg to \"off\"\rend if\relse\rreturn msg\rend if\rend tell\rend if\rreturn \"\""}
我正在使用 "My Touch Bar My Rules" 配置新按钮。预设允许以字符串格式添加代码。我正在尝试为 Spotify 编写一个随机播放按钮,当 Spotify 关闭时它不会消失。
我正在尝试初始化一个新变量 ("msg"),其输出将根据 "shuffle" 布尔值 (true/false) 的状态而变化。随机播放按钮只显示"error",而不显示基于spotify的随机播放变量的状态。
代码如下:
{
{"type": "appleScriptTitledButton", "source":
{"inline": "property msg: \"\"
if application \"Spotify\" is running then
tell application \"Spotify\"
if player state is playing then
if shuffling is true then
set msg to \"on\"
else
set msg to \"off\"
then
return msg
end if
else
return msg
end if
end tell
end if
return \"\""},
}
then 语句的 if 部分需要与条件语句在同一行,因此实际脚本应该是喜欢:
property msg: ""
if application "Spotify" is running then
tell application "Spotify"
if player state is playing then
if shuffling is true then
set msg to "on"
else
set msg to "off"
end if
else
return msg
end if
end tell
end if
return ""
这将翻译(未测试)为:
{"inline": "property msg: \"\" \rif application \"Spotify\" is running then\rtell application \"Spotify\"\rif player state is playing then\rif shuffling is true then\rset msg to \"on\"\relse\rset msg to \"off\"\rend if\relse\rreturn msg\rend if\rend tell\rend if\rreturn \"\""}