AppleScript and Labels

I have tried numerous times to set up something with applescript or automator that would move specific labeled files to another folder, maybe i’m missing something or someone else might have some ideas that i could use…

the automator workflow i had set on my Applications folder ended up not working because it would move files regardless what label i set that file to.

What i’m trying to do is set something up to move files with certain labels to another folder to sort them automatically.

The problem i was running into was once the workflow was set, it would move the files anyway and so i was having things moved i didn’t want moved. Is there something i can do to make it be more specific as to what it’s going to move?

I was beginning to wonder if maybe using AppleScript with sorting/finding files with labels was maybe not a bullet proof way of sorting yet… I’m using Snow Leopard. Any help would be appreciated.


Hazel sounds like the perfect solution for you. It can watch folders and move, copy, sort, label, etc.

This is an intensely powerful tool for performing the type of thing you’re looking to do.

At work, I have files that need to be sorted and labelled depending on file name, and we can drop files into folders in our dock. Hazel labels the files green, copies the file to different locations on our server, then moves the file into a completed work folder.

I didn’t see any label specific tools in Automator, other than applying a label, so I’m not sure how well Automator will suit you.

Applescript could easily do the sorting as well, but for me, Hazel is much simpler.

In the AppleScript dictionary for the Finder you’ll see that you can set or get labels.

Hazel works like a charm!

Thanks for the reference.

I’m still interested in expanding my knowledge with AppleScript, here is what I have so far…

tell application “Finder”
if label background color is equal to “yellow” then
move file to “Macintosh HD:Users:lynnbatson:stuff:aliases:common”
display dialog “Finished moving Common aliases!” buttons {“Ok”} with title “Sorting Aliases”
display dialog “No common aliases found!” buttons {“Ok”} with title “Sorting Aliases”
end if
end tell

I get the error “Finder got an error: Can’t get color.” number -1728 from background color

Wondering what I am missing or doing wrong? I think I have the basic idea… Just missing something.


Where you are going wrong is not reading the Libraries provided.
In Applescript Editor.
go to the menu Windows->Library.

The Applications Library window will pop up.
This is where the definitions for the commands that the scriptable applications use are.

Double click on the one for the finder.

In the search field type “label”.

You will get 3 results. Use the command that says " the label of the item"

Also note the (integer) . This tells you that the reference to a label colour is an (integer) not a string.

The Labels in finder use an integers of 0 thru 7. Each (integer) represents a colour.

It is entirely possible to do what you want. An example of label manipulation from my personal library:

tell application "Finder" to set label index of every file of window 1 to 6

--0 -> No Label
--1 -> Orange Label
--2 -> Red Label
--3 -> Yellow Label
--4 -> Blue Label
--5 -> Purple Label
--6 -> Green Label
--7 -> Gray Label

It should be pretty simple to write if statements to sort by label index.

Something like

repeat with oneFile in ListofFiles
if label index of oneFile is 1 then
duplicate oneFile to NewPath
end if
end repeat

and so on.

Assuming that the Applications folder Junkie181 mentions in post #1 is the one at the root level of the startup disk, and that the label colour and destination folder are as per the script attempt in post #5, a basic solution would be:

tell application "Finder"
	set sourceFolder to folder "Applications" of startup disk
	set destinationFolder to folder "stuff:aliases:common:" of home
	move (every file of sourceFolder whose label index is 3) to destinationFolder
end tell

This will of course need to be modified if any of the files’ names are likely to belong to items already in the destination folder.