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

查看:114
本文介绍了使用“适应屏幕"更改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随最新的OS版本一起安装,但是如果您有几个修订版本,则可能需要自己安装.基本上,此脚本使用 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(不调整图像大小)
  • NSImageScaleAxes独立地(填充屏幕,根据需要拉伸)
  • 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的计算机上),则可以使用AppleScript编写这些相同的NSWorkspace调用,然后可以从python运行AppleScript使用 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天全站免登陆