2016-05-26 30 views
1

如何防止Apple Script应用程序在计算机注销或进入睡眠时崩溃?如何防止应用程序在Apple脚本中显示进入睡眠状态时崩溃

我目前正在研究将桌面背景设置为当前歌曲在iTunes中播放的专辑封面的应用程序,但是如果我将显示器设置为睡眠状态,iTunes将继续播放音乐,但我的应用程序会崩溃,这意味着当我返回到计算机时桌面背景不再更新。

这是我使用的设置壁纸代码:

 tell desktop 2 
      set picture to fileName 
     end tell 

其中FileName为当前歌曲,已提取并保存到桌面上的iTunes的作品的名字。

它说桌面2,因为我有两个不同的应用程序为每个屏幕,并在我的第二个屏幕上进行测试。

这是在Automator中运行,并把显示器睡觉时,我得到的错误:

The action “Run AppleScript” encountered an error.
System Events got an error: Can’t get desktop 2. Invalid index.

我得到我的主显示屏上显示了同样的结果也。

当显示屏处于睡眠状态时,如何停止此脚本遇到错误?

任何建议将不胜感激,因为我喜欢任何人谁想要它有这个应用程序。

回答

0

最安全的方法是,以检查是否该项存在

if exists desktop 2 then 
    tell desktop 2 
    set picture to fileName 
    end tell 
end if 

或在try块中忽略任何错误包的代码。

try 
    tell desktop 2 
    set picture to fileName 
    end tell 
end try 
+0

第一个选项完美运作。 –

相关问题