Complicated sorting and arranging of many files

Hey everyone,

I have come across a problem that I just can’t crack. I thought writing a script may be the solution, but it’s quite possible I am overlooking the possibility of using Mac Automator in some sort of fashion. I am also not very experienced in writing scripts, but I welcomed it as a challenge. Here is my situation…

I am using audacity to clip 30 sec dialogues from many different audio files. I have figured out a macro to clip every thirty seconds, along with providing a 1 sec clip that identifies the file by the track name. I export the files out of audacity in 30 sec segments along with the one sec clip of the track name to identify what track each following 30 sec clip belong to. These clips are all exported as files into a folder that sorts the files by date modified. This allows me to use the one sec clip with its name as a marker for the subsequent 30 sec clips. Each time a track name comes up, I know that the following clips will be from that track. Here is an example what the folder would look like…


The “untitled” wavs are a result of my method to clip 30 second segments of audio and provide a 1 sec clip of the track name (“VINTAGE TV CAR COMMERCIAL, ANIMATED AJAX COMMERCIAL…” etc). The untitled.wav’s are each 1 second long and each numbered wav is the 30 sec fragment. (01,02,03, etc). To make it less complicated, I can always delete the untitled files as they serve no practical purpose.

As you can see, some tracks are longer than others, meaning that some have four 30 sec clips, some have five, some two, etc. Still, a pattern emerges that when sorted by date modified, the numbered files following the 1 sec track name belong to that track. This pattern repeats throughout the folder.

So, my objective is to create a folder with the track name and place the subsequent numbered files that belong to that track in the folder. Every time a new 1 sec file is detected, a folder should be created and the numbered files following would be placed in the folder until another 1 sec file is detected and the process would repeat.

So, my kind readers and audience… does anybody have any suggestions, advice, or maybe even help for this problem? I am tickled that I figured out how to automatically clip 30 sec segments along with providing track markers, but I did not realize how long it would take to manually create, name, and move the folders and files into an organized structure. I have tons of audio, and I would love to create them into 30 sec clips for my mpc sampler and add these elements to my music.

Any advice or suggestions? And thank you everyone that took the time to read this.

Try this:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
set sourceFolder to choose folder
tell application "Finder"
   open sourceFolder
   set sourceFiles to sort items of sourceFolder ¬
      by creation date
   set saveTID to AppleScript's text item delimiters
   set AppleScript's text item delimiters to {"."}
   repeat with thisFile in sourceFiles
      set thisFileName to the name of thisFile
      set fileNameStub to (text items 1 thru -2 of thisFileName) as text
      if fileNameStub does not start with "untitled" then
            set fileAsNumber to fileNameStub as integer
         on error
            set currentFolder to make new folder ¬
               at sourceFolder ¬
               with properties {name:fileNameStub}
         end try
      end if
      move thisFile to currentFolder
   end repeat
end tell
set AppleScript's text item delimiters to saveTID

1 Like

Thank you so much! It worked.

1 Like