繁体   English   中英

异步运行Applescript中的python

[英]Run python in Applescript asynchronously

所以我想要一个 python 脚本在我的 AppleScript 的开头启动,并且在它运行时,我想做一些其他的事情。 是这样的:

set theResult to do shell script "python3 -c \"import time; time.sleep(60); print('finished waiting')\""

repeat with i from 1 to 60
    tell application "System Events" to keystroke (i as text) & ","
    delay 1
end repeat

return theResult 

它当然会比time.sleep()更复杂,这只是为了演示目的。

有没有办法在 python 脚本“加载”时敲击数字?

这是示例,它使用 osascript 并将结果重定向到 /dev/null 进行类似测试:

do shell script "osascript -e '
     repeat 5 times
      delay 2
   display notification \"TEST\"
    end repeat
' &> /dev/null &" -- [end shell script]

delay 1
repeat 5 times
    display notification "OTHER"
    delay 2
end repeat

其他示例 - 将结果重定向到临时文件,并在 shell 脚本终止后从中返回结果:

set tempPath to (POSIX path of (path to temporary items from user domain)) & "result.txt"

do shell script "osascript -e '
     repeat 5 times
      delay 2
   display notification \"TEST\"
    end repeat
return \"Loading finished\"
' &> " & tempPath & " &" -- [end shell script]

delay 1
repeat 5 times
    display notification "OTHER"
    delay 2
end repeat

set theResult to read (tempPath as POSIX file)

使用您问题中的现有代码,尽管经过修改可以工作并使用较少的时间进行测试,但以下工作:

示例AppleScript代码

do shell script "python3 -c \"import time; time.sleep(5); print('finished waiting')\" &>/tmp/result &"

repeat with i from 1 to 5
    tell application "System Events" to keystroke (i as string) & ","
    delay 1
end repeat

return (read "/tmp/result")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM