AppleScript, Why is my my code taking so long to run?

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).

  1. specificQuarterFolder – a folder of 30 or so concerts
  2. generalMp3Folder – a folder full of thousands of concerts throughout the years, but I’m only trying to change the files found in the specficQuarterFolder

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật