Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dragging and Dropping a File onto a .sh file

I have a fairly good amount of knowledge with Batch. I'm trying to port a batch script over to Mac/UNIX, but my batch file has a drag-and-drop thing to it. Through countless Google searches, I have came up with nothing. They all say that you can drag-and-drop into a Terminal Window - not good for no-input-required scripts.

Here is the code for Batch I have:

cd %USERPROFILE%

7za x %* -o%USERPROFILE%\Desktop\Temp

7za a %1 %USERPROFILE%\Desktop\Temp\*

cd %USERPROFILE%\Desktop
rmdir /q /s Temp\

Not particularly worried about 7za commands (because of Archive Utility),cd %USERPROFILE% (because Terminal starts in the user's profile), rmdir, cd, and such, as they are only basic file commands. But I don't know the code to reference to the file dropped/opened with the .sh script.

So, if someone knows that code, please tell me. I know this is kind-of a simple thing, but you can't know every command, especially when dealing with unfamiliar programming languages.

like image 766
DaGamer12345 Avatar asked Apr 27 '13 23:04

DaGamer12345


People also ask

How does a .sh file work?

A shell script or sh-file is something between a single command and a (not necessarily) small programm. The basic idea is to chain a few shell commands together in a file for ease of use. So whenever you tell the shell to execute that file, it will execute all the specified commands in order.

How do I save a .sh file in Windows?

Open Command Prompt and navigate to the folder where the script file is available. Type Bash script-filename.sh and hit the enter key.


2 Answers

I don't know of a way to have the raw script accept drag-and-dropped files, but there are a number of options to wrap it in something else that accepts drag-and-drop, and passes the files to a script. IMO the quickest option is to wrap the script in an Automator application. Run /Applications/Automator.app, select Application as the type for your new project, drag the "Run Shell Script" action from the actions list (second column) into the workflow (right column), set its "Pass input" to "as arguments", paste in the shell script, then save the app. Done.

Other options for wrapping a shell script include: AppleScript, Platypus, and DropScript.

like image 151
Gordon Davisson Avatar answered Oct 30 '22 13:10

Gordon Davisson


I'm late to this, and don't even have the answer to your drag-and-drop question. But I notice this looks like a Batch script for Windows' cmd.exe which will not work at all in any Unix shell. So you really need to completely rewrite it for Bash first!

  • "\" is an escape character in Unix. The path separator is "/".
  • Shell variables begin with "$", not "%". And the Windows %USERPROFILE% variable would be $HOME in Unix.
  • Options start with "-", not "/".
  • rmdir only deletes empty directories. See man rmdir and man rm with the -f -r options for an equivalent of your rmdir /q /s Temp Windows command.

But beware that once you learn some Bash, you will start to really hate that horrible cmd.exe :-)

like image 38
mivk Avatar answered Oct 30 '22 13:10

mivk