use AppleScript version "2.4" # Yosemite or higheruse framework "Foundation"
use scripting additions
on adding folder items to this_folder after receiving these_items
-- Defines the path to the alias.
-- If I understand well it's on the Desktop.
-- I assume that it has the same name that the original folder
tell application "Finder"
set itsName to name of this_folder
set nbItems to 1 + (count (get items of this_folder))
end tell
# Choose your preferred syntax
-- set leDossier to ((path to desktop as text) & itsName) as alias
# or
-- set leDossier to ((path to desktop as text) & itsName) as «class furl»
# or
set leDossier to POSIX path of ((path to desktop as text) & itsName)
set tagNames to (current application's NSWorkspace's sharedWorkspace()'s fileLabels()) as list
--> {"Aucun", "Gris", "Vert", "Violet", "Bleu", "Jaune", "Rouge", "Orange"}
if nbItems ≤ (count tagNames) then
my setTags:{tagNames's item nbItems} forItem:leDossier
end if
end adding folder items to
# Borrowed to Shane STANLEY
-- Replace tags; pass a list of the new tags plus a file, alias or POSIX path
on setTags:tagList forItem:fileOrPosixPath
if class of fileOrPosixPath is not text then set fileOrPosixPath to POSIX path of fileOrPosixPath
set thisURL to current application's class "NSURL"'s fileURLWithPath:fileOrPosixPath -- 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:
Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) jeudi 9 mars 2017 18:39:18