如何获取AppleScript中的桌面列表

时间:2013-09-09 19:18:05

标签: applescript wallpaper multiple-monitors

我正在尝试制作一个AppleScript,让我将桌面图片更改为硬盘上文件夹中的随机图片

tell application "Finder"
    set desktopPictures to folder "Path:To:Desktop:Pictures:"
set fileList to name of every file of desktopPictures
set theNum to random number from 1 to (count fileList) with seed ((time of (current date)) * 4)
set fileName to item theNum of fileList
set desktop picture to file fileName in desktopPictures
end tell

到目前为止它完美运行,我唯一的问题是当我连接另一台显示器时,他的桌面图片不会改变。 我尝试使用the following code I found making a web search

解决此问题
tell application "Finder"
    set desktopPictures to folder "Path:To:Desktop:Pictures:"
    set fileList to name of every file of desktopPictures
    set theDesktops to a reference to every desktop 
    repeat with aDesktop in theDesktops
        set theNum to random number from 1 to (count fileList) with seed ((time of (current date)) * 4)
        set fileName to item theNum of fileList
        set picture of aDesktop to file fileName in desktopPictures
    end repeat
end tell

但是这段代码无法编译,因为我收到语法错误:

Expected class name but found property. 第4行突出显示desktop

1 个答案:

答案 0 :(得分:1)

您已从找到的代码中省略了tell应用程序“System Events”块。

在Applescript中,某些命令存在于特定应用程序的字典中,必须使用“tell application”块进行引用。在这种情况下,“每个桌面”调用都在“系统事件”应用程序中。

试试这个。

tell application "Finder"
    set desktopPictures to folder "Path:To:Desktop:Pictures:"
    set fileList to name of every file of desktopPictures
    tell application "System Events"
        set theDesktops to a reference to every desktop
    end tell
    repeat with aDesktop in theDesktops
        set theNum to random number from 1 to (count fileList) with seed ((time of (current date)) * 4)
        set fileName to item theNum of fileList
        set picture of aDesktop to file fileName in desktopPictures
    end repeat
end tell