How can I tell the Finder to change the labels...

I am looking for an applescript that can change labels of all contents of a folder and all its subfolders from one color to another. I am definitely NOT a scripter, so I would appreciate if someone would help me with that. Many thanks in advance...

Posted on Jul 6, 2014 12:56 PM

Reply
12 replies
Sort By: 

Jul 6, 2014 2:50 PM in response to Twitcheye

For the non-Scripter, there is FolderLabel, an Automator solution. It has three principal actions.

  1. Label Finder Items

    Set or remove x the outer folder tag color

  2. Get Folder Contents

    Option to get the entire inner file/folder hierarchy

  3. Label Finder Contents

    Set or remove x all tag colors of inner file/folder contents


Launch Automator from /Applications/Automator. It is the silver robot with the Javelin rocket launcher. Click New Document > Application > Choose.


On the extreme left is the Library categories, the adjacent column are the Library actions, and the large space is where you drag and drop the actions. Locate the Files and Folders Library category. In the following order, drag and drop the following actions whose name appears in items 1., 2., and 3. above. Look at my enclosed image for guidance. Don’t forget to select the Repeat for each subfolder found.


To make this work, you File > Save… and save it to your Desktop as FolderLabel. Drag and drop any folder on it. The first tag color category is for this outer folder that you just dragged/drop. The second tag color panel, is for the contents of this dragged/dropped folder. The colors are applied in the original location of the folder that you dragged/dropped on FolderLabel.


User uploaded file

Reply

Jul 6, 2014 5:12 PM in response to VikingOSX

Hi, VikingOSX,


This is really helpful, but I forgot one thing: I want to change label color only for files with extension .flac. (I still want all encompassing folders to change the color to the same one as .flac files.) How would that affect your previous reply? Please, bear with me... After that, I hope, there will be no more stupid questions. 😝

Reply

Jul 9, 2014 4:24 PM in response to VikingOSX

Hi, Viking,

I replicated your Application and it did work, but not exactly as I expected. Namely, instead of replacing the old color label with the new one, I get both old and new colors (in Mavericks, I can apply more than one label color per Finder item). To remove the old label, I have to click on that color's option button.


So, I tried to modify your suggested app by inserting another 'Label Finder Items' action as the next to the last one and set the color in that action to the one that I want to remove, in the hope that after that, the last 'Label Finder Items' action will finally add the right color. But it did not work. I still got TWO color labels, as in the attached image.User uploaded file


Do you have a suggestion for a work-around?

Reply

Jul 10, 2014 7:04 AM in response to Twitcheye

So, I tried to modify your suggested app by inserting another 'Label Finder Items' action as the next to the last one and set the color in that action to the one that I want to remove

Basically what you did is right but instead of setting the color to the color you want to remove set it to remove the label entirely then set it to the label you want.

Reply

Jul 10, 2014 4:27 PM in response to Frank Caggiano

Hi, Frank,


I have tried that, as well. It gave me the same result as described above.


Hi, dwbrecovery,


As I mentioned in the beginning, I am no scripter, but those lines you suggested look to me like AppleScript, not Automator app? I will try your method and will post the result tomorrow.


I will also try to use iKey to create a shortcut that would give me an action I want, although I'm not sure whether iKey can do batch-processing... For now, thanks to all of you!

Reply

Jul 10, 2014 7:27 PM in response to Twitcheye

Wiht the change in Mavericks to the way files get labeled and adn the addition of tags it seems that tagging with Automator (and possibly Applescript) does not work as it use to.


I tired a few things out with Automator and got basically the same results you are seeing. Hopefully you will have better luck doing this with Applescript.


regards

Reply

Jul 10, 2014 11:12 PM in response to Twitcheye

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"}.



-- SCRIPT 1
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 -- 0..7

set_label_in_tree(lb, d)

on set_label_in_tree(lb, d)
    (*
        integer lb : label index (0..7)
        alias d : start directory alias
        
        * This handler set label index of files that reside in direcotry tree rooted at d
            and have name extension in extension_list to lb
    *)
    script o
        property extension_list : {"flac"} -- target extensions
        property dd : {d}
        property aa : {}
        property bb : {}
        property ee : {}
        
        repeat while (count my dd) > 0
            -- shift dd
            set d to my dd's item 1
            set my dd to my dd's rest
            
            -- retrieve files, extensions and folders in d
            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
            
            -- set label of files filtered by extension
            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
            
            -- push directories in d to dd
            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
-- END OF SCRIPT 1



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).



-- SCRIPT 2
(*
    requires OSX 10.6 or later
*)
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 -- 0..7
set_label_in_tree(lb, d)

on set_label_in_tree(lb, d)
    (*
        integer lb : label index (0..7)
        alias d : start directory alias
        
        * This handler set label index of files that reside in direcotry tree rooted at d
            and have name extension in EXTS to lb
    *)
    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
-- END OF SCRIPT 2


Both scripts are tested under 10.6.8. Not sure it works well under 10.9.


Hope this may help,

H

Reply

Jul 11, 2014 1:04 AM in response to Hiroto

Oops.


SCRIPT 2 needs correction. The line:


re_exts = %r/\\.#{EXTS.join('|')}\\z/



should have been:


re_exts = %r/\\.(#{EXTS.join('|')})\\z/



(Wrong code won't surface unless you define multiple extensions in EXTS.)


Here's the corrected script. Sorry for any confusions.


Regards,

H



-- SCRIPT 2 (CORRECTED)
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 -- 0..7
set_label_in_tree(lb, d)

on set_label_in_tree(lb, d)
    (*
        integer lb : label index (0..7)
        alias d : start directory alias
        
        * This handler set label index of files that reside in direcotry tree rooted at d
            and have name extension in EXTS to lb
    *)
    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
-- END OF SCRIPT 2 (CORRECTED)
Reply

Jul 11, 2014 6:44 AM in response to Twitcheye

I guess I did a poor job of explaining how this Automator workflow performs in my first post. As Frank pointed out, if you want to remove either the outer and/or inner tag color, you press the x in the appropriate tag color panel when it appears. For instance, you may like the outer tag color, but want to change the inner matched files and sub-folder tag color. You will have to run the Automator workflow twice.


First pass (remove inner tag colors)

  1. Keep the same color on the first tag panel
  2. Remove (select the x) all inner tag colors


Second pass (change inner tag colors)

  1. Keep the same color on the first tag panel
  2. Choose the new inner tag color for the inner sub-folder/matched file content.
Reply

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How can I tell the Finder to change the labels...

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.