如何从python脚本传递applescript参数?

时间:2017-04-18 23:50:54

标签: python bash python-3.x terminal applescript

我有一个AppleScript,在执行时会有两个参数。

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

然后我希望这个脚本在python脚本中执行。我知道如何从python执行一个AppleScript,但我怎么也给它参数?这是我目前写的python脚本。

#!/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')

尝试在终端中执行此代码后收到的错误是:

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

感谢先进的帮助!

1 个答案:

答案 0 :(得分:1)

线索在错误消息中。从参数列表中删除'arch', '-i386',因为osascript仅为64位。