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

问题描述 投票:30回答:9

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

python image macos
9个回答
38
投票

从python,如果你安装了appscriptsudo 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)

21
投票

如果您正在为当前用户执行此操作,则可以从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

[基于a posting elsewhere,并基于来自Matt Miller's answer的信息。]


12
投票

我有同样的问题,除了我想要更改所有连接的显示器上的壁纸。这是一个使用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__()

4
投票

你可以调用“defaults write com.apple.Desktop Background ...”,如本文所述:http://thingsthatwork.net/index.php/2008/02/07/fun-with-os-x-defaults-and-launchd/

本文还将脚本编写为自动运行,但第一点可以让你开始。

您可能还对默认手册页:http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/defaults.1.html感兴趣


3
投票

小牛队的一线解决方案是:

osascript -e 'tell application "Finder" to set desktop picture to POSIX file "/Library/Desktop Pictures/Earth Horizon.jpg"'

3
投票

dF.'s answer的基础上,您可以使用没有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

这就是我最终做的(在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)

有时,桌面图像不会立即显示。我不知道为什么会这样,但重启码头会修复它。要从python执行此操作:

subprocess.check_call("killall Dock", shell=True)

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

tell application "System Events"
    get the number of desktops
end tell

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


1
投票

要添加到Matt Miller's response:您可以使用subprocess.call()执行shell命令,如下所示:

import subprocess
subprocess.call(["defaults", "write", "com.apple.Desktop", "background", ...])

0
投票

您也可以使用py-appscript代替Popening osascript或使用ScriptingBridge和pyobjc,它包含在10.5中,但使用起来有点麻烦。


0
投票

以编程方式更改桌面墙纸的另一种方法是简单地将壁纸设置指向文件。使用您的程序用新设计覆盖文件,然后重新启动dock:killall Dock

以下内容取决于Xcode,lynx和wget,但这里是我如何自动下载并安装Mountain Lion上的月度壁纸(无耻地被盗和改编自http://ubuntuforums.org/showthread.php?t=1409827):

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/local/bin
size=1440
dest="/usr/local/share/backgrounds/wallpaperEAA.jpg"
read -r baseurl < <(lynx -nonumbers -listonly -dump 'http://www.eaa.org/en/eaa/aviation-education-and-resources/airplane-desktop-wallpaper' | grep $size) &&
wget -q "$baseurl" -O "$dest"
killall Dock

将它转移到/etc/periodic/monthly/和宝贝,你炖了!

© www.soinside.com 2019 - 2024. All rights reserved.