I hope someone can help me with this. I would really, really appreciate it.
I have 1000’s of files that have duplicate text in the file name and I’m trying to figure out a script to delete any duplicate word or text (please see below)
Create on your desktop new empty folder, duplicate to it 2-3 of your files. Test following script:
set folderHFS to (choose folder) as text
set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"_", "-", "."}
tell application "System Events"
repeat with aFile in (get files of folder folderHFS)
set fileName to name of aFile
-- try block required to change only the names which confirm to your template
try
set {A, B, C, D, E, F, G} to text items of fileName
set newName to A & "_" & B & "_" & C & "-" & D
if newName does not contain E then set newName to newName & "_" & E
if newName does not contain F then set newName to newName & "-" & F
set newName to newName & "." & G
set name of aFile to newName
end try
end repeat
end tell
set AppleScript's text item delimiters to ATID
activate
set theFiles to (choose file with prompt "Choose Files" with multiple selections allowed)
tell application "System Events"
repeat with i in theFiles
set oldName to name of i
tell current application to set newName to ¬
(text 1 thru (offset of "- " in oldName) of oldName) & ¬
text -1 thru -8 of oldName
set name of i to newName
end repeat
end tell
I will not be able to install Big Sur on my Macbook Pro. Actually, the parenthesis immediately following the “in” keyword should be interpreted by AppleScript as “(get” implicitly. The fact that Big Sur doesn’t do this means a change to this old rule. But thanks anyway for the info. I updated the script.
I get an error in Mojave too: error “System Events got an error: AppleEvent handler failed.” number -10000.
The parenthesis doesn’t make any difference in this sort of repeat. The repeat is still through items in an application. It seems that System Events doesn’t like being treated like this. The Finder doesn’t seem to mind, but it’s giving me some strange results this morning.
When repeating with objects in an application, it’s usually safer and more efficient to fetch the objects from the application explicitly and then work through the resulting list, rather than index-referencing the objects in the application itself.
Just for learning purposes, I wrote an ASObjC suggestion. It wasn’t clear to me if all of the files are in one folder or not, so my script works recursively. I understand that many will find the script to be unacceptably long, and I post it FWIW. Using an AppleScript to rename thousands of files can be risky; it’s important that the OP have a completely reliable backup of all files.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
on main()
set theFolder to POSIX path of (choose folder)
set theFiles to getFiles(theFolder, {"mov"})
repeat with aFile in theFiles
set aFile to (current application's NSString's stringWithString:(POSIX path of aFile))
set filePath to aFile's stringByDeletingLastPathComponent()
set fileName to (aFile's lastPathComponent())'s stringByDeletingPathExtension()
set fileNameList to (fileName's componentsSeparatedByString:"_") as list
if (count fileNameList) = 4 then
if item 3 of fileNameList = item 4 of fileNameList then
set newFileName to current application's NSString's stringWithFormat_("%@_%@_%@.mov", item 1 of fileNameList, item 2 of fileNameList, item 3 of fileNameList)
renameFile(aFile, newFileName)
end if
end if
end repeat
end main
on getFiles(theFolder, theFileExtensions)
set fileManager to current application's NSFileManager's defaultManager()
set theFolder to current application's |NSURL|'s fileURLWithPath:theFolder
set searchOptions to (current application's NSDirectoryEnumerationSkipsPackageDescendants as integer) + (current application's NSDirectoryEnumerationSkipsHiddenFiles as integer)
set theFiles to (fileManager's enumeratorAtURL:theFolder includingPropertiesForKeys:{} options:searchOptions errorHandler:(missing value))'s allObjects()
set searchPredicate to current application's NSPredicate's predicateWithFormat_("pathExtension.lowercaseString IN %@", theFileExtensions)
return ((theFiles's filteredArrayUsingPredicate:searchPredicate) as list)
end getFiles
on renameFile(theFile, newName)
set fileManager to current application's NSFileManager's defaultManager()
set theNewPath to theFile's stringByDeletingLastPathComponent()'s stringByAppendingPathComponent:newName
if fileManager's fileExistsAtPath:theNewPath then
display dialog quote & (newName as text) & quote & " already exists and will be skipped" with icon stop
else
fileManager's moveItemAtPath:theFile toPath:theNewPath |error|:(missing value)
end if
end renameFile
main()
Thank you all for your prompt responses. I’m new to scripts and this site, so please bear with me.
I couldn’t find a way to reply back, thats why I had not replied sooner.
Unfortunately these scripts are not working for me. I apologize if my last post sounded a bit vague.
So here is the full story and what brought me here:
I have created a workflow in Automator to remove duplicate words from files using “Get Specified Movies” as the input by dragging the .mov files and using the “Rename Finder Items / Find and Replace”. However, I believe there is a limit to the number of actions “Rename Finder Items” that I can add because it takes a really long time to open and stops responding upon opening.
My question is, I have 1000’s of .mov (clips) files that I would like to rename. Is there a script that I can add to my workflow to do all the heavy-lifting?
Here are some examples of how my files are label and hoping to be labeled if there is a script out there.
I created a folder containing four files with the exact names shown in your post. I then copied and pasted the three script solutions contained in this thread into Script Editor (one at a time of course) and when run all of the scripts renamed the files as you want. Wch1zpink’s script requires that you select the source files in a dialog, which obviously isn’t going to work when renaming thousands of files, but his rename code works.
So, my suggestion is that you do what I did. If the scripts work, then you can try them with a larger quantity of source files. If they don’t work, then you can report back to this forum what you did, what happened, and what error messages were reported. After getting a working script, you can attempt to get the script to work in Automator.
I mentioned above that using an AppleScript to rename thousands of files can be risky and doing this within Automator adds an additional layer of complexity and risk. Please be sure to retain a reliable backup of all files.