运行 python 在 Applescript 中异步
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")
所以我想要一个 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")