【问题标题】:Automator/Applescript + shell script: grab parent folder nameAutomator/Applescript + shell 脚本:获取父文件夹名称
【发布时间】:2015-04-07 19:43:46
【问题描述】:

我有一个 Automator 工作流,它利用这个 shell 脚本来获取承载通过这个工作流运行的文件的目录的名称。稍后我将该目录名称作为文件的注释。

for f in "$@"
do
    filepath=$(dirname "$f")
    dirname=$(basename "$filepath")

echo "$dirname"
done

不过,每当我向它抛出多个文件时,目录名称都不会被反映一次(如我所愿),而是会多次反映我丢弃的许多文件。这随后会多次添加相同的评论。

我该如何解决这个问题?

编辑: 我想尝试消除 Automator 并单独使用 Applescript + Shell。 如何让 shell 返回目录名称?现在它只是在对话框中给我$dirname...

on adding folder items to theWatchedFolder after receiving theDetectedItems
    set dirName to do shell script "for f in '$@'
do
    filepath=$(dirname '$f')
    dirname=$(basename '$filepath')

echo '$dirname'
done"
    display alert dirName
end adding folder items to

【问题讨论】:

  • 它必须这样做,因为如果您传递多个文件夹名称,每个文件夹都可能有不同的父级。一定?你希望它做什么?
  • 那么我的整个方法可能是错误的......我想要的是每个文件都被单独处理并分配其唯一的注释(只有它的父文件夹名称)。但我希望能够批处理文件,我可以在工作流程中删除所有文件,并且每个文件都按照此处所述进行操作。 ...那我的选择是什么?
  • 我修改了我的方法以排除 Automator 并选择 Applescript...

标签: shell applescript


【解决方案1】:
on adding folder items to thisFolder after receiving added_items
    repeat with aFile in added_items
        tell application "Finder"
            set parentpath to POSIX path of (parent of (aFile) as string)
            set comment of aFile to parentpath
        end tell
    end repeat
end adding folder items to

【讨论】:

  • 当然,由于这似乎是一个文件夹操作,它将为每个文件添加完全相同的路径...您是否正在寻找它们在移动到文件夹之前所在的路径?那会很艰难……可能需要一台时光机……
【解决方案2】:

我会选择 Applescript droplet。

将此代码保存为应用程序。

当您将文件从单个或多个目录拖放到其上时,它会使用自己的原始目录注释每个文件。

然后将文件移动到列出的移动文件夹中。

property moveFolder : "Macintosh HD:Users:USERNAME:fooDIR:"

on open theseFiles

    repeat with i from 1 to number of items in theseFiles
        set this_item to item i of theseFiles
        tell application "Finder"
            set parentpath to POSIX path of (parent of (this_item) as string)
            set comment of this_item to parentpath
        end tell
    end repeat

    tell application "Finder"

        move theseFiles to moveFolder
    end tell
end open

您可以使用choose 命令来选择将文件移动到何处而不是硬编码,但文件可能并不总是在一个批次中交给 droplet,即使您就是这样将它们放到它上面的。这意味着“选择”对话框可能会在似乎单次运行时显示多次。

但希望以上内容可以为您提供一个起点。

【讨论】:

    猜你喜欢
    • 2014-05-08
    • 2018-05-28
    • 1970-01-01
    • 2013-11-04
    • 2014-08-03
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    相关资源
    最近更新 更多