Here is a revised version.
(2) it reactivate the instruction texting if we press the Cancel button.
On a system running in English this instruction is never executed because the default Cancel button is named "Cancel".
But it's not true on non English system.
For instance, on French one, the default Cancel button is named "Annuler" so, with a dialog using a button named "Cancel", triggering this one doesn't exit gracefully (error number -128) but returns button_returned = "Cancel" which we must filter.
It would be fine to drop two deprecated functions : info for and list folder but it's an other story.
(*
Trim File Names -- Modified for Mavericks
This script is designed to trim the name of specific folders in the front window of the desktop.
If no folder windows are open, the script will affect folders on the desktop.
Copyright © 2001–2007 Apple Inc.
You may incorporate this Apple sample code into your program(s) without
restriction. This Apple sample code has been provided "AS IS" and the
responsibility for its operation is yours. You are not permitted to
redistribute this Apple sample code as "Apple sample code" after having
made changes. If you're going to redistribute the code, we require
that you make it clear that the code was descended from Apple sample
code, but that you've made changes.
*)
-- The following line was disabled due to a Menu Manager bug. No longer needed !
set the source_folder to (choose folder with prompt "Pick the folder containing the folders to rename:")
try
tell application "Finder" to set the source_folder to (folder of the front window) as alias
on error -- no open folder windows
set the source_folder to path to desktop folder as alias
end try
repeat
display dialog "Text to trim from every file name:" default answer "" buttons {"Cancel", "Trim Start", "Trim End"}
# copy the result as list to {button_pressed, text_to_trim} # AWFUL INSTRUCTION
set {text returned:text_to_trim, button returned:button_pressed} to result # Thanks to Nigel GARVEY
if the button_pressed is "Cancel" then return "user cancelled" # Useful when the hosting system doesn't run in English !
if the text_to_trim is not "" then exit repeat
end repeat
set the character_count to the number of characters of the text_to_trim
set the item_list to list folder source_folder without invisibles
set source_folder to source_folder as string
repeat with i from 1 to number of items in the item_list
set this_item to item i of the item_list
set this_item to (source_folder & this_item) as alias
set this_info to info for this_item
if folder of this_info is false then
set the current_name to the name of this_info
if the button_pressed is "Trim Start" then
if the current_name begins with the text_to_trim then
set the new_name to (characters (the character_count + 1) thru -1 of the current_name) as string
set_item_name(this_item, new_name)
end if
else
if the current_name ends with the text_to_trim then
set the new_name to (characters 1 thru -(the character_count + 1) of the current_name) as string
set_item_name(this_item, new_name)
end if
end if
end if
end repeat
beep 2
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 {button_pressed, text_to_trim} # AWFUL INSTRUCTION
set {text returned:text_to_trim, button returned:button_pressed} to result # Thanks to Nigel GARVEY
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 {button_pressed, new_item_name} # AWFUL INSTRUCTION
set {text returned:text_to_trim, button returned:button_pressed} to result # Thanks to Nigel GARVEY
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