Re: [AS] Changer le label du fichier (tag de couleur)

Page principale
Supprimer ce message
Répondre à ce message
Auteur: Yvan KOENIG via Applescript_fr
Date:  
À: Liste AppleScript francophone
CC: Yvan KOENIG
Sujet: Re: [AS] Changer le label du fichier (tag de couleur)
Désolé mais la syntaxe requise par ASObjC ne s'improvise pas.

Vous trouverez ci-dessous un example simple.

[script]
use AppleScript version "2.3.1"
use scripting additions
use framework "Foundation"

-- Fetch tags; pass a POSIX path
on returnTagsFor:thePathInput
    set thisURL to my (|NSURL|'s fileURLWithPath:(thePathInput)) -- make URL
    set {theResult, theTags, theError} to thisURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(reference)
    if theResult as boolean is false then error (theError's |localizedDescription|() as text)
    if theTags = missing value then return {} -- because when there are none, it returns missing value
    return theTags as list
end returnTagsFor:


-- Replace tags; pass a list of the new tags plus a  POSIX path
on setTags:tagList forItem:thePathInput
    set thisURL to my (|NSURL|'s fileURLWithPath:(thePathInput)) -- make URL
    set {theResult, theError} to thisURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(reference)
    if theResult as boolean is false then error (theError's |localizedDescription|() as text)
end setTags:forItem:


-- Add tags; pass a list of the new tags plus a  POSIX path
on addTags:tagList forItem:thePathInput
    set thisURL to my (|NSURL|'s fileURLWithPath:(thePathInput)) -- make URL
    -- get existing tags
    set {theResult, theTags, theError} to thisURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(reference)
    if theResult as boolean is false then error (theError's |localizedDescription|() as text)
    if theTags ≠ missing value then -- make list of old and 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
    set {theResult, theError} to thisURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(reference)
    if theResult as boolean is false then error (theError's |localizedDescription|() as text)
end addTags:forItem:


-- Code utilisateur

set aFile to choose file
set POSIXPath to POSIX path of aFile
-- crée un ou plusieurs tags
my setTags:{"3"} forItem:POSIXPath
-- récupère le ou les tags
set theTags to my returnTagsFor:POSIXPath
log result -- (*3*)
-- ajoute des tags
my addTags:{"rouge", "corrompu"} forItem:POSIXPath
-- récupère le ou les tags
set theTags to my returnTagsFor:POSIXPath
log result -- (*3, rouge, corrompu*)
-- supprime les tags
my setTags:{""} forItem:POSIXPath
-- récupère le ou les tags
set theTags to my returnTagsFor:POSIXPath
log result
[/script]

Yvan KOENIG (VALLAURIS, France) mercredi 12 mai 2021 16:30:55
_______________________________________________
Applescript_fr mailing list
Applescript_fr@???
http://listes.patpro.net/mailman/listinfo/applescript_fr