Hi I have a folder with 264 files named from 1.txt to 264.txt
I would like to rename them according to a text file where I have 264 names in alphabetical order.
Each original file contains references to the text file
Yet eg 1.txt matches the first paragraph in my text list
I tried this script I found on the internet, but the Finder gives errors.
set the_list to (read (choose file with prompt "Choose the file that contains the student ID's:"))'s paragraphs
set source_folder to (choose folder with prompt "Choose the folder with the items to rename:") as Unicode text
tell application "Finder"
repeat with this_para in the_list
set {source_file, new_name} to my string_to_list(this_para, tab)
try
set name of ((source_folder & source_file) as alias) to new_name & "html"
on error e
activate
display dialog e buttons {"Cancel", "OK"} default button 2 with icon 0 giving up after 5
end try
end repeat
end tell
on string_to_list(s, d)
tell (a reference to my text item delimiters)
set {o, contents} to {contents, d}
set {s, contents} to {s's text items, o}
end tell
return s
end string_to_list
Thanks for your help
Model: Quad-Core Intel Xeon
AppleScript: Version 2.3 (118)
Browser: Safari 533.20.27
Operating System: Mac OS X (10.6)
the script look’s interesting. I’ll have a look at it later on …
You may try this simple code instead…
Hope it works:
set NewNames to paragraphs of (read (choose file))
set theFolder to choose folder
set fileList to list folder theFolder without invisibles
repeat with i from 1 to count of NewNames
try
set theName to item i of NewNames
do shell script "mv -f " & quoted form of POSIX path of ((theFolder as text) & (item i of fileList)) & space & quoted form of POSIX path of ((theFolder as text) & theName)
on error e
activate
display dialog e buttons {"Cancel", "OK"} default button 2 with icon 0 giving up after 5
end try
end repeat
Thanks a lot, however I forgot to mention Unicode encoding in this post. Which makes your script act weirdly, althouygh it functions perfectly with standard non accented letters or Chinese names
I tried as follows without success:
I add the word Unicode, but I think i should change something in the shell script … and there I am totally lost
thanks anyway
set NewNames to paragraphs of (read (choose file))
set theFolder to choose folder
set fileList to list folder theFolder without invisibles
repeat with i from 1 to count of NewNames
try
set theName to item i of NewNames
do shell script "mv -f " & quoted form of POSIX path of ((theFolder as Unicode text) & (item i of fileList)) & space & quoted form of POSIX path of ((theFolder as Unicode text) & theName)
on error e
activate
display dialog e buttons {"Cancel", "OK"} default button 2 with icon 0 giving up after 5
end try
end repeat
If your text file contains UTF-16 Unicode text, the first line of the script should be:
set NewNames to paragraphs of (read (choose file) as Unicode text)
This tells the ‘read’ command how to interpret the data in the file. For historical reasons to do with the Mac’s PowerPC past, ‘read’ assumes that UTF-16 text is big-endian unless the first two bytes read area a little-endian byte-order mark.
If the file contains UTF-8 Unicode text, the first line should be:
set NewNames to paragraphs of (read (choose file) as «class utf8»)
I imagine the shell script should work OK after that, but I haven’t tried it.
The “UTF-8” line in combination with your above script works freaking perfectly! Thank you SO much for this!
A couple notes:
I removed the “on error” part of the “try” loop since it kept giving me errors while my initial result worked just fine.
Also, it’s REALLY IMPORTANT that the files to be renamed in the folder are in the exact same order as are the names in the UTF list. I know this sounds obvious, but the Mac OS does a much better job of “alphabetizing” than the base OS does. So, while the Finder will correctly order files that are numbered (such as 1, 2, 3…10, 11, 12, etc.) the script will see them in the order of: 1, 11, 12, …2, 3.
In general it’s better to make absolutely certain that your old-name/new-name lists line up.
Here’s one simple method.
-Chris
--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2023/01/06 18:05
# dMod: 2023/01/06 18:05
# Appl: Finder
# Task: Rename Files in a Given Folder Using a List
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder, @Rename, @Files, @List
--------------------------------------------------------
set sourceFolder to alias ((path to home folder as text) & "test_directory:rename_test:rename_test:")
set renameRecordList to getRenameList()
set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, tab}
tell application "Finder"
tell folder sourceFolder
repeat with renameRecord in renameRecordList
set oldName to text item 1 of renameRecord
set newName to text item -1 of renameRecord
set name of (get item oldName) to newName
end repeat
end tell
end tell
set AppleScript's text item delimiters to oldTIDS
--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
on getRenameList()
return paragraphs 2 thru -1 of text 2 thru -2 of "
ORIGINAL FILE NAME NEW FILE NAME
Test File 01.txt Goofy 01.txt
Test File 02.txt Knockout 01.txt
Test File 03.txt Thunder 01.txt
Test File 04.txt Ranch 01.txt
"
end getRenameList
--------------------------------------------------------
Hey Chris, thanks for the suggestion. In my case, my “oldNames” aren’t particularly descriptive, but are automatically derived from my editing of files from an audio application. As long as I’m diligent about my recording and editing protocol, renaming to the “newNames” works just like it’s supposed to, but I won’t know until I try it and audition the files.
Therefore, it’s always good to have a backup plan and make a backup of the folder with the files that are being renamed.