是否可以初始化一个变量,然后使用if语句输出相同的变量?

时间:2019-08-11 17:32:07

标签: applescript

我正在使用“我的触控栏我的规则”来配置新按钮。预设允许以字符串格式添加代码。我正在尝试为Spotify编写一个无序播放按钮,当Spotify关闭时该按钮不会消失。

我正在尝试初始化一个新变量(“ msg”),该变量的输出将根据“ shuffle”布尔值(真/假)的状态而改变。随机播放按钮仅显示“错误”,而不显示基于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 \"\""},
}

1 个答案:

答案 0 :(得分:0)

if 语句的 then 部分必须与条件语句在同一行,因此实际脚本应类似于:

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 \"\""}