I want to change my wallpaper using python on macOs. Using this code :
from appscript import app, mactypes
app('Finder').desktop_picture.set(mactypes.File(file_loc))
which works fine, except it always resets parameter from "adjust to screen" to "centered". I can't find anything on how to add the "adjust to screen" parameter. Thanks for your help.
It's been a while since I've played with python, but I think I got something cobbled together. it uses PyObjC. PyObjC is installed with the most recent OS versions, but you may need to install it yourself if you're behind a few revisions. Basically this script calls NSWorkspace's
setDesktopImageURL:forScreen:options:error:
method with theNSImageScaleProportionallyUpOrDown
option, and sets a dark gray color fill as a border.I don't know the best way to get
imagePath
— sorry, my python is rusty, and I used a hard-coded value for testing — but it should be a POSIX file path.The scaling options available are:
See NSScreen if you need to wallpaper secondary monitors, and NSColor if you want to fill with a different color (has to be one of the predefined colors, or a custom color using RGB).
If you have difficulty making this work, or special concerns (like you need this to ship to computers that may not have PyObjC installed), these same NSWorkspace calls can be written in AppleScript, and then the AppleScript can be run from python using
osascript
. Let me know it that's a preferable solution.