我对 python 和 applescript 很陌生。我有一个 python 脚本正在调用 2 个 applescripts。我想在 python 中定义一些全局变量并传递给 applescript 1 ,这些值将由 applescripts 1 中的不同函数修改,并传递回 python 脚本,然后将这些值传递给 applescript 2 使用。
我用谷歌搜索了一下,并尝试了以下方法:
在苹果脚本中,
on run argv
if (item 1 of argv is start with "x") then
function1(item1 of argv)
else
function2 (item 1 of argv)
function3 (item 2 of argv)
end run
on function1 (var)
set var to (something code to get value from interaction with user)
return var
end function1
在Python脚本中
导入操作系统
导入系统
os.system ('osascript Setup.scpt')
variable1 = sys.argv[1]
variable2 = sys.argv[2]
在applescript2中,我做了与applescirpt1类似的事情。
然而,这并没有奏效。我尝试打印出两个脚本中的所有 argv,看起来值未正确传递。谁能给我更多的指导吗?谢谢!
os.system()
:在子 shell 中执行命令(字符串)。这是通过调用标准 C 函数 system() 来实现的,并且具有相同的限制。
更改为sys.stdin, sys.stout
不反映在执行命令的环境中。
返回值是退出状态,而不是 osascript 输出。
Use 子进程.Popen:
import os, sys, commands
from subprocess import Popen, PIPE
var1 = sys.argv[1]
var2 = sys.argv[2]
(var3, tError) = Popen(['osascript', '/Setup.scpt', var1, var2], stdout=PIPE).communicate()
print var1
print var2
print var3
The osascript
命令总是返回一个string
。
如果 AppleScript 返回一个list
,python中的字符串会用逗号和空格分隔。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)