Mac Automator:从字符串中获取文件

时间:2015-01-28 04:19:49

标签: service applescript workflow automator finder

我正在尝试通过自动化服务创建一个快捷方式,将所选文件移动到目录中。它如下:

  1. 获取选定的Finder项目

  2. 获取变量值Path

  3. 运行Applescript:

    on join(someList, delimiter)
    set prevTIDs to AppleScript's text item delimiters
        set AppleScript's text item delimiters to delimiter
        set output to "" & someList
        set AppleScript's text item delimiters to prevTIDs
        return output
    end join
    to split(someText, delimiter)
        set AppleScript's text item delimiters to delimiter
        set someText to someText's text items
        set AppleScript's text item delimiters to {""}
        return someText
    end split
    on run {input, parameters}
        set pathToMe to POSIX path of (item 1 of input as text)
        set newPath to split(pathToMe, "/")
        set revPath to reverse of newPath
        set restList to rest of revPath
        set restList to rest of restList
        set joinPath to join(reverse of restList, "/")
        set source to POSIX file joinPath
        return source
    end run
    
  4. 设置变量Parent

  5. 的值
  6. 将Finder项目移至Parent

  7. Applescript解析Path中的第一个文件路径,以便找到项目的祖父母,并将其作为POSIX文件字符串返回。问题是“移动查找器”操作只接受文件/文件夹。如何选择带有结果字符串的目标父文件夹,以便将其传递给“移动查找器”操作?

    我尝试过的事情:

    • mv中使用Run Bash ScriptRun Applescript操作似乎没有向Run Bash Script返回任何内容;设置为参数输入,“$ @”始终为空。
    • tell finder中执行Run Applescript。没有错误或警告,没有任何反应。
    • 手动设置parent变量的值。

    提前致谢!

2 个答案:

答案 0 :(得分:1)

列表中返回alias类型的路径,而不是posix file

on run {input, parameters}
    set pathToMe to (item 1 of input) as text
    set f to my getParent(pathToMe, ":")
    return {f as alias}
end run

to getParent(someText, delimiter)
    set prevTIDs to AppleScript's text item delimiters
    set AppleScript's text item delimiters to delimiter
    set n to -2
    if someText ends with ":" then set n to -3
    set t to text 1 thru text item n of someText
    set AppleScript's text item delimiters to prevTIDs
    return t
end getParent

答案 1 :(得分:0)

我更喜欢在AppleScript中执行此操作,因此请尝试使用此代码。我没有测试它,但它应该工作。您仍然可以使用applescript操作将其添加到automator,但不需要所有其他操作。它会自己做所有事情。祝你好运。

tell application "Finder"
    set theSelection to get selection
    set parentFolder to container of (item 1 of theSelection)
    move theSelection to parentFolder
end tell