【问题标题】:Run script on a selected file在选定的文件上运行脚本
【发布时间】:2016-09-03 16:20:56
【问题描述】:

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

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

unrar e <path_to_selected_file.rar> <hard_coded_path>

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

【问题讨论】:

标签: macos terminal applescript rar unrar


【解决方案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 命令),此示例就会运行。为了更安全,您应该只在文件中添加一个测试,以检查它是否是一个可以取消栏的文件。如果没有,什么都不做。

【讨论】:

  • 我不知道应该如何在 shell 脚本中使用 [input],而且我的路径包含空格和“!”那个applescript似乎有问题,我该如何处理?
猜你喜欢
  • 2012-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-27
  • 2014-06-11
  • 1970-01-01
  • 2017-01-16
相关资源
最近更新 更多