问题描述
我在脚本编辑器中编写了这段代码。程序循环一两次,但很快就停止了。该代码的目的是在受害者的Mac电脑上无限播放“My heart will go on”。相反,它只循环两次并且很容易中断。我怎样才能做到这一点?
repeat
set volume 10
try
using terms from application "Spotify"
if player state of application "Spotify" is paused then
tell application "Spotify" to play
return "unpaused"
end if
end using terms from
tell application "System Events" to (name of processes) contains "Spotify"
if true then
tell application "Spotify"
play track "spotify:track:33LC84JgLvK2KuW43MfaNq" in context "spotify:playlist:2CLbqnKJirVefl8xdtMvEN"
end tell
else
open application "Spotify"
end if
end try
end repeat
解决方法
有人告诉我,您所指的“受害者的 Mac”属于您的一位女性朋友。 (顺便说一句……你可能需要考虑编辑你的问题并重新表述“受害者”)
如果是这种情况,本着“Guy Code”和“Bro's Before Ho's”的精神,我重新修改了您的代码。这应该会为您提供所需的结果。
我让它更友好一点,这样当代码启动时,它会激活 Spotify 并在 repeat
循环中继续运行命令,只有在 Spotify 运行时。它不会继续重新激活 Spotify。如果用户退出 Spotify,AppleScript 也会停止。
这个 AppleScript 代码适用于我使用最新版本的 macOS Big Sur。
use Spotify : application "Spotify" with importing
use scripting additions
if Spotify is not running then launch Spotify
repeat while Spotify is running
set volume 10 -- System Volume
try
set sound volume to 100 -- Spotify Volume
if Spotify is running and Spotify's player state is in {paused,stopped} ¬
and Spotify's current track's spotify url ¬
is in "spotify:track:33LC84JgLvK2KuW43MfaNq" then
play
else if Spotify is running and Spotify's current track's spotify url is not in ¬
"spotify:track:33LC84JgLvK2KuW43MfaNq" then
play track "spotify:track:33LC84JgLvK2KuW43MfaNq" in context ¬
"spotify:playlist:2CLbqnKJirVefl8xdtMvEN"
end if
delay 5
end try
end repeat