Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

chris_jg

macrumors newbie
Original poster
Jan 26, 2020
1
0
UK
Hi,

I thought this might be a simple, common problem but I've tried searching the forums here and looked at previous posts using google but can't seem to find an answer that helps me.
I have a library of thousands of music files and their corresponding stems some of which have repeated words in their file name.
How can I use automator to remove anything that is a repeated word in the file name?

Before:
tr43-Apocalyptic_Dreams_Stem1_Drums_Stem1_Drums.aiff
tr44-Apocalyptic_Dreams_Stem2_Choir_Stem2_Choir.aiff
tr45-Apocalyptic_Dreams_Stem3_Percussion_Stem3_Percussion.aiff
tr46-Apocalyptic_Dreams_Stem4_Strings_Brass_Stem4_Strings_Brass.aiff
tr133-Apocalyptic_Dreams_30_Second_30_Second.aiff

After:
tr43-Apocalyptic_Dreams_Stem1_Drums.aiff
tr44-Apocalyptic_Dreams_Stem2_Choir.aiff
tr45-Apocalyptic_Dreams_Stem3_Percussion.aiff
tr46-Apocalyptic_Dreams_Stem4_Strings_Brass.aiff
tr133-Apocalyptic_Dreams_30_Second.aiff

TIA
 

Red Menace

macrumors 6502a
May 29, 2011
578
226
Colorado, USA
There aren't any default actions in Automator to do this, but a script can be used to rename the items. For example, the following can be used as a rename action - add a Run AppleScript action and replace the default script:

Code:
    use AppleScript version "2.4" -- Yosemite (10.10) or later
    use framework "Foundation"
    use scripting additions

    on run {input, parameters}
        set output to {}
        repeat with anItem in input
            set {theContainer, theName, theExtension} to (getNamePieces from anItem)
            set newName to (removeDuplicates from theName) & theExtension
            try
                if newName is not equal to (theName & theExtension) then tell application "Finder" to set name of anItem to newName
                set end of output to (theContainer & newName) as alias -- success
            on error errmess number errnum -- skip errors (duplicate names, etc)
                display alert "Item Skipped (error " & errnum & ")" message errmess
            end try
        end repeat
        return output
    end run

    to removeDuplicates from someText given delimiters:delimiters : "_" -- default
        set tempTID to AppleScript's text item delimiters -- save current
        set AppleScript's text item delimiters to delimiters -- where to break the text apart
        set orderedSet to (current application's NSOrderedSet's orderedSetWithArray:(text items of someText))
        set textWithoutDuplicates to (((orderedSet's array()) as list) as text)
        set AppleScript's text item delimiters to tempTID -- restore
        return textWithoutDuplicates
    end removeDuplicates

    to getNamePieces from somePath
        tell application "System Events" to tell disk item (somePath as text)
            set _container to path of container
            set {_name, _extension} to {name, name extension}
        end tell
        if _extension is not "" then
            set _name to text 1 thru -((count _extension) + 2) of _name
            set _extension to "." & _extension
        end if
        return {_container, _name, _extension}
    end getNamePieces

I would recommend starting with small batches to make sure the script does what you are wanting.
 

t0mat0

macrumors 603
Aug 29, 2006
5,473
284
Home
Isn't exactly what you're asking, but have you looked at something like Hazel
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.