No internet connection
  1. Home
  2. How to

SF To Change Color Tags in Finder

By Blake Bunzel @Blake_Bunzel
    2021-09-23 19:36:50.403Z

    Hello,

    Was wondering if SF can add/remove/change the color tags in finder for files/folders and how best to approach this if so.

    • 6 replies
    1. samuel henriques @samuel_henriques
        2021-09-23 19:58:56.488Z

        hello Blake,

        I'm using this function, and made a package here, https://soundflow.org/store/color-pt-clip-and-finder. I made it a while ago, not sure if everything is working, I made a different version but didn't have the chance to change it in the store. If you like this one, I'll try to get it there.

        function setColorAppleScript(color) {
            sf.system.execAppleScript({
                script:
                    `
        property unset : 0
        property orange : 1
        property red : 2
        property yellow : 3
        property blue : 4
        property purple : 5
        property green : 6
        property grey : 7
        
        tell application "Finder"
               repeat with f in selection as alias list
                       set label index of f to ${color}
               end repeat
        end tell
        `
            });
        }
        
        
        setColorAppleScript("orange") // name colors or unset
        
        1. BBlake Bunzel @Blake_Bunzel
            2021-09-23 20:08:47.615Z

            This is excellent Samuel and works very well! It currently seems to either label the file/folder with one color instead of adding.
            Wondering if you know of a way for it to stack colors as well as maybe toggle the color on/off?

            1. samuel henriques @samuel_henriques
                2021-09-23 20:26:15.465Z

                Hey Blake, thank you.
                I just found this on forums discussing appleScript, and managed to adapt it, I really know nothing about appleScript, I can hardly manage. But it's worth searching, I'm sure there is a way.

                1. BBlake Bunzel @Blake_Bunzel
                    2021-09-23 21:07:05.153Z

                    Awesome! Thanks again Samuel!

                    1. Dustin Harris @Dustin_Harris
                        2024-04-05 14:08:38.907Z

                        I ended up making this script yesterday to do just that:

                        function getCurrentTags({ filePath }) {
                            const script = `use framework "Foundation"
                        use scripting additions
                        
                        -- Hardcoded file path
                        set filePath to "${filePath}"
                        
                        -- Retrieve tag names for the specified file
                        set tagNames to retrieveTagNamesForFile(filePath)
                        
                        -- Return the tag names
                        if tagNames ≠ missing value then
                            return tagNames
                        else
                            return "No tags found for file " & filePath
                        end if
                        
                        on retrieveTagNamesForFile(filePath)
                            try
                                -- Create NSURL object from file path
                                set fileURL to current application's |NSURL|'s fileURLWithPath:filePath
                                
                                -- Retrieve tag names for the file
                                set {theResult, theTags} to fileURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
                                
                                if theTags ≠ missing value then
                                    return theTags as list
                                else
                                    return missing value
                                end if
                            on error errMsg
                                return "Error: " & errMsg
                            end try
                        end retrieveTagNamesForFile`
                        
                            const text = sf.system.execAppleScript({ script }).result
                            const tags = text.split(",").map(t => t.replace("\n", "").trim())
                            return tags
                        
                        }
                        
                        /**
                         * @param {string} filePath
                         * @param {string[]} tags
                         */
                        function applyTags(filePath, tags) {
                        
                            let tagString = ""
                            tags.forEach(tag => tagString = tagString + `"${tag}",`)
                            tagString = tagString.slice(0, -1); //remove trailing comma
                        
                            const script = `use framework "Foundation"
                        use scripting additions
                        
                        -- Hardcoded file path
                        set filePath to "${filePath}"
                        
                        -- Replace tags for the hardcoded file path
                        my replaceTags:{${tagString}} forPath:filePath
                        
                        on replaceTags:tagList forPath:p
                            set u to current application's |NSURL|'s fileURLWithPath:p
                            u's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
                        end replaceTags:forPath:`
                        
                            sf.system.execAppleScript({ script })
                        
                        }
                        
                        
                        /**
                         * @param {{colour: "Red"|"Orange"|"Blue"|"Yellow"|"Green"|"Purple"|"Gray"}} args 
                         * */
                        function toggleTagColour({ colour }) {
                        
                            const paths = sf.ui.finder.selectedPaths;
                        
                            paths.forEach(filePath => {
                                const currentTags = getCurrentTags({ filePath });
                                const colorTagPresent = currentTags.includes(colour)
                                const tagsToApply = (colorTagPresent) ? currentTags.filter(tag => tag !== colour) : [...currentTags, colour];
                                applyTags(filePath, tagsToApply)
                            })
                        }
                        
                        
                        toggleTagColour({ colour: "Orange" })
                        
                2. In reply toBlake_Bunzel:
                  Jess Davy @Jess_Davy
                    2022-10-25 08:29:28.612Z

                    Hi, is it possible to use custom tags?
                    I have tags "DONE" "IN PROGRESS" "To Do" "On the Disk" and "Archived".

                    How would I go about writing the script to tag files using these custom tags?