在python脚本和applescrip之间传递和接收值

2024-05-15 22:18:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python和applescript非常陌生。我有一个python脚本,它调用2个applescripts。我想在python中定义几个全局变量并传递给applescript 1,这些值将由applescripts 1中的不同函数修改,然后传递回python脚本,然后将这些值传递给applescript 2使用。在

我在谷歌上搜索了一下,尝试了以下方法:

在applescript中

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脚本中 导入操作系统 导入系统

^{pr2}$

在applescript2中,我做了与applescript1相似的事情。在

但是,这没用。我试图打印出两个脚本中的所有argv,看起来值没有正确传递。有人能给我更多的指示吗?谢谢!在


Tags: oftorun脚本定义onvarwith
2条回答

os.system():在子shell中执行命令(字符串)。这是通过调用标准的C函数system()来实现的,并且有相同的限制。在

{cd2>对环境的更改不会反映到^中。 返回值是退出状态,而不是osascript输出。在

使用子流程.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

osascript命令始终返回string。 如果AppleScript返回list,python中的字符串将用逗号和空格分隔。在

您必须从applescript中的“运行中”处理程序返回一些内容,否则返回的结果只是最后一行代码的结果。所以你会想做这样的事。。。在

on run argv
    set returnList to {}

    if (item 1 of argv starts with "x") then
        set end of returnList to function1(item1 of argv)
    else
        set end of returnList to function2(item 1 of argv)
        set end of returnList to function3(item 2 of argv)
    end if

    return returnList
end run

另外,如果你想让用户提供一些东西,你的函数需要看起来像这样。注意,我告诉Finder显示对话框。这是因为您是从python运行的,如果某个应用程序不能处理用户交互,它就会出错。在

^{2}$

相关问题 更多 >