简体   繁体   中英

How do I pass applescript arguments from a python script?

I have an applescript that takes in two parameters on execution.

on run {targetBuddyPhone, targetMessage}
    tell application "Messages"
        set targetService to 1st service whose service type = iMessage
        set targetBuddy to buddy targetBuddyPhone of targetService
        send targetMessage to targetBuddy
    end tell
end run

I then want this script to execute from within a python script. I know how to execute a applescript from python, but how do I also give it arguments? Here is the python script that I currently have written out.

#!/usr/bin/env python3
import subprocess

def run_applescript(script, *args):
    p = subprocess.Popen(['arch', '-i386', 'osascript', '-e', script] +
                         [unicode(arg).encode('utf8') for arg in args],
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    err = p.wait()
    if err:
        raise RuntimeError(err, p.stderr.read()[:-1].decode('utf8'))
    return p.stdout.read()[:-1].decode('utf8')

The error I receive after trying to execute this code in the terminal is:

Traceback (most recent call last):
  File "messageExecuter.py", line 14, in <module>
    run_applescript("sendMessage.scpt",1111111111,"hello")
  File "messageExecuter.py", line 11, in run_applescript
    raise RuntimeError(err, p.stderr.read()[:-1].decode('utf8'))
RuntimeError: (1, u'arch: posix_spawnp: osascript: Bad CPU type in executable')

Clue is in the error message. Delete 'arch', '-i386' from arguments list, as osascript is 64-bit only.

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