rename Files

Discussion in 'Mac Programming' started by thriftinkid, Jul 30, 2009.

  1. macrumors regular

    Joined:
    Mar 24, 2008
    #1
    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.
     
  2. Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
  3. macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #3
    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
     

    Attached Files:

  4. thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #4
    Ruby Script and Applescript not being friends

    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:

    Code:
    #!/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


    Code:
    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)

    Code:
    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:



    The variable MyFolder is not defined.



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

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #5
    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

    :

    Is there a file "MyFolder"?

    mt
     
  6. macrumors regular

    Joined:
    Mar 6, 2007
    #6
    Are you literally using "/path/to/rename_files.rb" here?
    Code:
    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:
    Code:
    set thePath to ((path to movies folder as text) & "MyFolder")
    do shell script "/usr/bin/ruby '~/Movies/rename_files.rb' " & thePath
     
  7. thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #7
    that did it thanks. I don't know anything about ruby scripting.
     

Share This Page