有什么办法在Mavericks下使用Applescript在文件上设置/添加标签?


9

试图将我的一些脚本从标签移到Mavericks下的标签上,但是我似乎找不到使用Applescript设置/添加标签的方法。

有人知道该怎么做吗?据我所知,标签并不是真正的新标签,只是成为更新的Finder的核心部分而已。

Answers:


7

您可以使用xattr。这会将标签从文件1复制到文件2:

xattr -wx com.apple.metadata:_kMDItemUserTags "$(xattr -px com.apple.metadata:_kMDItemUserTags file1)" file2
xattr -wx com.apple.FinderInfo "$(xattr -px com.apple.FinderInfo file1)" file2

标签作为单个字符串数组存储在属性列表中:

$ xattr -p com.apple.metadata:_kMDItemUserTags file3|xxd -r -p|plutil -convert xml1 - -o -
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <string>Red
6</string>
    <string>aa</string>
    <string>Orange
7</string>
    <string>Yellow
5</string>
    <string>Green
2</string>
    <string>Blue
4</string>
    <string>Purple
3</string>
    <string>Gray
1</string>
</array>
</plist>

颜色标签的值类似于Red\n6(其中\n是换行符)。

如果未设置com.apple.FinderInfo中的kColor标志,则Finder不会在文件旁边显示圆圈。如果kColor标志设置为橙色,并且文件带有红色标签,则Finder会同时显示红色和橙色圆圈。您可以使用AppleScript设置kColor标志:

do shell script "xattr -w com.apple.metadata:_kMDItemUserTags '(\"Red\\n6\",\"new tag\")' ~/desktop/file4"
tell application "Finder" to set label index of file "file4" of desktop to item 1 of {2, 1, 3, 6, 4, 5, 7}

'("Red\n6","new tag")' 是老式的plist语法:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <string>Red
6</string>
    <string>new tag</string>
</array>
</plist>

xattr -p com.apple.FinderInfo file|head -n1|cut -c28-29打印用于kColor标志的位的值。红色是C,橙色是E,黄色是A,绿色是4,蓝色是8,洋红色是6,灰色是2。(在OS X中不使用将值加1的标志。)


标签图像是.PNG还是彩色图形?在硬盘上找不到“ C.png”之类的东西:)

1

答案已发布在Applescript用户列表上:

http://lists.apple.com/archives/applescript-users/2015/Jan/msg00193.html


引用页码-Shane Stanley编写的代码

您可以使用AppleScriptObjC轻松完成此操作。这是检索标签,设置标签和添加标签的处理程序:

use scripting additions
use framework "Foundation"

on returnTagsFor:posixPath -- get the tags
    set aURL to current application's |NSURL|'s fileURLWithPath:posixPath -- make URL
    set {theResult, theTags} to aURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
    if theTags = missing value then return {} -- because when there are none, it returns missing value
    return theTags as list
end returnTagsFor:

on setTags:tagList forPath:posixPath -- set the tags, replacing any existing tags
    set aURL to current application's |NSURL|'s fileURLWithPath:posixPath -- make URL
    aURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
end setTags:forPath:

on addTags:tagList forPath:posixPath -- add to existing tags
    set aURL to current application's |NSURL|'s fileURLWithPath:posixPath -- make URL
    -- get existing tags
    set {theResult, theTags} to aURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
    if theTags  missing value then -- add new tags
        set tagList to (theTags as list) & tagList
        set tagList to (current application's NSOrderedSet's orderedSetWithArray:tagList)'s allObjects() -- delete any duplicates
    end if
    aURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
end addTags:forPath:

如果将它们保存在脚本库中,也可以从Mavericks中使用它们。

-Shane Stanley www.macosxautomation.com/applescript/apps/

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.