Dragging and Dropping a File onto a .sh file

 ̄綄美尐妖づ 提交于 2019-12-09 17:10:26

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/16257884/dragging-and-dropping-a-file-onto-a-sh-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!