使用 Applescript 修改文件标签

问题描述 投票:0回答:3

我试图从文件夹的所有内容中递归地删除所有标签(OS X 10.9 的新功能之一)。由于该文件夹中有许多文件(以及包含更多文件的文件夹),我想尝试使用 Applescript 来简化该过程。我在网上查了一下,没有发现任何有用的东西。

此外,我在 Finder 或标准添加词典中找不到任何对我有帮助的内容。

可能是这样工作的:

set folder to "folder_path"
set files to (all files of folder)
for each file:
    check for tag (optional)
    remove all tags from file

PS。上面的代码应该是脚本功能的指南,而不是使其工作的确切代码。

applescript osx-mavericks
3个回答
3
投票

这将递归地从文件夹的文件中删除所有标签:

set targetFolder to POSIX path of (choose folder with prompt "Remove all tags from this folder..." default location path to desktop)

do shell script "xattr -rd com.apple.metadata:_kMDItemUserTags " & quoted form of targetFolder

2
投票

我认为您遇到的问题是您的文件实际上没有标签 - 它们可能只是有标签。

尽管 Mavericks 中的新标签系统旨在通过将标签显示为标签来合并旧标签系统,但在 Mavericks 之前的 Mac OS X 版本中标记为黄色的文件实际上可能没有正确的标签。他们只是有老式的标签,小牛队将其显示为标签。如果文件上的标签是黄色和蓝色之类的东西,那么这些可能只是标签。这可能就是您在尝试使用 xattr 删除它们时看到错误的原因。

因此,如果您的文件只有标签,则删除标签的方法与您仍在运行 Mountain Lion 时相同。您要求 Finder 将文件的标签索引设置为 0。

此 AppleScript 要求您选择一个文件夹,然后循环遍历该文件夹中的所有文件,如果文件上有标签,则删除该标签。

tell application "Finder"
    activate
    set theFolder to (choose folder with prompt "Choose a folder to remove labels from the files within:")
    set theFiles to every file of theFolder
    repeat with theFile in theFiles
        if the label index of theFile is not equal to 0 then
            set the label index of theFile to 0
        end if
    end repeat
    open theFolder
end tell

需要明确的是:上面的 AppleScript 仅删除了自 Mac OS X 之前就存在的 7 种标准标记/标签颜色。如果您通过打开“获取信息”窗口并输入项目名称或类似内容来手动标记 Mavericks 中的文件作为标签,那么必须通过 shell 脚本删除该标签,如上面 adayzdone 的响应中所述。


0
投票

这是一个完整的解决方案,可用于清除标签和标签。基本上是其他两个答案的组合。

如果将此 AppleScript 保存为应用程序,您可以将文件和文件夹拖放到其上,它们的标签 + 标签将被清除。

-- A script for removing all Tags and the Label from files & folders
--
-- The following handler gets executed when files are dropped onto
-- this script (when saved as an application)
on open of theFiles
    repeat with aFile in theFiles
        -- remove Tags with a shell command
        try
            set qpath to quoted form of (POSIX path of aFile)
            do shell script "/usr/bin/xattr -d com.apple.metadata:_kMDItemUserTags " & qpath
        end try
        -- remove the Label through the Finder's Applescript support
        tell application "Finder"
            if the label index of aFile is not equal to 0 then
                set the label index of aFile to 0
            end if
        end tell
    end repeat
end open

您还可以将其变成Service,以便您可以右键单击Finder中的文件以删除其标签:打开Automator.app,创建一个新服务,添加AppleScript操作,设置“服务接收选定的”弹出“文件或文件夹”并将代码粘贴到其中,将第一行更改为

on run {theFiles, parameters}
。然后保存它,例如名为“删除标签和标签”。下次您在 Finder 中右键单击某个文件时,请查看上下文菜单底部的“服务”,您将在其中找到此命令。

© www.soinside.com 2019 - 2024. All rights reserved.