##################################
# on idle.app
# This script MUST be saved as a stay open application
# In real life put it in the list of files to launch on boot.
# to try, just double click its icon
##################################
use AppleScript version "2.4" # Yosemite or higher
use framework "Foundation"
use scripting additions
property reverseTheList : true
# true = build a list whose 7 last items are reversed from standard one
# false = use the standard list but calculate the correct index
#=====
on run
--
end run
#=====
on idle
local |⌘|, p2d, theAliases, tagNames, nbNames, skipHidden, anAlias
local aliasURL, originalURL, theError, fileManager, theFiles, nbItems, indx, theTag
# Some instructions triggered only once
set |⌘| to current application
set p2d to path to desktop as text
set theAliases to {"my alias", "tempo"} #<<<<<<<<<<<<<<<<<<<<<<< Edit to fit your needs
# Grab the standard list of tag names
set tagNames to (|⌘|'s NSWorkspace's sharedWorkspace()'s fileLabels()) as list
(*
1 -- "Aucun", none
2 -- "Gris", gray
3 -- "Vert", green
4 -- "Violet", purple
5 -- "Bleu", blue
6 -- "Jaune", yellow
7 -- "Rouge", red
8 -- "Orange", orange
*)
(*
# Don't ask me why, the labels are ordered backwards compared to tag names.
tell application "Finder"
set label index of anItem to 1 # orange it's 8th tagName
set label index of anItem to 2 # red it's 7th tagName
set label index of anItem to 3 # yellow it's 6th tagName
set label index of anItem to 4 # blue it's 5th tagName
set label index of anItem to 5 # purple it's 4th tagName
set label index of anItem to 6 # green it's 3rd tagName
set label index of anItem to 7 # gray it's 2nd tagName
end tell
*)
set labelNames to reverse of tagNames
set nbNames to count tagNames
set skipHidden to (|⌘|'s NSDirectoryEnumerationSkipsHiddenFiles)
set fileManager to |⌘|'s NSFileManager's defaultManager()
# Now the loop
repeat with anAlias in theAliases
set POSIXAlias to POSIX path of (p2d & anAlias)
set aliasURL to (|⌘|'s class "NSURL"'s fileURLWithPath:POSIXAlias)
log "aliasURL : " & aliasURL
set {originalURL, theError} to (|⌘|'s class "NSURL"'s URLByResolvingAliasFileAtURL:aliasURL options:0 |error|:(reference)) # Thank's to Shane STANLEY
log "originalURL : " & originalURL
set POSIXOriginal to originalURL's |path| as text
set theFiles to (fileManager's contentsOfDirectoryAtURL:originalURL includingPropertiesForKeys:{} options:skipHidden |error|:(missing value))
set nbItems to count theFiles
# A bit of trickery to apply Robert's wishes based upon the fact that he is accustomed to work with label indexes.
# What need to work with tags, not with labels ?
# If we never apply a tag to an item, we may clear its label with : set label index of anItem to 0
# If we apply - at least once - a tag, we are unable to get rid of it with : set label index of anItem to 0
# Yes, you understood, tags are a drug like crack ;-)
if reverseTheList then
if nbItems = 0 then
set indx to nbNames
else
set indx to nbItems
if (indx + 1) ≥ nbNames then set indx to nbNames - 1
end if
set theTag to labelNames's item indx
else
if nbItems + 1 ≥ nbNames then
set indx to 2
else if nbItems = 0 then
set indx to 1
else
set indx to nbNames + 1 - nbItems
end if
set theTag to tagNames's item indx
end if
(my setTags:{theTag} forItem:POSIXOriginal)
(my setTags:{theTag} forItem:POSIXAlias)
end repeat
return 10 # period of 10 seconds. You may use 5 seconds #<<<<<<<<<<<<<<<<<<<<<<< Edit to fit your needs
end idle
# Handler borrowed to Shane STANLEY
-- Replace tags; pass a list of the new tags plus an URL
on setTags:tagList forItem:fileOrPOSIXPath
local |⌘|, thisURL, theResult, theError
set |⌘| to current application
if class of fileOrPOSIXPath is not text then set fileOrPOSIXPath to POSIX path of fileOrPOSIXPath
set thisURL to |⌘|'s class "NSURL"'s fileURLWithPath:fileOrPOSIXPath -- make URL
set {theResult, theError} to thisURL's setResourceValue:tagList forKey:(|⌘|'s NSURLTagNamesKey) |error|:(reference)
if theResult as boolean is false then error (theError's |localizedDescription|() as text)
end setTags:forItem:
#=====
on quit
continue quit
end quit
#=====
Eureka, I think that I understood why the tag names are in reverse order.
It's the easy way to get the name linked to a label index.
use AppleScript version "2.4" # Yosemite or higher
use framework "Foundation"
use scripting additions
set |⌘| to current application
set targetFile to choose file
# Grab the standard list of tags
set tagNames to (|⌘|'s NSWorkspace's sharedWorkspace()'s fileLabels()) as list
(*
1 -- "Aucun", none
2 -- "Gris", gray
3 -- "Vert", green
4 -- "Violet", purple
5 -- "Bleu", blue
6 -- "Jaune", yellow
7 -- "Rouge", red
8 -- "Orange", orange
*)
set labelNames to (reverse of tagNames)
(*
1 -- "Orange", orange
2 -- "Rouge", red
3 -- "Jaune", yellow
4 -- "Bleu", blue
5 -- "Violet", purple
6 -- "Vert", green
7 -- "Gris", gray
8 -- "Aucun", none
*)
repeat with indx from 1 to 7
tell application "Finder" to set label index of targetFile to indx
log "indx = " & indx & " colorname = " & labelNames's item indx
end repeat
set indx to 0
tell application "Finder" to set label index of targetFile to indx
log "indx = " & indx & " colorname = " & last item of labelNames
Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) samedi 11 mars 2017 20:46:01