How to get a list of the desktops in applescript
Asked Answered
Z

2

1

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

Zurkow answered 9/9, 2013 at 19:18 Comment(0)
S
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
Snoddy answered 9/9, 2013 at 21:23 Comment(3)
This doesn't work for me. I only ever get one desktop. I'm on Mac OS X 10.7. Ideas?Echinus
Not sure, perhaps start a new question about it?Snoddy
Actually i think i have a solution if you would like to start a new question.Snoddy
C
0

This works for me in Mojave using 3 monitors with 8 Desktops each. After getting a list of monitors, it uses Mission Control to visit each Desktop and then set it's picture to a random jpeg from the user's Pictures folder. The speech parts can be removed, that's just to inform since it's a demo. The Mission Control toggle using the key command is faster than the shell script, but not everyone has the same keyboard settings so the shell script is set as the default. It starts with a nice warning, since it changes stuff. If you click anything while it's working it just stops.

tell application "System Events" to set MonitorQTY to count of every desktop
set msg to "Your total number of monitors is " & MonitorQTY
say msg without waiting until completion
display dialog msg buttons {"Cancel", "Assign RANDOM wallpaper to each desktop!"} with icon 0
----set MissionControlToggle---- 
set toggleMissionControl to "do shell script \"/Applications/Mission\\\\ Control.app/Contents/MacOS/Mission\\\\ Control\" " & linefeed & "delay 1"--any keyboard
#set toggleMissionControl to "tell application \"System Events\" to (key code 126 using control down)" & linefeed & "delay 1" -- depends function keys
----count desktopspaces----
run script toggleMissionControl --on.
tell application "System Events" to tell application process "Dock" to tell group "Mission Control"
    set DesktopNMBR to 0
    repeat with MonitorNMBR from 1 to MonitorQTY
        set dsktpBTNS to (every UI element of list 1 of group "Spaces Bar" of group MonitorNMBR) -->list of desktops as buttons.
        set dsktpQNTY to count of dsktpBTNS
        say "Monitor " & MonitorNMBR & " has, " & dsktpQNTY & " desktop spaces." without waiting until completion
        repeat with eachDesktopButton in dsktpBTNS
            click eachDesktopButton --causes mission control to close.
            set DesktopNMBR to DesktopNMBR + 1
            say "This is desktop " & DesktopNMBR without waiting until completion
            ----set random wallpaper----
            say "assigning random wallpaper" without waiting until completion
            set rndmWALLPAPER to some item of (paragraphs of (do shell script "mdfind -onlyin ~/Pictures/  '.jpeg' "))
            tell application "System Events" to set picture of desktop MonitorNMBR to rndmWALLPAPER
            -----------------------------
            run script toggleMissionControl --on again.
        end repeat
    end repeat
    say "Your total number of desktops is, " & DesktopNMBR without waiting until completion
end tell
----exit----
run script toggleMissionControl --off
say "Process complete."
Capp answered 30/10, 2023 at 20:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.