How to get a list of the desktops in applescript

删除回忆录丶 提交于 2019-12-11 07:52:08

问题


I'm trying to make an applescript that let's me change the desktop picture to a random picture in a folder on my hard drive

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

So far it works perfectly, the only issue I have is when I connect another monitor, his desktop picture won't change. I tried solving this problem with 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

But this code won't compile as I get a syntax error saying:

Expected class name but found property. With desktop highlighted on row 4


回答1:


You have omitted the tell application "System Events" block from the code you found.

In Applescript some commands exist in the dictionary of specific applications and must be referenced with a 'tell application' block. In this case the 'every desktop' call is in the "System Events" app.

Try this.

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


来源:https://stackoverflow.com/questions/18705359/how-to-get-a-list-of-the-desktops-in-applescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!