Automator/Applescript+;shell脚本:获取父文件夹名称

Automator/Applescript+;shell脚本:获取父文件夹名称,shell,applescript,Shell,Applescript,我有一个Automator工作流,它利用这个shell脚本获取通过该工作流运行的文件所在目录的名称。稍后,我将该目录名作为文件的注释 for f in "$@" do filepath=$(dirname "$f") dirname=$(basename "$filepath") echo "$dirname" done 但是,每当我向它抛出多个文件时,目录名就不会被反射一次(正如我所希望的那样),而是被反射几次我在其中丢弃的文件。之后,它会多次添加相同的注释 我该如何解决这

我有一个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

我要一滴苹果汁

将此代码另存为应用程序

当您从一个目录或多个目录将文件放到它上面时,它将用它自己的原始目录对每个文件进行注释

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

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
命令来选择将文件移动到何处,而不是硬编码,但文件可能并不总是在一个批次中交给液滴,即使您是这样将它们放在液滴上的。这意味着“选择”对话框可能会在一次运行中多次显示


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

必须这样做,因为如果您传递多个FolderName,每个FolderName可能都有不同的父级。当然?你期望它做什么?我的整个方法可能是错误的。。。我希望每个文件都单独处理,并指定其唯一的注释(仅其父文件夹名称)。但我希望能够批处理文件,我可以在工作流中删除所有文件,并使每个文件的行为符合此处所述。。。那么我的替代方案是什么呢?我修改了排除Automator的方法,选择了Applescript…当然,因为这似乎是一个文件夹操作,它将为每个文件添加完全相同的路径。。。您是否正在查找它们在移动到文件夹之前所在的路径?那会很难。。。可能需要一台时间机器。。。
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