11

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.

4

2 回答 2

16

我不知道让原始脚本接受拖放文件的方法,但是有许多选项可以将其包装在接受拖放的其他东西中,并将文件传递给脚本。IMO 最快的选择是将脚本包装在 Automator 应用程序中。运行 /Applications/Automator.app,选择 Application 作为新项目的类型,将“运行 Shell 脚本”操作从操作列表(第二列)拖到工作流(右列)中,将其“传递输入”设置为“作为参数”,粘贴到 shell 脚本中,然后保存应用程序。完毕。

包装 shell 脚本的其他选项包括:AppleScriptPlatypusDropScript

于 2013-04-28T05:24:02.147 回答
1

我迟到了,甚至没有回答你的拖放问题。但我注意到这看起来像是 Windows 的批处理脚本,cmd.exe在任何 Unix shell 中都不起作用。所以你真的需要首先为 Bash 完全重写它!

  • “\”是 Unix 中的转义字符。路径分隔符是“/”。
  • Shell 变量以“$”开头,而不是“%”。并且 Windows%USERPROFILE%变量将$HOME在 Unix 中。
  • 选项以“-”开头,而不是“/”。
  • rmdir只删除空目录。请参阅man rmdirman rm与您的Windows 命令-f -r等效的选项。rmdir /q /s Temp

但是要注意,一旦你学会了一些 Bash,你就会开始真正讨厌那种可怕的cmd.exe:-)

于 2019-07-29T14:46:00.853 回答