Replace space with underscore

An AppleScript to replace spaces (‘ ‘) with underscore (‘_’) in names of files selected in the Finder. It does not do folders, but should be easy enough to modify so it can.

It does display a prompt with the file names as a list, so processing a mountain of files may not be ideal since AppleScript dialogs have the unwelcome habit of pushing the top (the only place you can grab an AppleScript dialog) and bottom (where the buttons are) past the edges of the screen when they contain enough text. Although ‘OK’ and ‘Cancel’ can still be activated by return and Command . respectively.

[UPDATE] Removed some redundant text in repeat routines, and switched to “my text item delimiters”.

(*
http://strawhousepig.net/
Portions of this script from:

Replace Text In Item Names
Copyright � 2001 Apple Computer, Inc.
*)

tell application "Finder" to set the_items to selection
set name_list to ""
repeat with i in the the_items
  set this_item to i as alias
  set this_info to info for this_item
  repeat 1 times
    if folder of this_info is true then
      exit repeat
      --set this_name to "(Folder) " & this_name
    end if
    set this_name to name of this_info & return
    set name_list to name_list & this_name
  end repeat
end repeat
if name_list is not "" then
  display dialog "Replace spaces with underscores in the following files?" & return & return & name_list
else
  display dialog "Please select files first."
  return
end if

repeat with i in the the_items
  set this_item to i as alias
  set this_info to info for this_item
  repeat 1 times
    if folder of this_info is true then
      exit repeat
    end if
    set the current_name to the name of this_info
    -- replace target string using delimiters
    set my text item delimiters to " "
    set the text_item_list to every text item of the current_name
    set my text item delimiters to "_"
    set the new_item_name to the text_item_list as string
    my set_item_name(this_item, new_item_name)
  end repeat
end repeat

on set_item_name(this_item, new_item_name)
  tell application "Finder"
    --activate
    set the parent_container_path to (the container of this_item) as text
    if not (exists item (the parent_container_path & new_item_name)) then
      try
        set the name of this_item to new_item_name
      on error the error_message number the error_number
        if the error_number is -59 then
          set the error_message to "This name contains improper characters, such as a colon (:)."
        else --the suggested name is too long
          set the error_message to error_message -- "The name is more than 31 characters long."
        end if
        --beep
        tell me to display dialog the error_message default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
        copy the result as list to {new_item_name, button_pressed}
        if the button_pressed is "Skip" then return 0
        my set_item_name(this_item, new_item_name)
      end try
    else --the name already exists
      --beep
      tell me to display dialog "This name is already taken, please rename." default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
      copy the result as list to {new_item_name, button_pressed}
      if the button_pressed is "Skip" then return 0
      my set_item_name(this_item, new_item_name)
    end if
  end tell
end set_item_name

2 thoughts on “Replace space with underscore

  1. Pingback: I Want to Automate ALL the Things – Podfeet Podcasts

Leave a Reply

Your email address will not be published. Required fields are marked *