Saturday, 19 June 2010

Applescript - Demo changing a file label

Files in MAC OS X can be given a label. This is a colored background in the Finder and on the desktop. There are 8 possibilities:
none, red, orange, yellow, green, blue, purple and grey

That's also the order in which the labels are shown in the Finder menu. However, when experimenting in Applescript, I found that the order seems to have been randomised when trying to change the label color programmatically.

Copy and paste the code below into Applescript Editor and try it out for yourself on a file of your choice.


tell application "Finder"
set thisItem to selection as alias
if label index of thisItem < 7 then
set the label index of thisItem to (label index of thisItem) + 1
else
set label index of thisItem to 0
end if
end tell

(*
0 = no label
1 = orange
2 = red
3 = yellow
4 = blue
5 = purple
6 = green
7 = grey
*)


As you'll see, the order of colors is not as the Filer makes you believe it is.

No comments: