OSX Associate a shell script with a file extension?

对着背影说爱祢 提交于 2021-01-22 03:51:05

问题


I want to associate the .exe file extension with a shell script that launches wine. What is the best way to do this?

From what I've gathered, I need to create an AppleScript that will call wine, but how do I get the name of the input file in the AppleScript? If there is a better way to do this, let me know, but as far as I know this is the best way.


回答1:


You can use an AppleScript application to do this - files are passed to the open handler, the same as a droplet, for example:

on open theFiles
    set arguments to ""
    repeat with anItem in theFiles
        set arguments to arguments & space & (quoted form of POSIX path of anItem)
    end repeat
    do shell script "/path/to/wine" & arguments
end open

To associate a particular document type with your application, you will first need to add information to your application's information property list (Info.plist) to tell Launch Services what types of documents it can handle. See Apple's Document-Based Applications Overview (or take a look at the settings in other applications).




回答2:


You can also use Automator to wrap your own bash, python or ruby script in an "Application".

  1. Open Automator and choose to create an Application.

  2. Find the Action "Run Shell Script" and double-click it or drag it to the script area.

  3. Select the interpreter you want (bash, other shells, python or ruby).

  4. Set the "Pass input" option to "as arguments". (In the automator model, the application "receives files and folders as input"; hence this lets your script see filenames as commandline arguments).

  5. Enter your shell script in the edit area. On bash, use "$@" for the list of commandline arguments (individually quoted to protect embedded spaces).

You can now save the script (it will get a .app extension), and move it to the Applications folder or other reasonable location. It can be associated with a file type, like any other application.

NOTE: This works on Mountain Lion (10.8); can someone comment on how long Automator has been able to do this?



来源:https://stackoverflow.com/questions/7184987/osx-associate-a-shell-script-with-a-file-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!