这是一个测试代码:
tell application "Spotify"
set playerState to player state as string
end tell
display dialog playerState
在 AppleScript 编辑器中工作正常。但是,当我将脚本导出为应用程序时,我得到的只是:
为什么会这样?
这是一个测试代码:
tell application "Spotify"
set playerState to player state as string
end tell
display dialog playerState
在 AppleScript 编辑器中工作正常。但是,当我将脚本导出为应用程序时,我得到的只是:
为什么会这样?
似乎 Spotify 并没有将常量强制转换为字符串。由于编辑器无法像在 AppleScript Editor 中运行脚本时那样从 applet 强制它,因此返回四字母常量代码。由于您无法将播放器状态的值作为字符串进行测试,因此请尝试根据常量本身对其进行测试。
property spotPause : «constant ****kPSp»
property spotPlay : «constant ****kPSP»
tell application "Spotify" to set playerState to player state
if playerState = spotPause then
display dialog "paused"
else if playerState = spotPlay then
display dialog "playing"
end if
最好使用此代码来获取播放器状态。您不需要知道确切的常数值。适用于 OS X 10.13
tell application "Spotify"
if player state is playing then
display dialog "Player running"
else if player state is paused then
display dialog "Player paused"
else if player is stopped then
display dialog "Player is stopped"
else
display dialog "Unknow state"
end if
end tell
我无法评论adayzdone的答案,但这里有一个替代方案:
if player state is playing or player state is paused then
例如
if application "Spotify" is running then
tell application "Spotify"
if player state is playing or player state is paused then
set trackID to (get id of current track)
end if
end tell
end if
在我自己的 Spotify-Applescript 项目上工作时,这至少适用于 Catalina (10.15.7)。
这个想法的来源来自这个线程。