提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
Mac OS下 使用 osascript 指令,执行完可执行程序后,关闭当前已结束的终端(Terminal)窗口
最近在工作中需要使用shell 脚本的可执行程序,在shell里调用完成一些其他功能。Mac os下 执行的时候是调用终端(Terminal)。执行完成后会有一个已结束终端进程界面,一直在桌面显示,每次都需要手动关闭就很麻烦。
osascript是mac上执行applescript的命令。
applescript脚本可以用来控制声音、亮度、发送通知等功能。
1.示例
如下会在终端(Terminal)输出Hello World,进程执行完成后,并不会关闭终端窗口
echo $'Hello'echo $'World!'
2. 使用 osascript
代码如下(示例):
THIS_FILE=`basename "$0"`echo $THIS_FILEecho $'Hello'echo $'World!'sleep 5osascript -e 'tell application "Terminal" to close (every window whose name contains "'"$THIS_FILE"'")' &exit
`basename "$0"` 是获取 获取执行当前可执行文件的 文件名称osascript -e 参数'tell application "Terminal" to close 。是 在Mac os里 application 里 找到 "Terminal" 应用程序并关闭它(every window whose name contains "'"$THIS_FILE"'")' 。这里通过 前面获取的 文件名称,在Terminal 程序的所有窗口下查找 当前文件名的窗口,然后关闭它。
3. osascript 其他应用
不止有 to close
sudo osascript -e 'tell app "[name of an open program]" to quit'sudo osascript -e 'tell app "Finder" to sleep'sudo osascript -e 'tell app "Finder" to shut down'sudo osascript -e "set volume 0"sudo osascript -e "beep"sudo osascript -e 'display dialog "Did you know that you are annoying?" buttons "Yes" with icon note'sudo osascript -e 'tell app "Finder" to quit'sudo osascript -e 'say "[whatever]" using "Zarvox"'iTunes Control:sudo open /Applications/iTunes.app; sudo osascript -e 'say "Play some music. Go on. I dare you." using "Zarvox"'sudo osascript -e 'tell app "iTunes" to stop' -e 'say "Please stop playing your annoying music" using "Zarvox"'sudo osascript -e 'tell app "iTunes" to next track' -e 'say "I did not like that song very much" using "Zarvox"'sudo osascript -e 'tell app "iTunes" to fast forward' -e 'say "This song is boring" using "Zarvox"'sudo osascript -e 'tell app "iTunes" to quit'
来源地址:https://blog.csdn.net/qq_45559174/article/details/130380344