macos 如何创建 AppleScript 应用程序来运行一组终端命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3154535/
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
how to create AppleScript app to run a set of terminal commands
提问by Hristo
How would I go about creating an AppleScript command that when I just run the script (or double click it in Finder?), it would run a set of terminal commands? The set of commands completely remove MySQL, and it has become a pain to constantly write them out. The commands are:
我将如何创建一个 AppleScript 命令,当我运行脚本(或在 Finder 中双击它?)时,它会运行一组终端命令?这套命令彻底删除了MySQL,不断的写出来就变成了一件痛苦的事情。命令是:
sudo rm /usr/local/mysql
sudo rm -rf /usr/local/mysql*
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/My*
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL*
sudo rm /etc/my.cnf
There is also another command sudo nano /etc/hostconfig
that opens a file and I need to delete a line from the file, but that seems like it'd be too hard to code, so I guess I can do that by hand. But it would be a huge help to do this automatically with a single script.
还有另一个sudo nano /etc/hostconfig
打开文件的命令,我需要从文件中删除一行,但这似乎很难编码,所以我想我可以手动完成。但是,使用单个脚本自动执行此操作将有很大帮助。
Would it just be a bunch of these commands?
它只是一堆这些命令吗?
do shell script (...)
Thanks, Hristo
谢谢,赫里斯托
回答by Paul R
You don't actually need to use AppleScript for this - just put all the shell commands in a text file and give it a .command
suffix and make sure it's executable (e.g. chmod +x my_script.command
) - this will make it double-clickable in the Finder.
您实际上并不需要为此使用 AppleScript - 只需将所有 shell 命令放在一个文本文件中并给它一个.command
后缀并确保它是可执行的(例如chmod +x my_script.command
) - 这将使其在 Finder 中双击。
回答by indragie
Yes, you would use do shell script
.
是的,你会使用do shell script
.
However, for the commands where you execute as super user (sudo
) you would instead use with administrator privileges
. So, for sudo rm /usr/local/mysql
you'd do:
但是,对于以超级用户 ( sudo
)身份执行的命令,您可以使用with administrator privileges
. 所以,sudo rm /usr/local/mysql
你会这样做:
do shell script "rm /usr/local/mysql" with administrator privileges
If your list of commands is long, then it might just be easier to put all your commands into a single shell script file, and then execute that shell script using do shell script
:
如果您的命令列表很长,那么将所有命令放入一个 shell 脚本文件中,然后使用do shell script
以下命令执行该 shell 脚本可能会更容易:
do shell script "/path/to/shell/script" with administrator privileges
回答by Beejster
I find that .scpt files work best but this is just a preference thing.
我发现 .scpt 文件效果最好,但这只是一种偏好。
Open "Script Editor" and add the following command:
打开“脚本编辑器”并添加以下命令:
sudo rm -rf /usr/local/mysql; sudo rm -rf /usr/local/mysql*; sudo rm -rf /Library/StartupItems/MySQLCOM; sudo rm -rf /Library/PreferencePanes/My*; sudo rm -rf /Library/Receipts/mysql*; sudo rm -rf /Library/Receipts/MySQL*; sudo rm /etc/my.cnf; say job completed successfully" with administrator privileges