I deal with a lot of files (mostly images and video) with quite a few different words and phrases that I want to remove from the filenames. I keep finding Automator tutorials or AppleScripts to completely rename or replace X with Y in filenames, but what I really need is an action or script I can run from Hazel that just deletes X, Y, Z, A, B, C, and whatever else I might later add to the list of words and phrases to remove.

For instance, I want to say something like search for "X", "Y", "Z", "A", "B", "C", "this phrase", "-)"

and delete it or replace it with "".

Could anyone provide me such a script or instruction on what Automator might need to do it?

Thank you.

You could use a script like this:

--Select some files in the Finder first. Then run this script.

set oldDelims to AppleScript's text item delimiters
set the_strings_to_strip to {"ee", "shot", "Z", "at"}

tell application "Finder"
    set the_files to the selection
    --
    repeat with a_file in the_files
        set the_name to name of a_file
        repeat with I from 1 to count of the_strings_to_strip
            set AppleScript's text item delimiters to item I of the_strings_to_strip
            set the_text_items to text items of the_name
            set AppleScript's text item delimiters to ""
            set the_name to the_text_items as string
        end repeat
        set the name of a_file to the_name
    end repeat
    --
    set AppleScript's text item delimiters to oldDelims
end tell

The idea is, you have your set of characters or phrases that you want to strip. Then, you use them as "text item delimiters." Normally text item delimiters are "". So, when you have a word like "cat" the text items are "c", "a", and "t". But, if you SET AppleScript's text item delimiters to "a" instead of "" you will only have TWO text items in the word "cat": "c", and "t". Then, when you set the text item delimiters back to "", and then you concatenate the text items, you've essentially done a search and replace: replacing your string with "".

I tried this on a bunch of screen shot files and it worked fine.

Notice that at the end, I set AppleScript's text item delimiters back to whatever they were when the script started. It would be a good bet that they were "" but just in case, I record what they were, and restore them at the end, in case they are/were something other than "". No point guessing when you can know for sure.

Let's see if this helps. If I misunderstood your issue and/or example, forgive me, but if I did, other people probably have too. Thus, if that is the case, please provide us more realistic examples. If won't hurt our feelings to see real file names before and after the changes that you want to make.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.