PDA

View Full Version : rename Files




thriftinkid
Jul 30, 2009, 08:51 PM
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 automaor 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.



angelwatt
Jul 30, 2009, 09:22 PM
I'd personally use Name Mangler (http://www.manytricks.com/namemangler/).

mysterytramp
Jul 31, 2009, 07:13 AM
Automator was made for stuff like this ...

Just use the Rename Finder Items workflow, choose Replace Text, search for space, replace with underscore. Save it as an app and you're done. You could do it in AppleScript but that adds a layer of complexity that just isn't worth it.

mt

thriftinkid
Jul 31, 2009, 09:56 AM
Hey Guys I have the following ruby script. But when I try and run it through applescript (Which I need to to) I get one of the following two errors:

Ruby Script:

#!/usr/bin/env ruby -w

if ARGV.size != 1
puts "USAGE: Supply a folder path to files to rename."
exit
end

dir_path = ARGV[0]
DIR_TO_IGNORE = ['.', '..', '.DS_STORE']

(Dir.entries(dir_path) - DIR_TO_IGNORE).each do |file_name|
begin
file_path = "#{dir_path}#{file_name}"
u_name = file_name.downcase.gsub(/\s/, '_')
new_name = "#{dir_path}#{u_name}"
File.rename(file_path, new_name)
rescue
puts "Error renaming: #{file_name}"
end
end

Save Ruby as rename_files.rb to Movies folder


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

Error Message:

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

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


Error Message:

[B]
The variable MyFolder is not defined.


Any idea why this isn't calling the ruby script.

mysterytramp
Jul 31, 2009, 10:30 AM
I believe that:

/path/to/rename_files.rb

is a placeholder. You should replace it with the actual path of the file you want to rename.

and

[b]:

Is there a file "MyFolder"?

mt

macfaninpdx
Jul 31, 2009, 11:41 AM
Are you literally using "/path/to/rename_files.rb" here?
set thePath to ((path to movies folder as text) & "MyFolder")
do shell script "/usr/bin/ruby '/path/to/rename_files.rb' " & thePath

If so, you need to change it to the actual path where your rename_files.rb is. As in:
set thePath to ((path to movies folder as text) & "MyFolder")
do shell script "/usr/bin/ruby '~/Movies/rename_files.rb' " & thePath

thriftinkid
Jul 31, 2009, 11:49 AM
that did it thanks. I don't know anything about ruby scripting.