如何以编程方式更改Mac OS X中的背景? [英] How can I programmatically change the background in Mac OS X?

查看:143
本文介绍了如何以编程方式更改Mac OS X中的背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How would I go about programmatically changing the desktop background in Mac OS X? I'd like to use python, but I'm interested in any way possible. Could I hook up to Terminal and call a certain command?

推荐答案

从python中,如果你有 appscript sudo easy_install appscript ),你可以简单地做到

From python, if you have appscript installed (sudo easy_install appscript), you can simply do

from appscript import app, mactypes
app('Finder').desktop_picture.set(mactypes.File('/your/filename.jpg'))

否则,这个AppleScript将改变桌面背景

Otherwise, this applescript will change the desktop background

tell application "Finder"
    set desktop picture to POSIX file "/your/filename.jpg"
end tell

您可以使用 osascript ,或使用类似

You can run it from the command line using osascript, or from Python using something like

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)

这篇关于如何以编程方式更改Mac OS X中的背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆