18

I need to make a bash script to that I can drag a .rar file on top of it and then the script will receive the pathname of that file as argument 1 ($1).

I have made the bash script, but how do I make it to be an application (right now MAC OSX is treating it as a text file even though I did chmod +x) and how do I enable the drag and drop way of sending a file to my script?

2 Answers 2

19

You should be able to turn your script into an application with Automator - Applications -> Utilities -> Automator.app. Look for an option called "Run Shell Script" and once you're done, it should behave like other apps in that anything that is dragged and dropped on it will be run with it. Good luck!

3
  • I had to drag the automator script from finder to the dock, and then I could drag files to it. Jul 30, 2020 at 23:18
  • Automator didn't load /usr/local/bin into my PATH, but I was able to run executables if I references the full path explicitly. Jul 30, 2020 at 23:20
  • 1
    Make sure to select "Pass input: as arguments" rather than stdin if that's where your script is looking for its inputs. Jul 30, 2020 at 23:21
8

I have created a AppleScript, that redirects all files dropped onto it, to a shell script with the matching name. Simply save as application from within Apple's ScriptEditor and rename as script.app to match your script.sh

on getScriptName()
    tell application "Finder"
        set p to path to me -- alias to the file of the running script
        set fullName to name of file p as text
        set strippedName to text 1 thru ((fullName's length) - (offset of "." in ¬
            (the reverse of every character of fullName) as text)) of fullName -- strip off extension
    end tell
    set my_path to (((path to me as text) & "::") as alias) as string -- parent folder of running script
    set ScriptName to POSIX path of my_path & strippedName & ".sh" -- path to bash script, expected to reside here
    return ScriptName
end getScriptName

on run -- double click on application
    set scriptfile to getScriptName()
    tell application "Terminal"
        activate
        do script scriptfile
    end tell
end run
on open dropfiles -- drag 'n drop files onto application
    set posixfiles to " "
    repeat with dropfile in dropfiles
        set posixfiles to posixfiles & " " & POSIX path of dropfile
    end repeat
    set scriptfile to getScriptName()
    tell application "Terminal"
        activate
        do script scriptfile & " " & posixfiles
    end tell
end open

It's based on this answer, I only got rid of hard-wired script.sh filename

1
  • I thought I'd DuckDuckGo for this kind of thing to save a few minutes instead of whiping up my own version... and here I find this beauty. You, sir, are a gentleman and a scholar.
    – hmijail
    Feb 28 at 3:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .