daily pastebin goal

Trim File Names AppleScript

daviesgeek Oct 30th, 2011 622 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (*
  2. Trim File Names
  4. This script is designed to trim the name of specific folders in the front window of the desktop.
  5. If no folder windows are open, the script will affect folders on the desktop.
  7. Copyright © 2001–2007 Apple Inc.
  9. You may incorporate this Apple sample code into your program(s) without
  10. restriction.  This Apple sample code has been provided "AS IS" and the
  11. responsibility for its operation is yours.  You are not permitted to
  12. redistribute this Apple sample code as "Apple sample code" after having
  13. made changes.  If you're going to redistribute the code, we require
  14. that you make it clear that the code was descended from Apple sample
  15. code, but that you've made changes.
  16. *)
  18. -- The following line is disabled due to a Menu Manager bug
  19. --set the source_folder to (choose folder with prompt "Pick the folder containing the folders to rename:")
  21. try
  22.         tell application "Finder" to set the source_folder to (folder of the front window) as alias
  23. on error -- no open folder windows
  24.         set the source_folder to path to desktop folder as alias
  25. end try
  26. repeat
  27.         display dialog "Text to trim from every file name:" default answer "" buttons {"Cancel", "Trim Start", "Trim End"} default button 2
  28.         copy the result as list to {the text_to_trim, the button_pressed}
  29.         --if the button_pressed is "Cancel" then return "user cancelled"
  30.         if the text_to_trim is not "" then exit repeat
  31. end repeat
  32. set the character_count to the number of characters of the text_to_trim
  33. set the item_list to list folder source_folder without invisibles
  34. set source_folder to source_folder as string
  35. repeat with i from 1 to number of items in the item_list
  36.         set this_item to item i of the item_list
  37.         set this_item to (source_folder & this_item) as alias
  38.         set this_info to info for this_item
  39.         if folder of this_info is false then
  40.                 set the current_name to the name of this_info
  41.                 if the button_pressed is "Trim Start" then
  42.                         if the current_name begins with the text_to_trim then
  43.                                 set the new_name to (characters (the character_count + 1) thru -1 of the current_name) as string
  44.                                 set_item_name(this_item, new_name)
  45.                         end if
  46.                 else
  47.                         if the current_name ends with the text_to_trim then
  48.                                 set the new_name to (characters 1 thru -(the character_count + 1) of the current_name) as string
  49.                                 set_item_name(this_item, new_name)
  50.                         end if
  51.                 end if
  52.         end if
  53. end repeat
  54. beep 2
  56. on set_item_name(this_item, new_item_name)
  57.         tell application "Finder"
  58.                 --activate
  59.                 set the parent_container_path to (the container of this_item) as text
  60.                 if not (exists item (the parent_container_path & new_item_name)) then
  61.                         try
  62.                                 set the name of this_item to new_item_name
  63.                         on error the error_message number the error_number
  64.                                 if the error_number is -59 then
  65.                                         set the error_message to "This name contains improper characters, such as a colon (:)."
  66.                                 else --the suggested name is too long
  67.                                         set the error_message to error_message -- "The name is more than 31 characters long."
  68.                                 end if
  69.                                 --beep
  70.                                 tell me to display dialog the error_message default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
  71.                                 copy the result as list to {new_item_name, button_pressed}
  72.                                 if the button_pressed is "Skip" then return 0
  73.                                 my set_item_name(this_item, new_item_name)
  74.                         end try
  75.                 else --the name already exists
  76.                         --beep
  77.                         tell me to display dialog "This name is already taken, please rename." default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
  78.                         copy the result as list to {new_item_name, button_pressed}
  79.                         if the button_pressed is "Skip" then return 0
  80.                         my set_item_name(this_item, new_item_name)
  81.                 end if
  82.         end tell
  83. end set_item_name
RAW Paste Data