Hello
If I'm not mistaken, you may try the following AppleScript script which will do the job though it may be slow. Script will let you choose the starting directory and the label index to set and then process files with given extenion under the directory tree rooted at the chosen directory. Extension list is defined as property extension_list in script, which is currently set to {"flac"}.
set d to choose folder with prompt ("Choose start folder to process.")
set lb to choose from list ("0 = none
1 = orange
2 = red
3 = yellow
4 = blue
5 = purple
6 = green
7 = gray"'s paragraphs) with prompt "Choose label"
if lb = false then error number -128
set lb to lb's item 1's character 1 as integer
set_label_in_tree(lb, d)
on set_label_in_tree(lb, d)
script o
property extension_list : {"flac"}
property dd : {d}
property aa : {}
property bb : {}
property ee : {}
repeat while (count my dd) > 0
set d to my dd's item 1
set my dd to my dd's rest
tell application "Finder"
tell (item d's files)
set {aa, ee} to {it as alias list, name extension}
end tell
set bb to item d's folders as alias list
end tell
repeat with i from 1 to count my aa
if my ee's item i is not in extension_list then set my aa's item i to false
end repeat
set aa to my aa's aliases
repeat with a in my aa
tell application "Finder" to set a's label index to lb
end repeat
repeat with b in my bb
set my dd's end to b's contents
end repeat
end repeat
end script
tell o to run
end set_label_in_tree
And in case the script using Finder turns out to be too slow for processing large directory tree, you may try the following AppleScript script which is a simple wrapper of rubycocoa script. It requires OSX 10.6 or later. It will behave the same as the script above except that the label index for each colour is different than that for Finder. Indeed label index number is quite a mess and not consistent through different APIs, namely Finder, Cocoa and shell (xattr).
set d to choose folder with prompt ("Choose start folder to process.")
set lb to choose from list ("0 = none
1 = gray
2 = green
3 = purple
4 = blue
5 = yellow
6 = red
7 = orange"'s paragraphs) with prompt "Choose label"
if lb = false then error number -128
set lb to lb's item 1's character 1 as integer
set_label_in_tree(lb, d)
on set_label_in_tree(lb, d)
set args to (("" & lb)'s quoted form) & space & (d's POSIX path's quoted form)
do shell script "/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -w <<'EOF' - " & args & "
#
# ARGV[0]: label index (0..7)
# ARGV[1]: posix path of start directory
#
EXTS = %w[ flac ] # target extensions
require 'osx/cocoa'
include OSX
raise ArgumentError, %Q[Usage: #{File.basename($0)} label directory] unless ARGV.length == 2
lb, dir, = ARGV
lb = lb.to_i
raise ArgumentError, %Q[Invalid label index: #{lb}] unless (0..7).to_a.include? lb
denum = NSFileManager.defaultManager.enumeratorAtPath(dir)
wks = NSWorkspace.sharedWorkspace
re_exts = %r/\\.#{EXTS.join('|')}\\z/
ff = [] # list of target files under dir
while (f = denum.nextObject) != nil do
file = dir + '/' + f.to_s
denum.skipDescendants if wks.isFilePackageAtPath(file) # ignore package contents
ff << file if file =~ re_exts
end
lbl = NSNumber.numberWithInt(lb)
ff.each do |f|
u = NSURL.fileURLWithPath(f)
err = OCObject.new
b = u.objc_send(
:setResourceValue, lbl,
:forKey, NSURLLabelNumberKey,
:error, err)
$stderr.puts 'Failed to set label index to %d: %s' % [lb, f] unless b
end
EOF"
end set_label_in_tree
Both scripts are tested under 10.6.8. Not sure it works well under 10.9.
Hope this may help,
H