13

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.

phuclv
  • 37,963
  • 15
  • 156
  • 475
DaGamer12345
  • 131
  • 1
  • 4

2 Answers2

18

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.

Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
  • I tested dragging and dropping directly onto a .sh file (before you answered this), and it didn't seem to work. Not to mention that I would have to have the .sh open automatically in Terminal instead of TextEdit, which the user would have to do that beforehand. Is it possible it would work with .command files instead? – DaGamer12345 Apr 28 '13 at 18:00
  • I haven't found a way to, but if there is one I'd like to know about it. – Gordon Davisson Apr 28 '13 at 18:10
1

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 :-)

mivk
  • 13,452
  • 5
  • 76
  • 69