macos 从 OS X Dock 执行 Shell 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/281372/
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
Executing Shell Scripts from the OS X Dock?
提问by Wilco
How do I set up a shell script to execute from the Mac OSX dock? It seems that simply creating a shortcut will open the file in my editor. Is there a flag I need to set somewhere to tell it to run instead of opening it for editing?
如何设置 shell 脚本以从 Mac OSX 扩展坞执行?似乎只需创建一个快捷方式即可在我的编辑器中打开该文件。是否需要在某处设置一个标志来告诉它运行而不是打开它进行编辑?
回答by dbr
You could create a Automator workflow with a single step - "Run Shell Script"
您可以通过一个步骤创建 Automator 工作流程 - “运行 Shell 脚本”
Then File > Save As
, and change the File Format to "Application". When you open the application, it will run the Shell Script step, executing the command, exiting after it completes.
然后File > Save As
,将文件格式更改为“应用程序”。当您打开应用程序时,它将运行 Shell 脚本步骤,执行命令,完成后退出。
The benefit to this is it's reallysimple to do, and you can very easily get user input (say, selecting a bunch of files), then pass it to the input of the shell script (either to stdin, or as arguments).
这样做的好处是它非常简单,您可以非常轻松地获取用户输入(例如,选择一堆文件),然后将其传递给 shell 脚本的输入(或者作为标准输入,或者作为参数)。
(Automator is in your /Applications
folder!)
(Automator 在您的/Applications
文件夹中!)
回答by Facebook Staff are Complicit
If you don't need a Terminal window, you can make any executable file an Application just by moving example.sh
to example.sh.app/Contents/MacOS/example.sh
. You can place the Application in your dock like any other, and execute it with a click.
如果您不需要终端窗口,只需将任何可执行文件移动example.sh
到example.sh.app/Contents/MacOS/example.sh
. 您可以像其他任何应用程序一样将应用程序放在您的 Dock 中,然后单击执行它。
NOTE:the name of the app must exactly match the script name. So it has to be example.sh.app
, not just example.app
.
注意:应用程序的名称必须与脚本名称完全匹配。所以它必须是example.sh.app
,而不仅仅是example.app
。
If you do need to have the terminal window displayed, I don't have a simple solution. You could probably do something with Applescript, but that's not very clean.
如果您确实需要显示终端窗口,我没有简单的解决方案。你可能可以用 Applescript 做一些事情,但这不是很干净。
回答by stiemannkj1
On OSX Mavericks:
在 OSX Mavericks 上:
- Create your shell script.
Make your shell script executable:
chmod +x your-shell-script.sh
Rename your script to have a
.app
suffix:mv your-shell-script.sh your-shell-script.app
- Drag the script to the OSX dock.
Rename your script back to a
.sh
suffix:mv your-shell-script.app your-shell-script.sh
- Right-click the file in Finder, and click the "Get Info" option.
- At the bottom of the window, set the shell script to open with the terminal.
- 创建您的 shell 脚本。
使您的 shell 脚本可执行:
chmod +x your-shell-script.sh
重命名您的脚本以添加
.app
后缀:mv your-shell-script.sh your-shell-script.app
- 将脚本拖到 OSX Dock。
将您的脚本重命名回
.sh
后缀:mv your-shell-script.app your-shell-script.sh
- 在 Finder 中右键单击该文件,然后单击“获取信息”选项。
- 在窗口底部,将 shell 脚本设置为使用终端打开。
Now when you click on the script in the dock, A terminal window will pop up and execute your script.
现在,当您单击 Dock 中的脚本时,将弹出一个终端窗口并执行您的脚本。
Bonus: To get the terminal to close when your script has completed, add exit 0
to the end and change the terminal settings to "close the shell if exited cleanly" like it says to do in this SO answer.
奖励:要在脚本完成后关闭终端,请添加exit 0
到末尾并将终端设置更改为“如果干净退出则关闭外壳”,就像在此 SO answer 中所说的那样。
回答by Jason Tholstrup
I know this is old but in case it is helpful to others:
我知道这是旧的,但以防万一它对其他人有帮助:
If you need to run a script and want the terminal to pop up so you can see the results you can do like Abyss Knight said and change the extension to .command. If you double click on it it will open a terminal window and run.
如果您需要运行脚本并希望弹出终端以便您可以看到结果,您可以像深渊骑士所说的那样并将扩展名更改为 .command。如果您双击它,它将打开一个终端窗口并运行。
I however needed this to run from automator or appleScript. So to get this to open a new terminal the command I ran from "run shell script" was "open myShellScript.command" and it opened in a new terminal.
但是,我需要它来从 automator 或 appleScript 运行。所以为了让它打开一个新终端,我从“运行 shell 脚本”运行的命令是“打开 myShellScript.command”,它在一个新终端中打开。
回答by Jason Coco
As long as your script is executable and doesn't have any extension you can drag it as-is to the right side (Document side) of the Dock and it will run in a terminal window when clicked instead of opening an editor.
只要您的脚本是可执行的并且没有任何扩展名,您就可以将其按原样拖动到 Dock 的右侧(文档侧),单击时它将在终端窗口中运行,而不是打开编辑器。
If you want to have an extension (like foo.sh), you can go to the file info window in Finder and change the default application for that particular script from whatever it is (TextEdit, TextMate, whatever default is set on your computer for .sh files) to Terminal. It will then just execute instead of opening in a text editor. Again, you will have to drag it to the right side of the Dock.
如果您想要一个扩展名(如 foo.sh),您可以转到 Finder 中的文件信息窗口并更改该特定脚本的默认应用程序(TextEdit、TextMate,无论您的计算机上为.sh 文件)到终端。然后它将只执行而不是在文本编辑器中打开。同样,您必须将其拖到 Dock 的右侧。
回答by willasaywhat
I think this thread may be helpful: http://forums.macosxhints.com/archive/index.php/t-70973.html
我认为这个线程可能会有所帮助:http: //forums.macosxhints.com/archive/index.php/t-70973.html
To paraphrase, you can rename it with the .command extension or create an AppleScript to run the shell.
换句话说,您可以使用 .command 扩展名重命名它,或者创建一个 AppleScript 来运行 shell。
回答by Joe McMahon
In the Script Editor:
在脚本编辑器中:
do shell script "/full/path/to/your/script -with 'all desired args'
"
do shell script "/full/path/to/your/script -with 'all desired args'
”
Save as an application bundle.
另存为应用程序包。
As long as all you want to do is get the effect of the script, this will work fine. You won't see STDOUT or STDERR.
只要您想做的就是获得脚本的效果,这将正常工作。您不会看到 STDOUT 或 STDERR。
回答by CJoshDoll
As joe mentioned, creating the shell script and then creating an applescript script to call the shell script, will accomplish this, and is quite handy.
正如 joe 所提到的,创建 shell 脚本,然后创建一个 applescript 脚本来调用 shell 脚本,将完成此操作,并且非常方便。
Shell Script
外壳脚本
Create your shell script in your favorite text editor, for example:
mono "/Volumes/Media/~Users/me/Software/keepass/keepass.exe"
(this runs the w32 executable, using the mono framework)
Save shell script, for my example "StartKeepass.sh"
在您喜欢的文本编辑器中创建您的 shell 脚本,例如:
mono "/Volumes/Media/~Users/me/Software/keepass/keepass.exe"
(这运行 w32 可执行文件,使用单声道框架)
保存 shell 脚本,例如“StartKeepass.sh”
Apple Script
苹果脚本
Open AppleScript Editor, and call the shell script
do shell script "sh /Volumes/Media/~Users/me/Software/StartKeepass.sh" user name "<enter username here>" password "<Enter password here>" with administrator privileges
do shell script
- applescript command to call external shell commands"sh ...."
- this is your shell script (full path) created in step one (you can also run direct commands, I could omit the shell script and just run my mono command here)user name
- declares to applescript you want to run the command as a specific user"<enter username here>
- replace with your username (keeping quotes) ex "josh"password
- declares to applescript your password"<enter password here>"
- replace with your password (keeping quotes) ex "mypass"with administrative privileges
- declares you want to run as an admin
打开AppleScript Editor,调用shell脚本
do shell script "sh /Volumes/Media/~Users/me/Software/StartKeepass.sh" user name "<enter username here>" password "<Enter password here>" with administrator privileges
do shell script
- 调用外部shell命令的applescript命令"sh ...."
- 这是您在第一步中创建的 shell 脚本(完整路径)(您也可以运行直接命令,我可以省略 shell 脚本并在此处运行我的单声道命令)user name
- 向applescript 声明您希望以特定用户身份运行该命令"<enter username here>
- 替换为您的用户名(保留引号),例如“josh”password
- 声明你的密码"<enter password here>"
- 替换为您的密码(保留引号),例如“mypass”with administrative privileges
- 声明您要以管理员身份运行
Create Your .APP
创建您的.APP
save your applescript as filename.scpt, in my case RunKeepass.scpt
save as... your applescript and change the file format to application, resulting in RunKeepass.app in my case
Copy your app file to your apps folder
将您的 Applescript 保存为 filename.scpt,在我的情况下为 RunKeepass.scpt
另存为...你的applescript并将文件格式更改为应用程序,在我的情况下导致RunKeepass.app
将您的应用程序文件复制到您的应用程序文件夹
回答by Jonny
Someone wrote...
有人写...
I just set all files that end in ".sh" to open with Terminal. It works fine and you don't have to change the name of each shell script you want to run.
我只是将所有以“.sh”结尾的文件设置为用终端打开。它工作正常,您不必更改要运行的每个 shell 脚本的名称。