如何以编程方式更改Mac OS X中的背景?

sta*_*zel 32 python macos image

我将如何以编程方式更改Mac OS X中的桌面背景?我想使用python,但我对任何可能的方式感兴趣.我可以连接到终端并调用某个命令吗?

dF.*_*dF. 39

从python,如果你有appscript安装(sudo easy_install appscript),你可以做到

from appscript import app, mactypes
app('Finder').desktop_picture.set(mactypes.File('/your/filename.jpg'))
Run Code Online (Sandbox Code Playgroud)

否则,此applescript将更改桌面背景

tell application "Finder"
    set desktop picture to POSIX file "/your/filename.jpg"
end tell
Run Code Online (Sandbox Code Playgroud)

您可以使用命令行从命令行运行它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)
Run Code Online (Sandbox Code Playgroud)

  • 另请注意,它只会影响具有菜单栏的显示器的桌面,而不会影响其他显示器上的桌面.有没有办法让它影响其他桌面? (4认同)
  • AppleScript可以简化为一行,如"告诉应用程序"Finder"将桌面图片设置为POSIX文件"/your/filename.jpg"`,您可以使用`osascript -e'告诉应用程序"Finder"从终端运行它"将桌面图片设置为POSIX文件"/your/filename.jpg"'`. (2认同)

Cli*_*ore 21

如果您正在为当前用户执行此操作,则可以从shell运行:

defaults write com.apple.desktop Background '{default = {ImageFilePath = "/Library/Desktop Pictures/Black & White/Lightning.jpg"; };}'
Run Code Online (Sandbox Code Playgroud)

或者,作为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
Run Code Online (Sandbox Code Playgroud)

您当然希望替换图像文件名和用户名.

当Dock启动时,新设置将生效 - 无论是在登录时,还是在您启动时

killall Dock
Run Code Online (Sandbox Code Playgroud)

[基于其他地方的帖子,并根据Matt Miller的答案提供的信息.]


Gle*_*iet 12

我有同样的问题,除了我想要更改所有连接的显示器上的壁纸.这是一个Python脚本使用appscript(如上所述; sudo easy_install appscript)就是这样做的.

#!/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__()
Run Code Online (Sandbox Code Playgroud)


小智 5

基于dF. 的答案,您可以在没有 Finder 的情况下使用 Apple Script 进行操作,并且可以为多个桌面进行操作。

设置桌面壁纸i(桌面编号从 1 开始):

tell application "System Events"
    set currDesktop to item i of desktop
    set currDesktop's picture to "image_path"
end tell
Run Code Online (Sandbox Code Playgroud)

这就是我最终做的(在 Python 中):

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)
Run Code Online (Sandbox Code Playgroud)

有时,桌面图像不会立即出现。我不知道为什么会发生这种情况,但重新启动 Dock 可以修复它。要从 python 中做到这一点:

subprocess.check_call("killall Dock", shell=True)
Run Code Online (Sandbox Code Playgroud)

顺便说一句,您可以使用以下 AppleScript 代码获取系统上的桌面数量:

tell application "System Events"
    get the number of desktops
end tell
Run Code Online (Sandbox Code Playgroud)

您可以使用它subprocess.check_output来获取输出