davidt
davidt

Reputation: 308

Applescript to determine file types and give error if not the same

My script processes audio files and I'd like to include a warning if the script determines that there are non audio files or the audio files are not all the same format. This is what I've tried. Ideally a do shell script would be good or bypassing the get items of folder via Finder as it slows the process down a lot.

    -- Get location of the files
    display dialog "Be sure that you have selected the folder in the front Finder window that you want to process!" buttons {"OK"} default button 1
    tell application "Finder" to set thisDir to (target of Finder window 1) as string
    set theDir to quoted form of POSIX path of thisDir
    set theFileCount to (do shell script "ls -1 " & theDir & " | wc -l") as integer
    -- Select the files in the locations & check that they are the same.
    tell application "Finder" to select (every file of target of front window whose name contains ".mp3" or name contains ".wav" or name contains ".aif")
    tell application "Finder" to set thefiles to (every file of target of front window whose name contains ".mp3" or name contains ".wav" or name contains ".aif")
    set the firstfileExt to the name extension of item 1 of thefiles
    if theFileCount is greater than 1 then
        set theFileCounter to 2
        repeat theFileCount times
            set the otherfileExt to the name extension of item theFileCounter of thefiles
            if firstfileExt is otherfileExt then
                -- do nothing
            else
                display dialog "the audio files are not all the same format" buttons {"OK"} default button 1
                return
            end if
        end repeat
    end if

Upvotes: 0

Views: 335

Answers (1)

adayzdone
adayzdone

Reputation: 11238

You can also use System Events to speed things up.

tell application "Finder" to set thisDir to (target of Finder window 1) as alias
tell application "System Events"
    set targetFiles to name extension of files of thisDir whose name extension = "mp3" or name extension = "aif" or name extension = "wav"
    set dupeCheck to {first item of targetFiles}
    repeat with aFile in my targetFiles
        if aFile is not in dupeCheck then
            display dialog "the audio files are not all the same format" buttons {"OK"} default button 1
            return
        end if
    end repeat
end tell

Upvotes: 1

Related Questions