I would like to use a script to change file names. I have a bunch of files that have an added file type. like XXXXX.cmx.pdf
and I would like to change the file to XXXX.pdf. I have a script but I guess I don’t know what is wrong with it.
display dialog "Search and replace in:" buttons {"File Names", "Folder Names", "Both"} default button 3
set the search_parameter to the button returned of the result
if the search_parameter is "File Names" then
set the display_parameter to "file"
else if the search_parameter is "Folder Names" then
set the display_parameter to "folder"
else
set the display_parameter to "file and folder"
end if
set the source_folder to choose folder with prompt "Folder containing " & the display_parameter & "s to process:"
repeat
display dialog "Enter text to find in " & the display_parameter & " names:" default answer ""
set the search_string to the text returned of the result
if the search_string is not "" then exit repeat
end repeat
repeat
display dialog "Enter replacement text:" default answer ""
set the replacement_string to the text returned of the result
if the replacement_string contains ":" then
beep
display dialog "A file or folder name cannot contain a colon (:)."
else
exit repeat
end if
end repeat
display dialog "Replace "" & the search_string & "" with "" & the replacement_string & "" in every " & the display_parameter & " name?"
display dialog "Include nested folders?" buttons {"Cancel", "Included Nested", "This Folder Only"} default button 3
set the search_level to the button returned of the result
if the search_parameter is "Both" then
if the search_level is "This Folder Only" then
set the item_list to (every item of the source_folder whose name contains the search_string) as list
else
set the item_list to (every item of the entire contents of the source_folder whose name contains the search_string) as list
end if
else if the search_parameter is "Folder Names" then
if the search_level is "This Folder Only" then
set the item_list to (every folder of the source_folder whose name contains the search_string) as list
else
set the item_list to (every folder of the entire contents of the source_folder whose name contains the search_string) as list
end if
else if the search_parameter is "File Names" then
if the search_level is "This Folder Only" then
set the item_list to (every file of the source_folder whose name contains the search_string) as list
else
set the item_list to (every file of the entire contents of the source_folder whose name contains the search_string) as list
end if
end if
repeat with this_item in the item_list
set this_item to the contents of this_item
set the item_name to the name of this_item
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of the item_name
set AppleScript's text item delimiters to the replacement_string
set the new_item_name to the item_list as string
set AppleScript's text item delimiters to ""
try
set the name of this_item to the new_item_name
on error error_msg
set the source_folder_path to my extract_parent_folder_path_from(this_item)
try
reveal (the source_folder_path & the new_item_name) as string as alias
on error
reveal this_item
set error_msg to "The new name: " & return & the new_item_name & return & "contains too many characters to rename this file."
end try
beep
display dialog error_msg buttons {"Stop", "Continue"} default button 2
if the button returned of the result is "Stop" then return "user canceled"
set selection to {}
end try
end repeat
end tell
on extract_parent_folder_path_from(this_filepath)
set this_filepath to this_filepath as text
set x to the offset of “:” in (the reverse of every character of this_filepath) as string
set this_filepath to (characters 1 thru -(x) of this_filepath) as text
return this_filepath
end extract_parent_folder_path_from
beep 2
the item_list
Browser: Safari 417.9.2
Operating System: Mac OS X (10.4)
The script you have here is a very long-winded general purpose script and you don’t tell us why it doesn’t work for you. If you know in advance exactly what you’re trying to change and that the name is always the name of a file, there are much more compact ways to go.
Moved to AppleScript | OS X from AppleScript | Mac OS
In the quote above, where are the “bunch of files”? Do you want to go through an entire folder full of them? Are there folders in that outer folder? Do all files have .cmx to be removed or are there other sub-extensions? Are the final extensions always “.pdf”? The efficiency of the script to be used depends on these kinds of things - i.e., do I have to dig down to find the files, do you want to rename folders too, are there more than one extension to be removed and more than one final result?
Something like this may be what you want: [Corrected version]
set theFldr to choose folder with prompt "Select a folder with file names to be changed"
tell application "Finder"
try -- because alias list won't work if there is only one file.
set theFiles to (every file of entire contents of theFldr) as alias list
on error
set theFiles to (every file of entire contents of theFldr) as alias as list
end try
repeat with aFile in theFiles -- this loop simply removes ".CMX" from every file name that has that in it.
set theName to name of aFile
set TID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ".CMX"
set newParts to (text items of theName)
set AppleScript's text item delimiters to TID
set the name of aFile to newParts as string
end repeat
end tell
I should warn you that I didn’t try this so test it on a copy of a folder you care about. I should further warn you that case matters unless you specify that it doesn’t, so I didn’t know whether ‘.cmx’ or ‘.CMX’ was what you were after - you use both.
The script runs, finds all the files, but it just changes the .cmx to either upper case or lower case, depending which I change it to. Could it just remove the .CMX ? It is upper case.
I’ve corrected my mistake in the script (in my previous post).
And, if both .cmx and .CMX occur within a folder then:
set theFldr to choose folder with prompt "Select a folder with file names to be changed"
tell application "Finder"
try -- because alias list won't work if there is only one file.
set theFiles to (every file of entire contents of theFldr) as alias list
on error
set theFiles to (every file of entire contents of theFldr) as alias as list
end try
repeat with aFile in theFiles -- this loop simply removes ".cmx" from every file name that has that in it.
set theName to name of aFile
considering case -- this deals with either case.
if theName contains ".cmx" then
set delim to ".cmx"
else if theName contains ".CMX" then
set delim to ".CMX"
end if
end considering
set TID to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set newParts to (text items of theName)
set AppleScript's text item delimiters to TID
set the name of aFile to newParts as string
end repeat
end tell
The only difference here is from ‘considering’ to ‘end considering’ and the use of a variable ‘delim’ instead of one or the other forms of ‘.cmx’/‘.CMX’.
Note that the suggested routine could return unexpected results if AppleScript’s text item delimiters happen to be set to some value other than the assumed {“”} (or “”). Also, when using Finder to perform operations, it’s usually not necessary to coerce from Finder references to aliases. In fact, for a job as specific as this, something like the following might do the trick:
tell application "Finder" to repeat with f in (get files of entire contents of (choose folder with prompt ¬
"Select a folder with file names to be changed:") whose name ends with ".cmx.pdf")
set f's name to text 1 thru -8 of (get f's name) & "pdf"
end repeat
As for coercing to alias - it has become a habit; a bad one sometimes. Thanks for the reminder.
Obviously, the last line depends on all the files having names ending in a 3-letter extension followed by another 3-letter extension and it wasn’t clear to me what else might be in this folder.
Yeah - since the script only operates on files whose name ends specifically with “.cmx.pdf” (or any case variant of that), it will actually leave any other files unscathed.
That did the trick. With a couple changes I have also used the script to globely change other files with strange names. Care must be given when changine the 1 to -8 line or the script will remove the last 8 charatures with the replacement text. When my scaner scans and saves it gives a huge title to the save, with this I can remove most and leave the important last 4 numbers in place, example “HPscan-1234567890-19.jpg” changed to “scan-19.jpg”.
That’s right, Tech. As mentioned, the suggestion had a fairly specific aim - which was to modify only files whose name ends with “.cmx.pdf”. When adapting such tightly targeted code for another purpose, it’s important to consider every aspect of it.
I might consider a slightly different approach for this type of situation - and possibly return to a tid-based solution. When removing substantial chunks of filenames, there’s also a risk that name conflicts might arise - so it could be worth including an algorithm that covers such an eventuality. You might, for example, have a couple of files in the same location named “HPscan-1234567890-19.jpg” and “HPscan-1234567891-19.jpg”. If the leading “HP” and the text between the hyphens is removed, you’d end up with two files trying to adopt the name “scan-19.jpg” - which would obviously result in an error.
The following suggestion adds a bracketed number to any duplicate, producing a series of unique names such as: “scan-19.jpg”, “scan-19[2].jpg”, “scan-19[3].jpg”, “scan-19[4].jpg”, etc:
to trim_filenames between char from strt to extn at ctnr
set {s, l, e, d, text item delimiters} to {1 + (count strt), 2 + (count extn), "." & extn, text item delimiters, char}
tell application "Finder" to repeat with f in (get files of folder ctnr's entire contents ¬
whose name starts with strt and name extension is extn)
tell text s thru -1 of (get f's name) to if (count text items) > 2 then
set {t, c} to {text 1 thru text item -3 & ({""} & text 1 thru -l of text item -1), 1}
set n to t & e
tell f's folder to repeat while file n exists
set c to c + 1
set n to t & "[" & c & "]" & e
end repeat
set f's name to n
end if
end repeat
set text item delimiters to d
end trim_filenames
trim_filenames between "-" from "HP" to "jpg" at choose folder with prompt "Select a folder with file names to be trimmed:"