I’m trying to change the names of files through a system of networks.
Most concert folders contain files that have been mislabelled.
This script renames the files with the specific issue.
- Folder: 20100523-ConcertName
- FileName: 20100523-SongNumber-SongName.wav -> 20100523-ConcertName-SongNumber-SongName.wav
There’s two locations I want to change the names of (And I can’t change this).
specificQuarterFolder
– a folder of 30 or so concertsgeneralMp3Folder
– a folder full of thousands of concerts throughout the years, but I’m only trying to change the files found in thespecficQuarterFolder
When testing with 10 or so folders of files, it runs in an instant. But running it on the system, it takes hours and eventually crashes.
# Set to the destination of a Specific Quarter. Ex: "Desktop/Fall 2010"
set specificQuarterFolder to "/Volumes/concert_archive/concert_archive_combined/2010_Fall"
set destination to "/Users/music-production/Desktop/Scripts for Renaming/2010_Fall_RenameLog.txt"
# Set to the destination of the general MP3 Folder
set generalMp3Folder to "/Volumes/concert_archive/concert_archive_mp3"
set combinedArchiving to (POSIX file specificQuarterFolder) as text
set mp3Archiving to (POSIX file generalMp3Folder) as text
set logFile to (POSIX file destination) as text
global logFileRef
global counter
set counter to 0
set specfic to "Selected Quarter: " & specificQuarterFolder
set general to "General: " & generalMp3Folder
set dialogResult to display dialog specfic & return & general & return & "Do you want to continue?" buttons {"Cancel", "Continue"} default button "Continue"
on changeFileNamesAtPath(folderPath, concertName, extensionName, type)
set changedFileCounter to 0
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"-"}
tell application "Finder"
set songFiles to files of folder folderPath whose name extension is extensionName
set songFileNames to name of every file of folder folderPath whose name extension is extensionName
end tell
repeat with i from 1 to count of songFiles
set songFile to item i of songFiles
set fileName to item i of songFileNames
set splitString to text items of fileName
-- Assuming Concert names will never be 2 characters
set secondItem to item 2 of splitString
if (length of secondItem) is equal to 2 then
set newName to concertName & text 9 thru -1 of fileName
tell application "Finder"
set name of songFile to newName
end tell
set counter to counter + 1
set changedFileCounter to changedFileCounter + 1
set logEntry to " " & counter & " [" & type & "] " & fileName & " -> " & newName & linefeed
write (logEntry) to logFileRef
end if
end repeat
set AppleScript's text item delimiters to oldDelimiters
if type is not equal to "Joint MP3s" and changedFileCounter is not equal to 0 then
write (return) to logFileRef
end if
return changedFileCounter
end changeFileNamesAtPath
if button returned of dialogResult is "Continue" then
set logFileRef to open for access logFile with write permission
set eof logFileRef to 0 -- Clear the file
try
tell application "Finder"
set concertList to name of folders of folder combinedArchiving
end tell
repeat with concert in concertList
write (concert & return) to logFileRef
set concertPath to (combinedArchiving & concert & ":") as text
try
set stereo96Path to (concertPath & "Stereo 96:") as text
set stereoCount to changeFileNamesAtPath(stereo96Path, concert, "wav", "Stereo 96")
end try
try
set mp3Path to (concertPath & "MP3:") as text
set mp3Count to changeFileNamesAtPath(mp3Path, concert, "mp3", "MP3")
end try
-- Changing the Other MP3 Folder
try
tell application "Finder"
set targetMP3Concert to (first folder of folder mp3Archiving whose name is concert)
end tell
if targetMP3Concert is not missing value then
set secondMp3Path to (mp3Archiving & (name of targetMP3Concert) & ":") as text
set secondMp3Count to changeFileNamesAtPath(secondMp3Path, concert, "mp3", "Joint MP3s")
else
set secondMp3Count to 0
end if
end try
try
if mp3Count is not equal to stereoCount or mp3Count is not equal to secondMp3Count then
write ("Unbalanced Changes" & return) to logFileRef
end if
end try
write ("=====Done=====" & return & return) to logFileRef
end repeat
close access logFileRef
on error errMsg
try
close access logFileRef
end try
display dialog ("Error: " & errMsg) buttons {"OK"}
end try
set display to "Files Changed: " & counter
set youCanFind to "You can find all the changed Files Here"
display dialog ("Files Changed: " & counter & return & youCanFind & return & destination) buttons {"OK"}
end if
- The problem for RuntimeError? I’m thinking it’s the “whose” function that is trying to find each one of the 30 or so concerts in the thousands of folders. I am new to AppleScript, what should I do differently?
- Or is it because both of these locations are not on the local computer but connected through NAS drives (Network Attached Storage)?
New contributor
jamesd5 jamesd5 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.