Search code examples
macosdefaultfile-association

Can a shell script be set as default application on OSX?


Is it possible to set a 'unixy' executable (e.g. shell script) as default application on OSX?

(Default application to open documents of some type/s with, that is. E.g. text documents.)

I guess that's a different launch mechanism from AppleScript events; which I believe is the normal way to launch.

Maybe one can just drop the shell script with a +x flag set into a normal .app bundle structure, and set that application as default; and it would then pass the file name as argument to the script.


Solution

  • Found a solution: launch AppleScript editor tool. Write some code to launch Terminal and run the script from there.

    Or paste this in there:

        on open (documentsToOpen)
            openInXcode(documentsToOpen)
        end open
    
        to openInXcode(documentsToOpen)
            tell application "Terminal"
                repeat with theDocument in documentsToOpen
                    set theDocumentName to POSIX path of theDocument
                    set theScript to "echo " & theDocumentName
                    do script theScript
                    set theScript to "open -a Xcode " & theDocumentName
                    do script theScript
                end repeat
            end tell
        end openInXcode
    

    Opening in Xcode is kind of redundant, but it's just an example what you might want to do in a shell script. Save as an application.

    There is a drawback to this, as it is written: a new Terminal window gets left behind (unless you keep running the script 'forever', of course).

    Also, it seems one must also dig out the Info.plist and add a unique 'Bundle identifier' for the app saved by the AppleScript tool, so that the LaunchServices plist can associate files with this app.