我正在尝试制作一个用于BTT触控栏的Applescript。 这个想法是打印当前正在播放的艺术家 - 歌名。当艺术家+歌曲长于20个字符时,我想首先显示艺术家,当剧本再次运行时,显示歌曲名称,脚本再次运行显示艺术家,显示歌曲名称等。
我使用atm的脚本只打印艺术家。
有人可以帮我修复脚本吗?谢谢!
if application "Spotify" is running then
tell application "Spotify"
if player state is playing then
-- Gets the text of the current track playing
set _artist to get artist of current track
set _name to get name of current track
set _full_string to _artist & " - " & _name
set _length to length of _full_string
-- Gets the duration of the track that is playing
if (_name = "") or (_artist = "") or (_name = "Listen Now") then
return "Ad"
else
if _length > 20 then
return text of _artist #& "..." -- & _time
if text of result is _artist then
return text of _name
else
return _full_string -- & " - " & _time
end if
end if
end if
else
return ""
end if
end tell
else
return ""
end if
答案 0 :(得分:1)
我有以下工作需要帮助。试试吧:
if application "Spotify" is running then
tell application "Spotify"
if player state is playing then
return (get artist of current track) & " - " & (get name of current track)
else
return ""
end if
end tell
end if
if application "Music" is running then
tell application "Music"
if player state is playing then
return (get artist of current track) & " - " & (get name of current track)
else
return ""
end if
end tell
end if
return ""
答案 1 :(得分:0)
我冒昧地重新使用了AppleScript。一切都在那里,真的。你刚刚拥有过多的if...then...else
块,让事情变得混乱,而这就是你忘记了return
条款的去向(无论如何都是其中之一)。
property _artist : missing value
property _name : missing value
property show_artist : true
tell application "Spotify"
if it is not running then return ""
if player state is not playing then return ""
-- Gets the text of the current track playing
set [_artist, _name] to [artist, name] of current track
set _full_string to [_artist, " - ", _name] as text
-- No track or artist name or a value of "Listen Now"
-- probably means an advertisement is playing
if (_name = "") or (_artist = "") or ¬
(_name = "Listen Now") then ¬
return "Ad"
-- Can return the full string if 20 or fewer characters
if (length of _full_string) < 21 then ¬
return _full_string
-- Otherwise, have to split up the artist and track name
-- and alternate between which one is returned with each
-- script execution (using the show_artist flag)
if show_artist then
set show_artist to not show_artist
if (length of _artist) < 21 then return _artist
return text 1 thru 17 of _artist & "..."
else -- show_artist is false
set show_artist to not show_artist
if (length of _name) < 21 then return _name
return text 1 thru 17 of _name & "..."
end if
end tell
-- This part of the script will never be reached
-- so this superfluous return clause is just acting
-- as a safety net. Delete it if you like.
return ""
请注意,我只使用了一个if...then...else
语句,这使得它更有效,更容易阅读。我通过考虑哪些条件会导致脚本提前退出来管理这一点,请记住,这意味着脚本的其余部分将不会被执行。因此,在这些情况下,单个if...then...return
行将会执行,我会首先处理这些行。
我做的唯一另一件事就是增加了脚本能够跟踪艺术家或曲目名称是否在之前的剧本运行中被返回的能力(因此这次返回另一个)。
这是使用property
的{{1}}声明(一个布尔值)来实现的,我只是在返回艺术家或曲目名称之前否定。由于在脚本运行之间记住属性值(并且只有在重新编译脚本时才重置),它将在show_artist
和true
之间不断交替,这将确定返回两个变量中的哪一个。
试一试。将歌曲粘贴到脚本编辑器中并播放一首歌曲(其组合文本长度超过20个字符),并继续按Cmd + R执行脚本。