Skip navigation

AppleScript or Automator to remove _ & - from filenames?

3432 Views 6 Replies Latest reply: Mar 9, 2013 9:18 AM by twtwtw RSS
downtownbarber Level 1 Level 1 (20 points)
Currently Being Moderated
Mar 6, 2013 10:28 AM

I'm trying to remove all the underscores and dashes from the filenames so they have a cleaner look to them. Searching for a place to post this question doesn't come up with anything for AppleScript or Automator so if this is the wrong place I'm sorry. I've tried getting direction from the Apple store with my onetoone subscription but no one knows how to use them. ThanksScreen Shot 2013-03-06 at 12.01.49 PM.png

MacBook Pro with Retina display, OS X Mountain Lion (10.8.2), Quad Core! Four is more...
  • danegeld Level 2 Level 2 (180 points)

    Try Name Mangler. You might also want to invest some time learning regular expressions. Name Mangler supports them and although very cryptic, they're very powerful and make replacing parts of filenames trivial.

  • pjdube Level 3 Level 3 (830 points)

    This is actually workable in automator.  Here is an automator workflow that I did, and it is a service, so all you have to do is right click the folder and select the service and it will do all the files in that folder.

     

    1a.jpg1b.jpg

    I have it set to loop 10 times. You can easily change this to however many files are in the folder.

     

     

    Now you might not have some of the actions in this workflow. You can get them here: http://www.automatedworkflows.com

     

    Good luck.

     

    Regards,

     

    Phil

  • pjdube Level 3 Level 3 (830 points)

    Ok cool. Well at least you figured out another solution!

     

    Yes, the key was Find and Replace in Text and entering in the replace part with a space. But it was tricky, as afterward this action you have to get rid of the spaces (so you don't produce just  "01 01 v2b AfterEffectsCS6.mp4", but instead would get "0101v2bAfterEffectsCS6.mp4"). That's what I had to figure out. The key is the use of variables and of course having all the automator actions available to you (there are actions that do not come with MAC which you would have to download separately: Find and Replace in Text and Get Names of Finder Items)  in order for it to actually run on your MAC.

     

    Automator is easy, it is meant for those that do not know scripting language and could easily put together a workflow that handles a lot of repetative tasks. I know HTML and CSS but have not dived into the scripting language of OSX yet and find this to be my savior for the time being, as I only do this on my spare time (I like to help others out!).

     

    Do let me know if you have anything else you want to automate. I can easiy help you out.

     

    Regards,

     

    Phil

  • VikingOSX Level 5 Level 5 (4,700 points)

    In Automator, I chose a Folder Action. I then used Library > Utilities > Run as Shell Script, and dragged that over into the workflow. I chose Pass input: as arguments because we want it to process every file dropped on the folder. I removed all the default Bash boilerplate loop and replaced it with a short script.

     

    The script will test that these are regular files (not executables,etc.) and then there are two character removal choices (one needs to remain commented).

     

    mv $f `echo $f | sed -e 's/[^0-9a-zA-Z\.]//g'`
    

     

    Renames the original filename without any character that is not a number, lowercase or uppercase letter, or a period from the output filename. The back tics ‘`’ tell the shell to execute the contents and use the returned results as the second argument to the shell rename (mv) command.

     

    The second option:

     

    mv $f `echo $f | sed -e 's/[_-]//g'`
    

     

    Renames the original filename without any characters contained in the brackets. This fits your original request. This script, once named and made executable, can run standalone in the Terminal with the following syntax:

     

    trimit.sh *.txt *.mp4
    

     

    Screen Shot 2013-03-09 at 11.22.41 AM.png

  • twtwtw Level 5 Level 5 (4,580 points)

    You do this in applescript as follows:

     

    set theFiles to choose file with multiple selections allowed

     

    tell application "System Events"

              repeat with thisFile in the theFiles

                        set oldFileName to name of thisFile

     

                        set {oldTID, my text item delimiters} to {my text item delimiters, {"_", "-"}}

                        set fileNameBits to text items of oldFileName

     

                        set my text item delimiters to " "

                        set newFileName to fileNameBits as text

     

                        set my text item delimiters to oldTID

     

                        set name of thisFile to newFileName

     

              end repeat

    end tell

Actions

More Like This

  • Retrieving data ...

Bookmarked By (2)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.