¿Alguna forma de configurar / agregar etiquetas en un archivo con Applescript en Mavericks?


9

Intento mover algunos de mis scripts de etiquetas a etiquetas en Mavericks, pero parece que no puedo encontrar una manera de configurar / agregar etiquetas con Applescript.

¿Alguien que sepa hacer esto? Hasta donde puedo entender, las etiquetas no son realmente nuevas, solo nuevas en términos de ser una parte más central del Buscador actualizado.

Respuestas:


7

Puedes usar xattr. Esto copia las etiquetas del archivo1 al archivo2:

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

Las etiquetas se almacenan en una lista de propiedades como una sola matriz de cadenas:

$ 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>

Las etiquetas para los colores tienen valores como Red\n6(donde \nhay un salto de línea).

Si la bandera kColor en com.apple.FinderInfo no está activada, Finder no muestra los círculos para los colores junto a los archivos. Si el indicador kColor está configurado en naranja y el archivo tiene la etiqueta roja, Finder muestra círculos rojos y naranjas. Puede configurar el indicador kColor con AppleScript:

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")' es la sintaxis de plist de estilo antiguo para esto:

<?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-29imprime el valor de los bits utilizados para el indicador kColor. El rojo es C, el naranja es E, el amarillo es A, el verde es 4, el azul es 8, el magenta es 6 y el gris es 2. (El indicador que agregaría 1 a los valores no se usa en OS X).


son las "imágenes de etiqueta" .PNG o gráficos en color? no se pudo encontrar algo como "C.png" en el disco duro :)

1

La respuesta ha sido publicada en la lista de usuarios de Applescript:

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


cita del código de la página escrita por Shane Stanley

Puede hacerlo fácilmente con AppleScriptObjC. Estos son los controladores para recuperar etiquetas, establecer etiquetas y agregar etiquetas:

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:

Si los guarda en una biblioteca de scripts, también puede usarlos desde Mavericks.

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

Al usar nuestro sitio, usted reconoce que ha leído y comprende nuestra Política de Cookies y Política de Privacidad.
Licensed under cc by-sa 3.0 with attribution required.