我如何在Mac OS X中以编程的方式更改桌面背景? 我想使用Python,但我对任何可能的方法都感兴趣。 我能连接到终端并调用特定命令吗?
如果你从Python中安装了appscript(sudo easy_install appscript
),你可以轻松执行以下操作:
from appscript import app, mactypes
app('Finder').desktop_picture.set(mactypes.File('/your/filename.jpg'))
否则,这个 applescript 将会改变桌面背景。tell application "Finder"
set desktop picture to POSIX file "/your/filename.jpg"
end tell
您可以使用osascript
从命令行运行它,或者使用类似以下的Python代码:
import subprocess
SCRIPT = """/usr/bin/osascript<<END
tell application "Finder"
set desktop picture to POSIX file "%s"
end tell
END"""
def set_desktop_background(filename):
subprocess.Popen(SCRIPT%filename, shell=True)
tell application "Finder" to set desktop picture to POSIX file "/your/filename.jpg"
,您可以在终端中使用以下命令运行它:osascript -e 'tell application "Finder" to set desktop picture to POSIX file "/your/filename.jpg"'
。 - user137369如果您正在为当前用户执行此操作,您可以在 shell 中运行:
defaults write com.apple.desktop Background '{default = {ImageFilePath = "/Library/Desktop Pictures/Black & White/Lightning.jpg"; };}'
或者,以 root 身份,为另一个用户执行:
/usr/bin/defaults write /Users/joeuser/Library/Preferences/com.apple.desktop Background '{default = {ImageFilePath = "/Library/Desktop Pictures/Black & White/Lightning.jpg"; };}'
chown joeuser /Users/joeuser/Library/Preferences/com.apple.desktop.plist
你当然会想要替换图像文件名和用户名。
新设置将在Dock启动时生效,无论是在登录时还是在你...
killall Dock
[基于其他帖子的发帖内容,并根据Matt Miller的回答的信息。]
我有一个相同的问题,但是我想要更改所有附加的显示器的壁纸。这里是一个使用appscript
(上面提到的;sudo easy_install appscript
)完成此操作的Python脚本。
#!/usr/bin/python
from appscript import *
import argparse
def __main__():
parser = argparse.ArgumentParser(description='Set desktop wallpaper.')
parser.add_argument('file', type=file, help='File to use as wallpaper.')
args = parser.parse_args()
f = args.file
se = app('System Events')
desktops = se.desktops.display_name.get()
for d in desktops:
desk = se.desktops[its.display_name == d]
desk.picture.set(mactypes.File(f.name))
__main__()
在 dF.的答案 基础上,您可以使用Apple Script而无需使用Finder,并且可以将其应用于多个桌面。
要设置桌面i 的壁纸(桌面编号从1开始):
tell application "System Events"
set currDesktop to item i of desktop
set currDesktop's picture to "image_path"
end tell
SET_DESKTOP_IMAGE_WRAPPER = """/usr/bin/osascript<<END
tell application "System Events"
{}
end tell
END"""
SET_DESKTOP_IMAGE = """
set currDesktop to item {idx} of desktops
set currDesktop's picture to "{image_path}"
"""
def set_wallpapers(images):
""" images is an array of file paths of desktops """
script_contents = ""
for i, img in enumerate(images):
idx = i+1
script_contents += SET_DESKTOP_IMAGE.format(idx=idx, image_path=img)
script = SET_DESKTOP_IMAGE_WRAPPER.format(script_contents)
subprocess.check_call(script, shell=True)
subprocess.check_call("killall Dock", shell=True)
tell application "System Events"
get the number of desktops
end tell
你可以使用 subprocess.check_output
来获取输出结果。
osascript -e 'tell application "Finder" to set desktop picture to POSIX file "/Library/Desktop Pictures/Earth Horizon.jpg"'
把以下内容放在你的~/.bashrc中:
function wallpaper() {
wallpaper_script="tell application \"Finder\" to set desktop picture to POSIX file \"$HOME/$1\""
osascript -e $wallpaper_script
}
使用方式如下:
wallpaper /path/to/image.png
killall Dock
命令。请创建一个名为chwall.swift
的文件,并将以下代码复制到其中:#!/usr/bin/env swift
import Cocoa
do {
// get the main (currently active) screen
if let screen = NSScreen.main {
// get path to wallpaper file from first command line argument
let url = URL(fileURLWithPath: CommandLine.arguments[1])
// set the desktop wallpaper
try NSWorkspace.shared.setDesktopImageURL(url, for: screen, options: [:])
}
} catch {
print(error)
}
subprocess
运行上述的chwall.swift
文件。import subprocess
subprocess.run(["./chwall.swift", "/path/to/your/wallpaper/file.png"])
默认情况下,有一组高质量图像在以下位置:
/Library/Desktop Pictures/
osascript -e 'tell application "System Events" to set picture of every desktop to "/Library/Desktop Pictures/Solid Colors/Stone.png"'
补充Matt Miller的回答:您可以使用subprocess.call()来执行shell命令,如下所示:
import subprocess
subprocess.call(["defaults", "write", "com.apple.Desktop", "background", ...])