No internet connection
  1. Home
  2. How to

Create a folder if it doesnt already exist.

By Brandon Jiaconia @Brandon_Jiaconia
    2024-05-10 19:53:49.896Z

    I'm working on a script that exports my stems. It's pretty basic, it selects a stem, grabs the file name, selects a few more stems, and exports to the current Dates folder in my Bounced Files folder. It works great. But the script relies on the dated folder to already be in the Bounced Files folder. I'm wondering if I can update the script to create that dated folder if it doesnt already exist

    //Select Next Clip, Copy Name, Select Stems//
    sf.ui.proTools.clipSelectNextFullClip();
    
    sf.keyboard.press({
        keys: "cmd+shift+r",
    });
    
    sf.keyboard.press({
        keys: "cmd+c",
    });
    
    sf.keyboard.press({
        keys: "return",
    });
    
    sf.wait({
        intervalMs: 500,
    });
    
    sf.keyboard.press({
        keys: "shift+semicolon",
        repetitions: 4,
    });
    //Export selected Clips//
    sf.keyboard.press({
        keys: "cmd+shift+k",
    });
    
    //Navigate to Current Session Bounced Files by Date//
    sf.ui.proTools.windows.whoseTitle.is("Export Selected").first.buttons.whoseTitle.is("Choose...").first.elementClick();
    
    function navigateToInDialog(path) {
        //Get a reference to the focused window in the frontmost app:
        var win = sf.ui.frontmostApp.focusedWindow;
    
        //Open the Go to... sheet
        sf.keyboard.type({ text: '/' });
    
        //Wait for the sheet to appear
        var sheet = win.sheets.first.elementWaitFor({ timeout: 500 }, 'Could not find "Go to" sheet in the Save/Open dialog').element;
    
        //It's a combo box on older OS'es, from 12.something it's a text field
        if (sheet.comboBoxes.first.exists) {
            sheet.comboBoxes.first.value.value = path;
    
            //Press OK
            sheet.buttons.whoseTitle.is('Go').first.elementClick({}, 'Could not click "Go"');
        }
        else {
            //Newer OS.
            //TextField with AXConfirm
            var textField = sheet.textFields.first;
            if (!textField.exists) throw `Can't find text field`;
            textField.value.value = path;
    
            sheet.elementRaise();
    
            textField.mouseClickElement({
                relativePosition: { x: 5, y: 5 },
            });
    
            sf.keyboard.press({ keys: 'return' });
        }
    
        //Wait for sheet to close
        win.sheets.first.elementWaitFor({ waitForNoElement: true, timeout: 2500 }, '"Go to" sheet didn\'t close in time');
    }
    
    // Get the base folder path
    var baseFolderPath = sf.ui.proTools.mainWindow.sessionPath.split('/').slice(0, -1).join('/');
    
    // Get the current date in the format "YYYY-MM-DD"
    var currentDate = new Date().toISOString().split('T')[0];
    
    // Combine the base path with the current date and '/Bounces'
    var folderPath = baseFolderPath + '/Bounced Files/' + currentDate;
    
    // Call the function with the updated folder path
    navigateToInDialog(folderPath);
    
    //Create New Folder//
    sf.keyboard.press({
        keys: "cmd+shift+n",
    });
    
    sf.wait({
        intervalMs: 150,
    });
    //Paste Folder name//
    sf.keyboard.press({
        keys: "cmd+v",
    });
    
    sf.keyboard.press({
        keys: "return",
    });
    
    sf.wait({
        intervalMs: 50,
    });
    
    sf.keyboard.press({
        keys: "return",
    });
    
    sf.ui.proTools.waitForNoModals();
    
    sf.keyboard.press({
        keys: "return",
        repetitions: 3,
        fast: false,
    });
    
    
    
    
    
    Solved in post #2, click to view
    • 1 replies
    1. B
      Brandon Jiaconia @Brandon_Jiaconia
        2024-05-10 21:36:44.927Z

        I was able to figure this out, but maybe there is a cleaner way to go about it. First the script navigates to the Bounced Files folder of the open session. Then an applescript checks the folder to see if the dated folder exists, if not, creates it, then it goes back to Pro Tools and executes the rest of my stem export process.

        //Navigate to Open Session Bounced Files folder
        function navigateToInDialog(path) {
            //Get a reference to the focused window in the frontmost app:
            var win = sf.ui.frontmostApp.focusedWindow;
        
            //Open the Go to... sheet
            sf.keyboard.type({ text: '/' });
        
            //Wait for the sheet to appear
            var sheet = win.sheets.first.elementWaitFor({ timeout: 500 }, 'Could not find "Go to" sheet in the Save/Open dialog').element;
        
            //Set the value of the combo box
            sheet.comboBoxes.first.value.value = path;
        
            //Press OK
            sheet.buttons.whoseTitle.is('Go').first.elementClick({}, 'Could not click "Go"');
        
            //Wait for sheet to close
            win.sheets.first.elementWaitFor({ waitForNoElement: true, timeout: 500 }, '"Go to" sheet didn\'t close in time');
        }
        
        var folderPath = sf.ui.proTools.mainWindow.sessionPath.split('/').slice(0, -1).join('/') + '/Bounced Files';
        
        if (sf.ui.frontmostApp.focusedWindow.title.value.match(/open|import/i)) {
            navigateToInDialog(folderPath);
        } else {
            sf.system.exec({ commandLine: 'open "' + folderPath + '"' });
            sf.ui.finder.appActivateMainWindow();
            sf.ui.finder.windows.first.elementRaise();
        }
        //Check if the Dated Folder exists, if not, create it
        sf.system.execAppleScript({
            script: `
        -- Get the current date in YYYY-MM-DD format
        set currentDate to do shell script "date +'%Y-%m-%d'"
        
        -- Get the path of the frontmost Finder window
        tell application "Finder"
            set finderWindow to front window
            set targetFolderPath to POSIX path of (target of finderWindow as alias)
        end tell
        
        -- Combine the target folder path with the current date to get the full path
        set fullFolderPath to targetFolderPath & "/" & currentDate
        
        -- Check if the folder exists
        tell application "System Events"
            if not (exists folder fullFolderPath) then
                -- Folder doesn't exist, create it
                do shell script "mkdir -p " & quoted form of fullFolderPath
                    else
                -- Folder already exists
            end if
        end tell
        
        `
        });
        
        //Go Back To Pro Tools
        sf.ui.proTools.appActivateMainWindow();
        
        
        //Select Next Clip, Copy Name, Select Stems//
        sf.ui.proTools.clipSelectNextFullClip();
        
        sf.keyboard.press({
            keys: "cmd+shift+r",
        });
        
        sf.keyboard.press({
            keys: "cmd+c",
        });
        
        sf.keyboard.press({
            keys: "return",
        });
        
        sf.wait({
            intervalMs: 500,
        });
        
        sf.keyboard.press({
            keys: "shift+semicolon",
            repetitions: 4,
        });
        //Export selected Clips//
        sf.keyboard.press({
            keys: "cmd+shift+k",
        });
        
        //Navigate to Current Session Bounced Files by Date//
        sf.ui.proTools.windows.whoseTitle.is("Export Selected").first.buttons.whoseTitle.is("Choose...").first.elementClick();
        
        function navigateToInDialog(path) {
            //Get a reference to the focused window in the frontmost app:
            var win = sf.ui.frontmostApp.focusedWindow;
        
            //Open the Go to... sheet
            sf.keyboard.type({ text: '/' });
        
            //Wait for the sheet to appear
            var sheet = win.sheets.first.elementWaitFor({ timeout: 500 }, 'Could not find "Go to" sheet in the Save/Open dialog').element;
        
            //It's a combo box on older OS'es, from 12.something it's a text field
            if (sheet.comboBoxes.first.exists) {
                sheet.comboBoxes.first.value.value = path;
        
                //Press OK
                sheet.buttons.whoseTitle.is('Go').first.elementClick({}, 'Could not click "Go"');
            }
            else {
                //Newer OS.
                //TextField with AXConfirm
                var textField = sheet.textFields.first;
                if (!textField.exists) throw `Can't find text field`;
                textField.value.value = path;
        
                sheet.elementRaise();
        
                textField.mouseClickElement({
                    relativePosition: { x: 5, y: 5 },
                });
        
                sf.keyboard.press({ keys: 'return' });
            }
        
            //Wait for sheet to close
            win.sheets.first.elementWaitFor({ waitForNoElement: true, timeout: 2500 }, '"Go to" sheet didn\'t close in time');
        }
        
        // Get the base folder path
        var baseFolderPath = sf.ui.proTools.mainWindow.sessionPath.split('/').slice(0, -1).join('/');
        
        // Get the current date in the format "YYYY-MM-DD"
        var currentDate = new Date().toISOString().split('T')[0];
        
        // Combine the base path with the current date and '/Bounces'
        var folderPath = baseFolderPath + '/Bounced Files/' + currentDate;
        
        // Call the function with the updated folder path
        navigateToInDialog(folderPath);
        
        //Create New Folder//
        sf.keyboard.press({
            keys: "cmd+shift+n",
        });
        
        sf.wait({
            intervalMs: 150,
        });
        //Paste Folder name//
        sf.keyboard.press({
            keys: "cmd+v",
        });
        
        sf.keyboard.press({
            keys: "return",
        });
        
        sf.wait({
            intervalMs: 50,
        });
        
        sf.keyboard.press({
            keys: "return",
        });
        
        sf.ui.proTools.waitForNoModals();
        
        sf.keyboard.press({
            keys: "return",
            repetitions: 3,
            fast: false,
        });
        
        
        
        
        ReplySolution