No internet connection
  1. Home
  2. Macro and Script Help

Check Folder for Audio Files: If They Exists, Import Them

By Nathan Salefski @nathansalefski
    2023-08-24 23:21:57.115Z2023-08-25 02:21:33.569Z

    Title

    Check Folder for Audio Files: If They Exists, Import Them

    What do you expect to happen when you run the script/macro?

    Session Prep Script

    Are you seeing an error?

    What happens when you run this script?

    It works as expected. I want to add functionality described in the comment below

    How were you running this script?

    I used a Stream Deck button

    How important is this issue to you?

    5

    Details

    {
        "inputExpected": "Session Prep Script",
        "inputIsError": false,
        "inputWhatHappens": "This will set up a session, importing Audio, I/O and my Template. It will gather the pathways of subfolders within a main folder and import the audio from each. I would like to have it check the main folder for audio as well",
        "inputHowRun": {
            "key": "-MpfwmPg-2Sb-HxHQAff",
            "title": "I used a Stream Deck button"
        },
        "inputImportance": 5,
        "inputTitle": "Import Audio from a Folder and it's Subfolders"
    }

    Source

    function getFolders(path) { return sf.file.directoryGetDirectories({ path, isRecursive: true, }).paths}
    
    function importAudioForEach({ paths }) {
        
        //Define Import Audio Window
        const openWin = sf.ui.proTools.windows.whoseTitle.is('Open').first;
    
        //Reference to Audio Import Options Window
        const aIOWin = sf.ui.proTools.windows.whoseTitle.is("Audio Import Options").first
    
        paths.forEach(path => {
    
            //File, Import, Audio
            sf.ui.proTools.menuClick({ menuPath: ["File", "Import", "Audio..."], });
    
            //Wait for 'Open' Window
            openWin.elementWaitFor({ waitType: "Appear" });
    
            //Open 'Go' Sheet
            sf.keyboard.type({ text: '/' });
    
            //Wait for 'Go' Sheet Window to Appear
            openWin.sheets.first.elementWaitFor({ waitType: "Appear" });
    
            //Set Destination
            openWin.sheets.first.textFields.first.elementSetTextAreaValue({ value: path });
    
            //Press Return
            sf.keyboard.press({ keys: "return", });
    
            //Wait for 'Go' Sheet Window to Disappear
            openWin.sheets.first.elementWaitFor({ waitType: "Disappear" });
    
            //Select All
            sf.keyboard.press({ keys: "cmd+a", });
    
            //Try "Copy" or "Convert"
            try {openWin.buttons.whoseTitle.is("Copy ->").first.elementClick();}
    
            catch(err) { 
                
                sf.wait({intervalMs: 200});
    
                if (openWin.buttons.whoseTitle.is("Convert ->").first.exists) { 
                
                    try {openWin.buttons.whoseTitle.is("Convert ->").first.elementClick();} 
                
                    catch(err) {log('Could Not Import Audio')};
                    
                } else {log('Could Not Import Audio'); throw 0;}
            }
    
            //Click "Open"
            openWin.buttons.whoseTitle.is('Open').first.elementClick();
    
            //Wait for 'Open' Window
            openWin.elementWaitFor({ waitType: "Appear" });
    
            //Click "Open"
            openWin.buttons.whoseTitle.is('Open').first.elementClick();
    
            //Wait for 'Go' Sheet Window to Appear
            aIOWin.elementWaitFor({ waitType: "Appear" });
    
            //Click "OK" to New Track
            aIOWin.buttons.whoseTitle.is("OK").first.elementClick();
    
            //Wait
            sf.ui.proTools.waitForNoModals();
    
        });
    
        //Refresh Cache
        sf.ui.proTools.mainWindow.invalidate();
    
    }
    
    importAudio({ paths: getFolders(selectedFolder) });
    
    

    Links

    User UID: EoVu20w2ZRTvmJvgozc57ZXuAhU2

    Feedback Key: sffeedback:EoVu20w2ZRTvmJvgozc57ZXuAhU2:-Ncdqg5q_o1dBefG1PGg

    Feedback ZIP

    Solved in post #3, click to view
    • 2 replies
    1. Nathan Salefski @nathansalefski
        2023-08-24 23:27:41.886Z2023-08-25 02:21:56.676Z

        Currently it will import all of the audio depicted below except "MAIN FOLDER 1" and "MAIN FOLDER 2". I know importAudio({ paths: getFolders(selectedFolder) }); is only getting the pathways of folders within the selected folder and that is why, but I would like to be able to check for audio files within the main folder. If it exists, import it along with the other files.

        1. Nathan Salefski @nathansalefski
            2023-08-25 00:07:00.915Z

            I think I got it. Just need an additional function for importing from this path. Could someone confirm?

            function checkForWAVE(path) {return sf.file.directoryGetFiles({searchPattern: "*" + '.wav', path: path }).paths[0]}
            
            function checkForMP3(path) {return sf.file.directoryGetFiles({searchPattern: "*" + '.mp3', path: path }).paths[0]}
            
            const wav = checkForWAVE(selectedFolder);
            
            const mp3 = checkForMP3(selectedFolder);
            
            if (wav || mp3) { importAudio({ path: selectedFolder }) };
            
            ReplySolution