0

我正在尝试制作脚本或自动unrar将所选文件解压缩到特定文件夹(硬编码)。

我希望在选择文件时通过单击查找器中的按钮或键盘快捷键在终端中运行以下代码。

unrar e <path_to_selected_file.rar> <hard_coded_path>

我怎样才能以最好的方式做到这一点?

4

1 回答 1

1

如果您的目标路径是硬编码的,那么我建议您使用 Automator。首先创建一个服务。在应用程序“Finder”中选择顶部的“获取文件”。然后只添加一个动作:“运行 Applescript”。

在该操作中,默认脚本以变量“input”开头。当您在 Finder 中右键单击它们时,此变量将包含所有选定文件的列表。构建您的脚本以循环遍历该列表的文件,使用 POXIS 函数将查找器路径 (myUser:myfolder:myfile) 转换为 shell 路径 (myUser/myfolder/myfile)。使用此路径,使用“do shell script”命令运行您的“unbar”脚本。

保存和测试后,您还可以为该服务定义一个快捷键(在系统偏好设置中)。

这是应该在您的 Applescript Action 中的脚本:

on run {input, parameters}
set Destination to path to desktop folder -- User Desktop by default. can be changed    
set PosixDest to POSIX path of Destination

set SelectedFiles to input
repeat with myFile in SelectedFiles -- loop through each selected file
    set PosixF to POSIX path of myFile -- convert Finder path to Unix path
    try -- try block to handle error during unbar
        do shell script "unrar e " & (quoted form of PosixF) & " " & (quoted form of PosixDest)
    end try
end repeat -- next file

return input
end run

只要您选择压缩文件(接受 unbar 命令),此示例就会运行。为了更安全,您应该只在文件中添加一个测试,以检查它是否是一个可以取消栏的文件。如果没有,就什么也不做。

于 2016-09-04T06:34:15.903 回答