使用“适应屏幕"更改 macOS 背景图片python中的参数 [英] Change macOS background picture with "adapt to screen" parameter in python

查看:24
本文介绍了使用“适应屏幕"更改 macOS 背景图片python中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 macOS 上使用 python 更改我的墙纸.使用此代码:

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.

推荐答案

我已经有一段时间没有使用 python 了,但我想我已经拼凑出了一些东西.它使用 PyObjC.PyObjC 与最新的操作系统版本一起安装,但如果您落后于一些修订版本,您可能需要自己安装它.基本上这个脚本使用 NSImageScaleProportionallyUpOrDown 选项调用 NSWorkspace 的 setDesktopImageURL:forScreen:options:error: 方法,并将深灰色填充设置为边框.

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 the NSImageScaleProportionallyUpOrDown option, and sets a dark gray color fill as a border.

我不知道获得 imagePath 的最佳方法 — 抱歉,我的 Python 生锈了,我使用了硬编码值进行测试 — 但它应该是 POSIX 文件路径.

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.

from AppKit import NSWorkspace
from AppKit import NSScreen
from AppKit import NSColor

from AppKit import NSWorkspaceDesktopImageScalingKey
from AppKit import NSWorkspaceDesktopImageFillColorKey
from AppKit import NSImageScaleProportionallyUpOrDown

from Foundation import NSURL
from Foundation import NSDictionary

imageURL = NSURL.fileURLWithPath_(imagePath);
sharedSpace = NSWorkspace.sharedWorkspace();
mainScreen = NSScreen.mainScreen();
fillColor = NSColor.darkGrayColor();

optDict = NSDictionary.dictionaryWithObjects_forKeys_([NSImageScaleProportionallyUpOrDown, fillColor], [NSWorkspaceDesktopImageScalingKey,NSWorkspaceDesktopImageFillColorKey]);

sharedSpace.setDesktopImageURL_forScreen_options_error_(imageURL, mainScreen, optDict, None);

可用的缩放选项有:

  • NSImageScaleNone(不调整图片大小)
  • NSImageScaleAxesIndependently(填充屏幕,根据需要拉伸)
  • NSImageScaleProportionallyUpOrDown(在一个轴上适合屏幕,保持纵横比)

如果您需要为辅助显示器设置壁纸,请参阅 NSScreen,以及NSColor 如果你想填充不同的颜色(必须是预定义颜色之一,或使用 RGB 的自定义颜色).

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).

如果你在完成这项工作时遇到困难,或者有特殊顾虑(比如你需要将它发送到可能没有安装 PyObjC 的计算机上),这些相同的 NSWorkspace 调用可以用 AppleScript 编写,然后 AppleScript 可以从 python 运行使用 osascript.让我知道这是一个更好的解决方案.

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.

这篇关于使用“适应屏幕"更改 macOS 背景图片python中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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