I have got a handler that renames images, spots duplicates and prompts for a new name. This has always worked fine on OS 10.4.11 but does not work as intended on OS 10.5.2. The script will rename providing they are all unique. If the finder is asked to rename with a duplicate it should error out of the try command but does not. It simply leaves the images with the original filenames
on nameselected(n_ame, i_mage)
tell application "Finder"
set x to info for alias (c_hosen & i_mage as Unicode text)
set ext to name extension of x
set ext to "." & ext
try
set name of alias (c_hosen & i_mage as Unicode text) to n_ame & ext
--AT THIS POINT WITH A DUPLICATE IT PROCEED TO ON ERROR BUT SIMPLY IGNORES IT. THE EVENT LOG APPEARS TO SHOW IT HAS RENAMED THE IMAGE.
on error
set dupIMAGE to (c_hosen & i_mage as Unicode text)
reveal dupIMAGE
display dialog n_ame & " is a duplicate. Please put in correct name" default answer n_ame
set the theResult to text returned of the result
try
set name of alias (c_hosen & i_mage as Unicode text) to theResult & ext
set the_message to n_ame & " is a duplicate name. Name set to " & theResult & " Name images script"
my createlog(the_message)
on error
set the_message to n_ame & " is a duplicate name. Name images script"
my createlog(the_message)
activate
display dialog n_ame & " is a duplicate name. Please check your data" buttons {"Cancel"}
end try
end try
end tell
end nameselected
thanks in advance
Model: 12 macs now…
AppleScript: 2.2
Browser: Safari 523.15
Operating System: Mac OS X (10.5)
c_hosen is the path to folder where the images are. i_mage is the name of the image. Remember it on bombs on duplicates!? If the filename is is OK it renames them as normal
very strange. On my machine (10.5.2 PPC) the same behavior
here a work around which checks first if the name (file) exists
on nameselected(n_ame, i_mage)
tell application "Finder"
set theFile to c_hosen & i_mage as alias
set ext to "." & name extension of theFile
try
c_hosen & n_ame & ext as alias -- throws error if file exists
reveal theFile
set newName to text returned of (display dialog n_ame & " is a duplicate. Please put in correct name" default answer n_ame) & ext
try
c_hosen & newName as alias -- throws error if file exists
set the_message to n_ame & " is a duplicate name. Name images script"
my createlog(the_message)
activate
display dialog n_ame & " is a duplicate name. Please check your data" buttons {"Cancel"}
on error
set name of theFile to newName
set the_message to n_ame & " is a duplicate name. Name set to " & newName & " Name images script"
my createlog(the_message)
end try
on error
set name of theFile to n_ame & ext
end try
end tell
end nameselected
Your solution does not quite work the script below works for me. I am not happy that i have to build into a script the failure of the finder to recognize a duplicate filename. I know that AS can be somewhat flakey but this seems a basic issue that should be addressed
on nameselected(n_ame, i_mage)
tell application "Finder"
set theFile to (c_hosen & i_mage as text) as alias
set ext to "." & name extension of theFile
set name of alias (c_hosen & i_mage as text) to n_ame & ext
try
(c_hosen & i_mage as text) as alias -- throws error if file exists
reveal theFile
set newName to text returned of (display dialog n_ame & " is a duplicate. Please put in correct name" default answer n_ame) & ext
try
c_hosen & newName as alias -- throws error if file exists
set the_message to n_ame & " is a duplicate name. Name images script"
my createlog(the_message)
activate
display dialog n_ame & " is a duplicate name. Please check your data" buttons {"Cancel"}
on error
set name of theFile to newName
set the_message to n_ame & " is a duplicate name. Name set to " & newName & " Name images script"
my createlog(the_message)
end try
on error
set name of theFile to n_ame & ext
end try
end tell
end nameselected