简体   繁体   English

从 OS X Dock 执行 Shell 脚本?

[英]Executing Shell Scripts from the OS X Dock?

How do I set up a shell script to execute from the Mac OSX dock?如何设置 shell 脚本以从 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?我需要在某处设置一个标志来告诉它运行而不是打开它进行编辑吗?

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".然后File > Save As ,并将文件格式更改为“应用程序”。 When you open the application, it will run the Shell Script step, executing the command, exiting after it completes.当您打开应用程序时,它将运行 Shell 脚本步骤,执行命令,完成后退出。

The benefit to this is it's really simple 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文件夹中!)

示例工作流程

If you don't need a Terminal window, you can make any executable file an Application just by creating a shell script Example and moving it to the filename Example.app/Contents/MacOS/Example .如果您不需要终端窗口,您只需创建一个 shell 脚本Example并将其移动到文件名Example.app/Contents/MacOS/Example即可将任何可执行文件设为应用程序。 You can place this new application in your dock like any other, and execute it with a click.您可以像其他任何应用程序一样将这个新应用程序放置在您的 Dock 中,然后单击执行它。

NOTE: the name of the app must exactly match the script name.注意:应用程序的名称必须与脚本名称完全匹配。 So the top level directory has to be Example.app and the script in the Contents/MacOS subdirectory must be named Example , and the script must be executable.所以顶级目录必须是Example.app并且Contents/MacOS子目录中的脚本必须命名为Example ,并且脚本必须是可执行的。

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 做一些事情,但这不是很干净。

On OSX Mavericks:在 OSX Mavericks 上:

  1. Create your shell script.创建您的 shell 脚本。
  2. Make your shell script executable:使您的 shell 脚本可执行:

     chmod +x your-shell-script.sh
  3. Rename your script to have a .app suffix:重命名您的脚本以具有.app后缀:

     mv your-shell-script.sh your-shell-script.app
  4. Drag the script to the OSX dock.将脚本拖到 OSX Dock。
  5. Rename your script back to a .sh suffix:将您的脚本重命名回.sh后缀:

     mv your-shell-script.app your-shell-script.sh
  6. Right-click the file in Finder, and click the "Get Info" option.在 Finder 中右键单击该文件,然后单击“获取信息”选项。
  7. At the bottom of the window, set the shell script to open with the terminal.在窗口底部,将 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并将终端设置更改为“如果干净退出则关闭 shell”,就像在此 SO answer 中所说的那样。

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.如果您需要运行脚本并希望终端弹出以便您可以看到结果,您可以像深渊骑士所说的那样并将扩展名更改为 .command。 If you double click on it it will open a terminal window and run.如果您双击它,它将打开一个终端窗口并运行。

I however needed this to run from automator or appleScript.但是,我需要它来从 automator 或 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.因此,为了打开一个新终端,我从“运行 shell 脚本”运行的命令是“打开 myShellScript.command”,它在新终端中打开。

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.如果您想要一个扩展名(如 foo.sh),您可以转到 Finder 中的文件信息窗口并更改该特定脚本的默认应用程序(TextEdit、TextMate,无论您的计算机上为.sh 文件)到终端。 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.同样,您必须将其拖到 Dock 的右侧。

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。

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。

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外壳脚本

  1. Create your shell script in your favorite text editor, for example:在您喜欢的文本编辑器中创建您的 shell 脚本,例如:

    mono "/Volumes/Media/~Users/me/Software/keepass/keepass.exe"

    (this runs the w32 executable, using the mono framework) (这运行 w32 可执行文件,使用单声道框架)

  2. Save shell script, for my example "StartKeepass.sh"保存 shell 脚本,例如“StartKeepass.sh”

Apple Script苹果脚本

  1. Open AppleScript Editor, and call the shell script打开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 - applescript command to call external shell commands do shell script - 调用外部shell命令的applescript命令
    • "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) "sh ...." - 这是您在第一步中创建的 shell 脚本(完整路径)(您也可以运行直接命令,我可以省略 shell 脚本并在此处运行我的单声道命令)
    • user name - declares to applescript you want to run the command as a specific user user name - 向 applescript 声明您希望以特定用户身份运行命令
    • "<enter username here> - replace with your username (keeping quotes) ex "josh" "<enter username here> - 替换为您的用户名(保留引号),例如“josh”
    • password - declares to applescript your password password - 声明你的密码
    • "<enter password here>" - replace with your password (keeping quotes) ex "mypass" "<enter password here>" - 替换为您的密码(保留引号)例如“mypass”
    • with administrative privileges - declares you want to run as an admin with administrative privileges - 声明您要以管理员身份运行

Create Your .APP创建您的 .APP

  1. save your applescript as filename.scpt, in my case RunKeepass.scpt将您的 Applescript 保存为 filename.scpt,在我的情况下为 RunKeepass.scpt

  2. save as... your applescript and change the file format to application, resulting in RunKeepass.app in my case另存为...你的applescript并将文件格式更改为应用程序,在我的情况下导致RunKeepass.app

  3. Copy your app file to your apps folder将您的应用程序文件复制到您的应用程序文件夹

Exact steps to achieve that in macOS Monterey 12.3在 macOS Monterey 12.3 中实现该目标的确切步骤

  1. Open Automator打开Automator
  2. File -> New File -> New
  3. Choose Application选择Application
  4. Go to Library -> Utilities Go 到Library -> Utilities
  5. Double-click Run Shell Script双击Run Shell Script
  6. Type in whatever command you want to run.输入您要运行的任何命令。 For example, try the command to toggle Dark Mode:例如,尝试使用命令切换深色模式:
osascript -e 'tell app "System Events" to tell appearance preferences to set dark mode to not dark mode'
  1. File -> Save File -> Save
  2. Drag the saved file to the Dock, done!将保存的文件拖到 Dock,大功告成!

pip install mac-appify pip 安装 mac-appify

I had trouble with the accepted solution but this command worked for me.我在接受的解决方案上遇到了问题,但此命令对我有用。

Install安装

pip install mac-appify

Run

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/appify ~/bin/webex_start.sh ~/Desktop/webex.app

Someone wrote...有人写...

I just set all files that end in ".sh" to open with Terminal.我只是将所有以“.sh”结尾的文件设置为用终端打开。 It works fine and you don't have to change the name of each shell script you want to run.它工作正常,您不必更改要运行的每个 shell 脚本的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM