简体   繁体   中英

How can I run osascript with python on MacOS?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM