G’day from Oz
To help someone who wanted to re-name photos of the same name, I’ve written a script to do the job.
Trouble is, it’s limited to items in folders one level deep
I imagine that it’s possible to write the algorythm to include re-iterated multiple levels of folders, but it’s beyond my limited Applescript knowledge.
Would anyone care to give me some pointers please?
Regards
Santa
(* Item renamer
Use as a Folder Action
When items (except for applications) are dropped on the folder,
they are renamed and stored in the enclosed folder '* Re-named Items'.
Version 2.0 added the ability to rename files in folders one
layer deep, and keep them in the folders.
When 'Property AlwaysReName : false' is set, then items are only
re-named if an item of the same name already exists in the folder
'*Re-named Items', or exists in a folder inside of '*Re-named Items'
If you ALWAYS want to re-name EVERY item, then set the
'Property AlwaysReName : true'
By Santa
Version 2.1
*)
property AlwaysReName : false
property FolderName : "* Re-named Items"
property FirstPass : true
global AllTheExistingNames
global AllTheNewNames
on adding folder items to this_folder after receiving added_items
set SelectShiftedItems to {}
try
tell application "Finder"
set temp to name of item 1 of added_items -- this bit ensures creation of new folder doesn't create loops
if temp is not FolderName then
if not (exists folder FolderName of this_folder) then
make new folder at this_folder with properties {name:FolderName}
end if
set the destination_folder to folder FolderName of this_folder as alias
set temp to every item of folder FolderName of this_folder
set AllTheExistingNames to my AddUpTheItems(temp)
set temp to added_items
set AllTheNewNames to my AddUpTheItems(temp)
(* We can't use 'Repeat with TheItem in added_items', because
we're altering the added_items by name changing. Instead we use a counting loop. *)
repeat with Counter from 1 to number of items in added_items
set TheItem to item Counter of added_items
if kind of (info for TheItem as alias) is not in {"Folder", "Application"} then
set NewFileName to my GoAndRename(TheItem)
--set CheckFolder to container of TheItem
if FolderName is not in (container of TheItem as string) then
set the name of TheItem to NewFileName
move document file NewFileName of this_folder to destination_folder
set SelectShiftedItems to SelectShiftedItems & NewFileName
end if
else
-- If we've struck a folder, re-name, move it, then rename contents
if kind of (info for TheItem as alias) is "Folder" then
-- Check if we need to rename the folder
-- but first, check if folder is still in re-naming folder?
if FolderName is not in (container of TheItem as string) then
set tempName to my GoAndRename(TheItem)
set the name of TheItem to tempName
move TheItem to destination_folder
-- Always rename them? then refresh the Existing name list
if AlwaysReName then
set temp to every item of folder FolderName of this_folder
set AllTheExistingNames to my AddUpTheItems(temp)
end if
set SelectShiftedItems to SelectShiftedItems & name of TheItem
set CycleContents to items of folder tempName in folder destination_folder
-- This bit here is a problem, it's restricted to one level deep
repeat with FolderCounter from 1 to (number of items in CycleContents)
set TheFolderItem to item FolderCounter of TheItem
if kind of (info for TheFolderItem as alias) is not in {"Folder", "Application"} then
set NewFileName to (my GoAndRename(TheFolderItem))
set the name of (TheFolderItem) to NewFileName
end if
end repeat
end if
end if
end if
end repeat
if SelectShiftedItems ≠{} then
open destination_folder
select (every item of destination_folder whose (name is in SelectShiftedItems))
end if
end if
end tell
end try
end adding folder items to
on AddUpTheItems(temp)
try
tell application "Finder"
set AllTheNames to {}
repeat with TheExistingItems in temp
if kind of (info for TheExistingItems as alias) is "Folder" then
if name of TheExistingItems ≠FolderName then
set AllTheNames to AllTheNames & name of TheExistingItems
repeat with ExistingFolderItems in TheExistingItems
if kind of (info for ExistingFolderItems as alias) is not in {"Folder", "Application"} then
set end of AllTheNames to name of (info for ExistingFolderItems as alias)
end if
end repeat
end if
else
if (kind of (info for TheExistingItems as alias)) is not "Application" then
set end of AllTheNames to name of (info for TheExistingItems as alias)
end if
end if
end repeat
end tell
end try
return AllTheNames
end AddUpTheItems
on GoAndRename(TheFolderItem)
try
tell application "Finder"
set temp3 to ""
set NameItem to TheFolderItem as alias
set TheName to (displayed name of (info for (NameItem)))
if (extension hidden of (info for (NameItem))) = false then
set temp3 to "." & (name extension of (info for (NameItem)))
-- Now set main string for name, minus extension
if temp3 = ".missing value" then
set temp3 to ""
else
set TheName to my replace_chars(TheName, temp3, "")
end if
end if
set AddToName to 1 -- The 'add-on to name' item
set DontAddOn to true -- To ensure first repeat cycle uses original name
set FirstPass to true -- stops 2 cycles of adding to names, cause renaming triggers another cycle
repeat
if DontAddOn and not AlwaysReName then
set NewName to TheName & temp3 -- Set back to original on first repeat
else
set NewName to TheName & " " & (AddToName as string) & temp3
end if
set RenameFlag to false
(* Now we have to check two states...
1. Does the re-named file exist in the destination folder,
2. is there a conflict in the present folder if we re-name.
*)
if NewName is not in AllTheExistingNames then
if my CheckAllTheNewItems(NewName) < 2 then
set RenameFlag to true
else
(* and if number 2 above is false, we still need to allow
for the 'Always Rename' state being false.
*)
if DontAddOn then
set RenameFlag to true
end if
end if
end if
if RenameFlag then
exit repeat
else
set AddToName to AddToName + 1
set DontAddOn to false
end if
end repeat
end tell
end try
set AllTheExistingNames to AllTheExistingNames & NewName
return NewName
end GoAndRename
on CheckAllTheNewItems(CheckName)
set y to 0
-- we need to fiddle with FirstPass in case the item has a unique name,
-- and just needs shifting. Otherwise, if a dropped file exists with the
-- new intended name then up y to 2
repeat with x from 1 to number of items in AllTheNewNames
set test to item x of AllTheNewNames
if test is equal to CheckName then set y to y + 1
end repeat
if FirstPass and y = 1 then
set y to 2
set FirstPass to false
end if
return y
end CheckAllTheNewItems
on replace_chars(this_text, search_string, replacement_string)
set tempdelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to tempdelimiters
return this_text
end replace_chars
Model: G5 1.8 GHz
AppleScript: 2.1.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)