Hello,
we work with template files (generally psds), all saved together in folders, that have a generic name. We copy them and remove the ones not needed for the project, and mass rename the generic string, which in our case is an X. Inside of these folders there are also residue folders and files that have some specific name (this is relevant later).
The templates will contain in the name either ITEM_A or ITEM_B depending which project they are designed for, DRAFT or FINAL depending on the status.
So a typical filename will be ITEM_A_X_DRAFT, where X will be replaced with the current project specifics.
I managed to get a script together that basically saves us a bunch of clicks. The version of the script I’ll use for this topic, will take the current selection in Finder, remove any files whose names contain the ITEM_A or FINAL strings, plus other strings that belong to the loose files or folders, and request a text input which will be used to replace the X in-between the first two underscores in the filename.
tell application "Finder"
set selectedItems to selection
set replacementText to text returned of (display dialog "Event#_AssetName" default answer "")
-- Process each item
repeat with i from 1 to length of selectedItems
set currentItem to item i of selectedItems
set currentName to name of currentItem
if ¬
name of currentItem contains ("ITEM_A") ¬
or name of currentItem contains ("FINAL") ¬
or name of currentItem contains ("assets") ¬
then
delete currentItem
else
if currentName contains "ITEM_B" then
-- Replace the first "X" between underscores in the filename with the specified text
set modifiedName to my replaceFirstX(currentName, replacementText)
set name of currentItem to modifiedName
end if
end if
end repeat
end tell
-- Function to replace the first 'X' between underscores in the filename
on replaceFirstX(fileName, newText)
-- Check if the file has an extension by finding the last period (.)
set fileExtensionPos to offset of "." in fileName
if fileExtensionPos is greater than 0 then
-- Split the file name and extension
set fileExtension to text (fileExtensionPos + 1) through end of fileName
set fileNameWithoutExtension to text 1 through (fileExtensionPos - 1) of fileName
else
-- If there's no extension, handle it as a normal file name
set fileNameWithoutExtension to fileName
set fileExtension to ""
end if
-- Find the first underscore
set firstUnderscorePos to offset of "_" in fileNameWithoutExtension
-- Find the last underscore by searching from the end of the string
set lastUnderscorePos to offset of "_" in (reverse of characters of fileNameWithoutExtension as string)
-- Convert the position from reverse back to regular position
set lastUnderscorePos to (length of fileNameWithoutExtension) - lastUnderscorePos + 1
-- Check if both underscores exist and are valid
if firstUnderscorePos is not 0 and lastUnderscorePos is not 0 then
-- Find the part before the first underscore and after the last underscore
set beforeUnderscore to text 1 through firstUnderscorePos of fileNameWithoutExtension
set afterUnderscore to text (firstUnderscorePos + 1) through (lastUnderscorePos - 1) of fileNameWithoutExtension
-- Replace the X between the underscores
set newMiddle to my replaceText(afterUnderscore, "X", newText)
-- Form the modified file name, keep the extension intact
set afterLastUnderscore to text (lastUnderscorePos + 1) through end of fileNameWithoutExtension
return beforeUnderscore & newMiddle & "_" & afterLastUnderscore & "." & fileExtension
else
-- No X found, return original name with extension
return fileNameWithoutExtension & "." & fileExtension
end if
end replaceFirstX
-- Function to replace text inside a string
on replaceText(theText, searchText, replaceText)
set AppleScript's text item delimiters to searchText
set theTextItems to text items of theText
set AppleScript's text item delimiters to replaceText
set theText to theTextItems as string
set AppleScript's text item delimiters to {""}
return theText
end replaceText
The script works well, without any error, as long as any subfolder selected is not expanded, as per this screenshot
resulting correcty in any item of the selection whose name contained “FINAL” or “ITEM_A” removed, “X” in the filename replaced by “Test”, which was the text input in the initial prompt.
However, if i have the subfolders expanded (and the content selected of course) as in this screenshot, the script will return this error
I half understand why this happens.
In my mind, I would like the script to work “top-down” in the folder structure, and if the subfolders match the criteria to be deleted, they should be deleted together with their content and obviously their content ignored in the script altogether, even if part of the selection, if that makes sense.
I am a total noob btw, so I hope i made any sense, and if I didn’t I am very happy to (try) to clarify.
Could anyone help be troubleshoot this please?
Sorry for the long post!
Cheers