bash Applescript——执行多行代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32156678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Applescript–execute multi line code
提问by Cplusplusplus
I have some apple script code:
我有一些苹果脚本代码:
tell application "System Events"
key code 97
end tell
How do i write the code as a osascript -e
command in Terminal?
Everytime I try using \n
or the such, I get errors. Sorry if I'm not being specific enough.
如何osascript -e
在终端中将代码编写为命令?每次我尝试使用\n
或类似的方法时,都会出错。对不起,如果我不够具体。
回答by foo
You have a couple of options:
您有几个选择:
Pass each line of the AppleScript code as a separate -e option:
osascript -e 'tell application "System Events"' -e 'key code 97' -e 'end tell'
Pipe the AppleScript code to osascript's STDIN:
osascript <<END tell application "System Events" key code 97 end tell END
将 AppleScript 代码的每一行作为单独的 -e 选项传递:
osascript -e 'tell application "System Events"' -e 'key code 97' -e 'end tell'
将 AppleScript 代码通过管道传送到 osascript 的 STDIN:
osascript <<END tell application "System Events" key code 97 end tell END
Oh, and you can also save AppleScript code as an executable shell script. Just add #!/usr/bin/osascript
at the top of the code and save it as a plain text file:
哦,您还可以将 AppleScript 代码保存为可执行的 shell 脚本。只需#!/usr/bin/osascript
在代码顶部添加并保存为纯文本文件:
#!/usr/bin/osascript
tell application "System Events"
key code 97
end tell
回答by Gongora
Other example:
其他例子:
open -a Terminal && \
sleep 2 && \
osascript -e 'activate application "Terminal"' -e 'tell application "System Events" to keystroke "q" using command down'
the first two lines are just to show the final goal, which is focus the Terminal window and quit it, sending Command+q
前两行只是为了显示最终目标,即聚焦终端窗口并退出它,发送 Command+q
回答by mixel
Actually -e
option accepts new lines:
实际上-e
选项接受新行:
osascript -e '
tell application "System Events"
key code 97
end tell'