Rename Files

Hey guys. I want to convert all the file names within a folder to the following:

My First Movie.mov

to

my_first_movie.mov

I know you can do this easily within automator with the “make web friendly” action.

I have an automator workflow that uses way to many programs to accomplish my goal, so I am trying to write as much as I can in applescript form. I would like to stay out of automator because I don’t find it very dependable.

Does anyone know how I would do this using applescript, or do you know a shell command to use within my script?

I’m assuming you would just use the the line “do shell command…”, but I’m not sure.

Thanks Guys.

Save this as rename_files.rb

Call it with this.

do shell script "/usr/bin/ruby '/path/to/rename_files.rb' '/path/to/folder/'"

Thanks so much. Is there anywhere in particular I have to store the file “rename_files.rb”?

Also, in looking at the shell script, it says puts “USAGE: Supply a folder path to files to rename.”. Does a normal applescript command work here? Such as:

puts "USAGE: ((path to movies folder as text) & MyFolder)"

Not really. If you are going to use it a lot then put it someplace it won’t get deleted and where you will remember it.

The script above is a Ruby script. You cannot use ‘puts’ inside AppleScript.

You can get a POSIX path by saying


set thePath to quoted form of (POSIX path of ((path to movies folder as text) & MyFolder))
do shell script "/usr/bin/ruby '/path/to/rename_files.rb' " & thePath

Do I have to change it in the ruby script as well? Or should the ruby script stay exactly as you have posted it?

Yea, the Ruby script stays the way it is.

To see what the Ruby script does when you do not supply a path do this.


set theResult to do shell script "/usr/bin/ruby '/path/to/rename_files.rb' "

theResult will now contain

This is the error message when no folder path is supplied.

I saved the “.rb” file to my desktop, and ran the following 2 scripts and got the following 2 errors:


Applescript Error:

set thePath to ((path to movies folder as text) & "MyFolder")
do shell script "/usr/bin/ruby '/path/to/rename_files.rb' " & thePath

/usr/bin/ruby: No such file or directory – /path/to/rename_files.rb (LoadError)

and

set thePath to quoted form of (POSIX path of ((path to movies folder as text) & Upload))
do shell script "/usr/bin/ruby '/path/to/rename_files.rb' " & thePath


Applescript Error:

The variable MyFolder is not defined.

Any ideas?

Is it my path to the ruby script that is wrong or the path to my folder?

Ok, change the /path/to/rename_files.rb to the actual path. This is just a place holder.


set rubyFile to quoted form of POSIX path of ((path to desktop as Unicode text) & "rename_files.rb")
do shell script "/usr/bin/ruby " & rubyFile & space & thePath

Make sure to use quoted form of POSIX path of when creating the path. This is the type of path the Shell uses.

It did not find ruby. If returned “/usr/bin/ruby”. Am I maybe missing some software? I’m running tiger 10.4.11.

I ran the following script and got the following return:

set thePath to quoted form of (POSIX path of ((path to movies folder as text) & "myFolder"))
set rubyFile to quoted form of POSIX path of ((path to desktop as Unicode text) & "rename_files.rb")
do shell script "/usr/bin/ruby " & rubyFile & space & thePath

Returned:

“Error renaming: .DS_Store”

Well my genious just realized I didn’t put a file in the folder. So I place one in there and ran it again. But got a similar return:


set thePath to quoted form of (POSIX path of ((path to movies folder as Unicode text) & "MyFolder"))
set rubyFile to quoted form of POSIX path of ((path to desktop as Unicode text) & "rename_files.rb")
do shell script "/usr/bin/ruby " & rubyFile & space & thePath


Returned:


“Error renaming: .DS_Store
Error renaming: My First Movie.mov”

Change this line in the Ruby file. The .DS_Store has all caps in the current Ruby file.

Ok, did that. I’m a little closer. It got rid of the “.ds” error, but the second error still remains. I know nothing of ruby. I copied your ruby script into text editor and saved it out under your file name. Is that the correct way to do that, or should I be saving that file in a different manner?

set thePath to quoted form of (POSIX path of ((path to movies folder as Unicode text) & "myFolder"))
set rubyFile to quoted form of POSIX path of ((path to desktop as Unicode text) & "rename_files.rb")
do shell script "/usr/bin/ruby " & rubyFile & space & thePath


Returned:

“Error renaming: My First Movie.mov”

Try this one.

That is correct as long as you are using a text editor like TextMate, BBEdit, TextRangler, etc. If you are using TextEdit make sure that you turn the document into a Plain Text doc. Format->Make Plain Text. If it is already plain text then the menu will display Format->Make Rich Text.

Eh, I got a bigger list on my return this time:

set thePath to quoted form of (POSIX path of ((path to movies folder as Unicode text) & "myFolder"))
set rubyFile to quoted form of POSIX path of ((path to desktop as Unicode text) & "rename_files.rb")
do shell script "/usr/bin/ruby " & rubyFile & space & thePath

Returned:

“here
Error renaming: .
here
Error renaming: …
here
Error renaming: .DS_Store
here
Error renaming: My First Movie.mov”

I made a minor change to the last script. Copy it and try again. It will give a better error message.