Hi.
Keeping the original strategy I have modified @Mokman’s script that allows to make modifications on a partial or total selection of subfolders and files and include the main part of the code in a script library handler.
Instead of treating folders and files in separate lists, both types of items are in a single list, with the files occupying the positions at the head of the list and the folders occupying the positions at the tail of the list.
Finally, modifications are performed sequentially on the list items (in this case, search and replace on their names).
It works well, even with a selection of several tens of items located in several sublevels of the same root folder.
The problem lies in its slowness of quite a few (too many) seconds, even on high-speed machines (it sometimes makes errors when handling a certain number of items, which I personally attribute to the time it takes to resolve name modifications).
I understand that lists gain a lot of speed when their elements are handled by reference rather than directly. However, I don’t have enough appleScript knowledge to do this and it is in this aspect that I ask for help in modifying the script I write below.
Thank you in advance.
set search to my strFound()
set replace to my replString()
tell application "Finder"
set finderSel to selection as alias list
activate
selection
end tell
search_Repl_FindSel(finderSel, search, replace)
on strFound()
set theText to text returned of (display dialog ¬
"String to be removed from the selection : " default answer "" with title "Search on the name of the selected items" with icon 1)
if theText is "" then
display dialog "" & "It is necessary to look for at least one character" giving up after 3
error number -128
end if
return theText
end strFound
on replString()
set theText to text returned of (display dialog ¬
"String to replace the searched string…" & return & "(Not entering anything is equivalent to deleting the characters searched for) : " default answer "" with title "Replace in the name of the selected items" with icon 1)
return theText
end replString
on search_Repl_FindSel(_selList, _search, _replace)
set totalList to {} --Single list. The first positions will be occupied by files and the last positions by folders.
tell application "System Events"
-- Separate items in a single list
repeat with eachItem in _selList
if kind of eachItem is not "Folder" then
set beginning of totalList to contents of eachItem -- Files at the top of the list
else -- If the item YES is of folder type…
set end of totalList to contents of eachItem -- Folders in the list queue
end if
end repeat
-- rename list items
repeat with eachAlias in totalList
--set itemName to name of eachAlias
set displItemName to displayed name of eachAlias
set extItem to name extension of eachAlias
if extItem is "" then -- If it has no extension (folder)
set nameShort to displItemName -- No extension
else -- If it has extension (file)
set nameShort to text 1 thru (-(length of extItem) - 2) of displItemName
set extItem to "." & extItem -- Yes extension
end if
--Search and replace in item names
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to _search -- búsqueda
set theItems to text items of nameShort
set AppleScript's text item delimiters to _replace
set nameShort to theItems as string
set AppleScript's text item delimiters to oldDelims
--Rebuild the name
set name of eachAlias to nameShort & extItem
end repeat
end tell
end search_Repl_FindSel