如何在MacOS上使用Python运行Osascript?

发布于 2025-01-28 12:34:41 字数 183 浏览 3 评论 0原文

如何使用osascript或MacOS上的任何其他方法在系统首选项中添加登录项目, 这个过程对吗? subprocess.call(“ osascript -e将登录项目添加到系统首选项{路径:“/home”; value; value; value; nidend} en end,shell = true')。

How can you add login item in system preferences with python using osascript or any other method on macos,
Is this process right?
subprocess.call("osascript -e add login items to System Preferences {Path: "/home"; value: hidden} at end, shell=True").

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守不住的情 2025-02-04 12:34:41

是的,您可以将osascript python 一起使用,但是您需要对两者使用适当的命令语法。我不确定您在哪里提出了该脚本,但是可以在应用程序的脚本词典中找到AppleScript项(如果有一个),在这种情况下,登录项目suite suite suite system事件

由于AppleScript仅使用双引号,因此使用subprocess.call使用参数的字符串列表可以避免很多报价逃脱,并且会避免以下内容:

import subprocess

def add_login_item(item_name, item_path):
   script = ('tell application "System Events" to make new login item at end of login items with properties {name:"%s", path:"%s", hidden:false}' % (item_name, item_path))
   subprocess.call(['osascript', '-e', script])

add_login_item('Item Name', '/full/path/to/whatever.app')

Yes, you can use osascript with Python, but you will need to use the proper command syntaxes for both. I'm not sure where you came up with that script, but AppleScript terms can be found in an application's scripting dictionary (if it has one), in this case the Login Items Suite from System Events.

Since AppleScript only uses double quotes, using subprocess.call with a list of strings for the arguments can avoid a lot of quote escaping, and would be something like :

import subprocess

def add_login_item(item_name, item_path):
   script = ('tell application "System Events" to make new login item at end of login items with properties {name:"%s", path:"%s", hidden:false}' % (item_name, item_path))
   subprocess.call(['osascript', '-e', script])

add_login_item('Item Name', '/full/path/to/whatever.app')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文