Dragging and Dropping a File onto a .sh file
Asked Answered
T

3

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.

Tribute answered 27/4, 2013 at 23:19 Comment(0)
T
19

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.

Treasurehouse answered 28/4, 2013 at 5:24 Comment(2)
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?Tribute
I haven't found a way to, but if there is one I'd like to know about it.Treasurehouse
C
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 :-)

Cowen answered 29/7, 2019 at 14:46 Comment(0)
E
0

For Windows, you can create a shortcut on the desktop. Files dragged onto the shortcut will be passed through to the command line in the shortcut.

This example uses a PowerShell script, but you can also use CMD.EXE for batch files. Type cmd /h for options on starting programs in the CMD shell.

PowerShell Example. The line (with quotes) is in a shortcut:

"C:\Program Files\PowerShell\7\pwsh.exe" -File "C:\bin\myps-script.ps1"

Note: PowerShell scripts are not recognize as executables in Windows so this "trick" is necessary for .ps1 files to be clickable.

In MacOS you can do something similar in Automator BUT, the script cannot be interactive as no terminal window is opened. I am still searching for a drag and drop solution for an interactive script in MacOS.

Evin answered 26/8, 2024 at 1:7 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.