4

I have attempted to use this AppleScript to to cause the Desktop wallpaper to change to the next image in the folder of images that has been selected in System Preferences. It does not work for me in Mojave, the current Desktop wallpaper remains unchanged.

Since I am unfamiliar with AppleScript, it might be that I am doing something wrong. I have copy/pasted the script into the Script Editor and clicked the Run button. I have also saved the script as an application and double clicked it from the Desktop.

Here is the version of the script that I am using:

tell application "System Events"
    tell current desktop
        set currentInterval to get change interval
        set change interval to currentInterval
    end tell
end tell

Any thoughts?

2
  • Why do you need a script ths is an option under Systems Prefrences-> Desktop (pre Mojave )
    – mmmmmm
    Commented Feb 6, 2019 at 12:41
  • @Mark I want to be able to quickly change to the next wallpaper "on demand". I could open system preferences and choose a different wallpaper but that is tedious.
    – Verticon
    Commented Feb 6, 2019 at 13:11

1 Answer 1

2

If you are simply looking to change your desktop wallpaper quickly, this following AppleScript code should do the trick. As it is now, the code is set to only choose the pictures from your desktop pictures folder. To be able to use a different folder for your desktop pictures, you can certainly play around with the code

This works for me using the latest version of macOS Mojave

tell application "System Events"
    set fileRef to a reference to files of desktop pictures folder
    set picPath to path of fileRef
    tell current desktop
        set currentPic to get picture as POSIX file as text
        set theIndex to my indexOfItemInList(currentPic, picPath)
        try
            set picture to item (theIndex + 1) of picPath
        on error errMsg number errNum
            set theIndex to 0
            set picture to item (theIndex + 1) of picPath
        end try
    end tell
end tell

(* THIS HANDLER DETERMINES THE INDEX OF THE CURRENT DESKTOP PICTURE
IN THE LIST OF AVAILABLE DESKTOP PICTURES, TO BE ABLE TO QUICKLY SET
THE NEW DESKTOP PICTURE TO THE NEXT ONE IN THE LIST*)

on indexOfItemInList(theItem, theList)
    script fasterList
        property fastList : theList
    end script
    repeat with i from 1 to length of fasterList's fastList
        try
            if item i of fasterList's fastList = theItem then return i
        on error "Item not found." number -1728 from theItem
            tell application "System Events"
                tell current desktop
                    set picture to item 10 of picPath
                end tell
            end tell
        end try
    end repeat
end indexOfItemInList

You mentioned “The Change Picture Every XXX option needs to be checked in Desktop Preferences. “, in your comment. I'm not quite sure why because the above AppleScript code works on my system without that being checked. However if having that option to be enabled is necessary, this following AppleScript code will do that for you


if application "System Preferences" is running then do shell script "killall 'System Preferences'"
repeat until application "System Preferences" is not running
    delay 0.1
end repeat
tell application "System Preferences"
    reveal anchor "DesktopPref" of pane id "com.apple.preference.desktopscreeneffect"
    activate
end tell
tell application "System Events" to tell application process "System Preferences"
    repeat while not (exists of static text 1 of row 2 of outline 1 of scroll area 1 of tab group 1 of window "Desktop & Screen Saver")
        delay 0.1
    end repeat
    set focused of static text "Desktop Pictures" of row 2 of outline 1 of scroll area 1 of tab group 1 of window "Desktop & Screen Saver" to true
    if (get value of checkbox 1 of tab group 1 of window 1) is 0 then
        click checkbox 1 of tab group 1 of window 1
    end if
end tell
if application "System Preferences" is running then do shell script "killall 'System Preferences'"
3
  • 1
    Thank you! A couple of comments: 1) The Change Picture Every XXX option needs to be checked in Desktop Preferences. 2) From the Script Editor I exported the script as an application. Then, whenever I executed it, MacOS prompted me for authorization. I exported it again, this time signing it using my iPhone developer certificate; now the prompts stopped after the first execution.
    – Verticon
    Commented Feb 7, 2019 at 13:40
  • I added additional code for you. Also... If you really want to make the process of changing your desktop picture quickly, Bring the code into Automator app and create a new service by adding a run AppleScript action to the workflow, insert the code and save it. Then in system preferences you can assign that service a keyboard shortcut. Then anytime you want to quickly change your desktop wallpaper. Just use your new keyboard shortcut
    – wch1zpink
    Commented Feb 8, 2019 at 17:11
  • Thk u, @wchtzpink
    – Verticon
    Commented Feb 11, 2019 at 18:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .