Applescript-从python获取返回

Applescript-从python获取返回,python,macos,applescript,Python,Macos,Applescript,我有一个启动python脚本的applescript。 python脚本计算一个整数。 是否可以将结果作为整数或字符串返回到applescript thx您应该能够从applescript运行的python脚本中返回一个值,如下所示: set scriptResult to do shell script ("python path/to/script.py") 其中,scriptResult然后设置为python脚本中提供给stdout的任何输出(例如,来自print语句)。因此,在pyth

我有一个启动python脚本的applescript。 python脚本计算一个整数。 是否可以将结果作为整数或字符串返回到applescript


thx

您应该能够从applescript运行的python脚本中返回一个值,如下所示:

set scriptResult to do shell script ("python path/to/script.py")
其中,scriptResult然后设置为python脚本中提供给stdout的任何输出(例如,来自print语句)。因此,在python脚本中,您可以简单地:

print yourInteger
然后将scriptResult设置为[integer]。请注意,scriptResult将设置为all stdout,因此您打印的所有内容都将包含在这里。您可能还需要在applescript中进行一些转换,以使scriptResult成为一个数字:

set scriptResult to (do shell script ("python path/to/script.py")) as integer

在同一类型中,使用do shell脚本和直接脚本python

set xy to do shell script "echo   'import sys, time; from Quartz.CoreGraphics import *
def mouseEvent(type, posx, posy): theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft); CGEventPost(kCGHIDEventTap, theEvent);
def mouseclickdn(posx,posy): mouseEvent(kCGEventLeftMouseDown, posx,posy);
def mouseclickup(posx,posy): mouseEvent(kCGEventLeftMouseUp, posx,posy);
ourEvent = CGEventCreate(None); currentpos = CGEventGetLocation(ourEvent); print currentpos[0]; print currentpos[1]; mouseclickdn(currentpos.x, currentpos.y);
mouseclickup(currentpos.x, currentpos.y);' | python "

set xy to do shell script "echo " & xy

你能给出代码的例子吗?与OP的特定用例无关,但值得注意的是,
do shell脚本
使用UTF8编码,因此在处理ASCII STR以外的任何东西时,Python代码中可能需要额外的
encode()
/
decode()
futzing。这对我不起作用!:/错误“python:无法打开文件”[/Users/Usr/Desktop/xMail.py]:[Errno 2]没有这样的文件或目录”编号2请关闭括号,这些括号仅用于指示应在脚本中插入内容的位置。