Mac Automater:从一个字符串中获取一个文件

Mac Automater: from a string, get a file

我正在尝试通过自动化服务创建快捷方式,该服务会将 selected 文件向上移动一个目录。内容如下:

  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. 将 Finder 项目移动到 Parent

Applescript 解析 Path 中的第一个文件路径以找到项目的总 parent,return 将其作为 POSIX 文件字符串。问题是 "Move Finder" 操作只接受 Files/Folders。我如何 select 目标 parent 文件夹和生成的字符串以便将其传递给 "Move Finder" 操作?

我尝试过的事情:

提前致谢!

我更喜欢在 applescript 中完成这一切,所以试试这个代码。我没有测试它,但它应该可以工作。您仍然可以使用 applescript 操作将其添加到自动化器中,但您不需要所有其他操作。它会自己做所有事情。祝你好运。

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

return 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