property nameString : ""
property extension : ""
tell application "Finder" to set theFiles to selection
if number of items in theFiles is greater than 0 then
display dialog "Please enter the new name for the files:" default answer nameString
set nameString to text returned of the result
display dialog "Enter a file extension, or leave blank for none:" default answer extension
set extension to text returned of the result
if the nameString is not "" then
repeat with i from 1 to number of items in theFiles
set thisItem to item i of theFiles as alias
set thisInfo to info for thisItem
set currentName to name of thisInfo
if extension is "" then
set extensionString to ""
else
set extensionString to "." & extension
end if
if i is 1 then
set newName to nameString & extensionString
else
set newName to nameString & " " & (i as string) & extensionString
end if
my setItemName(thisItem, newName)
end repeat
end if
else
display dialog "No items are selected" buttons "OK" default button 1
end if
on setItemName(thisItem, newName)
tell application "Finder"
set parentFolder to (container of thisItem) as text
if not (exists item (parentFolder & newName)) then
try
set name of thisItem to newName
on error errorMessage number errorNumber
tell me to display dialog ("An error occured when renaming to" & newName & ". The file will not be renamed.") buttons "OK" default button 1
return 0
end try
else --the name already exists
tell me to display dialog ("A file named" & newName & " already exists. The file will not be renamed.") buttons "OK" default button 1
return 0
end if
end tell
end setItemName