6 Replies Latest reply: Mar 9, 2013 9:18 AM by twtwtw
downtownbarber Level 1 Level 1 (20 points)

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...
  • 1. Re: AppleScript or Automator to remove _ & - from filenames?
    danegeld Level 2 Level 2 (185 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.

  • 2. Re: AppleScript or Automator to remove _ & - from filenames?
    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

  • 3. Re: AppleScript or Automator to remove _ & - from filenames?
    downtownbarber Level 1 Level 1 (20 points)

    I posted the question when I was in the Apple store about to start a onetoone because the week before I asked this question and no one knew. My trainer saw the post and suggested an app called "a better finder renamer" and I bought it, used it, and it works perfectly. Unfortunately it's $20, but it really is useful.

    I was looking at your workflow and am now wondering if the "find and replace text" is all that's necessary for what I was looking to do? That's all the better finder renamer really did, I dragged and dropped the files into the app and chose to remove the underscore and replace w/space, then did the same with the dashes.  I'm not nearly familiar with Automator as I want to be but it looks like it might be that easy. What do you think? Thank you pjd

  • 4. Re: AppleScript or Automator to remove _ & - from filenames?
    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

  • 5. Re: AppleScript or Automator to remove _ & - from filenames?
    VikingOSX Level 5 Level 5 (5,490 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

  • 6. Re: AppleScript or Automator to remove _ & - from filenames?
    twtwtw Level 5 Level 5 (4,690 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