Itunes Applescript活动

时间:2011-07-08 22:54:19

标签: applescript itunes polling

我正在编写一个自动点唱机应用程序,它使用php / js作为前端,并使用itunes作为后端。问题是我需要一种方法来判断一首歌何时停止播放。我曾想过使用一个空闲脚本通过applescript来调查itunes。但是,我必须每隔几秒钟进行一次轮询,相反,当歌曲停止播放时我想要一个事件来运行一个AppleScript。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

我不完全确定是否存在允许您这样做的方法,但是现在您可以始终使用iTunes'player state属性,该属性通过返回以下内容之一来告诉您当前iTunes正在做什么五个值:

playing, stopped, paused, fast forwarding, rewinding

使用该属性,您可以创建一个repeat until player state is stopped循环,其中没有任何命令(实质上,等到当前正在播放的歌曲停止),然后在循环之后,执行您想要的任何内容。翻译成代码,本段内容如下:

tell application "iTunes"
   repeat until player state is stopped
      --do nothing until the song currently playing is stopped...
   end repeat
   --[1]...and then execute whatever you want here
end tell

OPTIONAL

如果您只想运行一次脚本,可以将上述脚本插入无限repeat循环中,尽管您可能需要先delay来启动歌曲。否则,[1]将在您启动脚本后立即执行(假设没有使用歌曲)。

代码

repeat
  delay 60 --1 minute delay
  tell application "iTunes"
     repeat until player state is stopped
         --wait
     end repeat
     ...
  end tell
end repeat

如果您有任何疑问,请询问。 :)

答案 1 :(得分:0)

只要名为“com.apple.itunes.playerInfo”的状态发生变化,iTunes就会发出系统范围的通知。因此,如果您可以从php注册系统通知(NSDistributedNotificationCenter),那么这将是一种方式而不是轮询。快速搜索了如何从python ... here