【问题标题】:How do I pass applescript arguments from a python script?如何从 python 脚本传递 applescript 参数?
【发布时间】:2017-09-14 23:48:50
【问题描述】:

我有一个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')

感谢您的帮助!

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-03
      • 2017-01-22
      • 2016-03-20
      • 1970-01-01
      • 2011-04-26
      • 2012-08-23
      相关资源
      最近更新 更多