【问题标题】:Mac Automater: from a string, get a fileMac Automator:从字符串中获取文件
【发布时间】:2015-03-26 21:33:38
【问题描述】:

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

  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 中的第一个文件路径以查找项目的祖父母,并将其作为 POSIX 文件字符串返回。问题是“移动查找器”操作只接受文件/文件夹。如何使用结果字符串选择目标父文件夹,以便将其传递给“移动查找器”操作?

我尝试过的事情:

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

提前致谢!

【问题讨论】:

    标签: service applescript workflow automator finder


    【解决方案1】:

    list 中返回 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
    

    【讨论】:

    • getParent 由于某种原因没有工作,看起来应该可以,但是返回别名的想法是正确的
    【解决方案2】:

    我更喜欢在 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
    

    【讨论】:

    • 不起作用,从上下文菜单中尝试并选择/指定/询问查找器项目,服务设置为接受文件或文件夹作为输入。错误是“无法获取 {} 的第 1 项”。想法?
    • 它对我有用。直接从 Script Editor 而不是 Automator 将其作为 applescript 运行。在 Finder 窗口中选择一些文件,然后运行脚本。因此,既然它有效,那么我们知道问题在于您如何在 automator 中实现它......这是您必须弄清楚的事情。我的建议是不要使用除此代码之外的任何自动化操作,但您似乎已经这样做了,因此它没有正确实现。
    • 现在我知道它存在,从现在开始我会很乐意在脚本编辑器中做这些事情=] automator 很痛苦,但我不知道其他方法。然而,这仍然没有奏效。运行脚本并选择文件,输出显示选择的内容但没有任何移动。假设我做错了什么,我将如何在上下文菜单中将其用作服务?只是把它放在图书馆/服务?
    猜你喜欢
    • 2015-08-06
    • 2013-06-24
    • 2012-09-20
    • 2013-04-10
    • 2016-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多