Executing Shell Scripts from the OS X Dock?

前端 未结 9 1696
自闭症患者
自闭症患者 2020-12-22 17:15

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 som

相关标签:
9条回答
  • 2020-12-22 17:57

    On OSX Mavericks:

    1. Create your shell script.
    2. Make your shell script executable:

      chmod +x your-shell-script.sh
      
    3. Rename your script to have a .app suffix:

      mv your-shell-script.sh your-shell-script.app
      
    4. Drag the script to the OSX dock.
    5. Rename your script back to a .sh suffix:

      mv your-shell-script.app your-shell-script.sh
      
    6. Right-click the file in Finder, and click the "Get Info" option.
    7. At the bottom of the window, set the shell script to open with the terminal.

    Now when you click on the script in the dock, A terminal window will pop up and execute your script.

    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.

    0 讨论(0)
  • 2020-12-22 18:00

    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.

    Shell Script

    1. 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)

    2. Save shell script, for my example "StartKeepass.sh"

    Apple Script

    1. 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

    Create Your .APP

    1. save your applescript as filename.scpt, in my case RunKeepass.scpt

    2. save as... your applescript and change the file format to application, resulting in RunKeepass.app in my case

    3. Copy your app file to your apps folder

    0 讨论(0)
  • 2020-12-22 18:01

    I think this thread may be helpful: 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.

    0 讨论(0)
提交回复
热议问题