No internet connection
  1. Home
  2. How to

Thanks to Kitch I wrote my first script! is there a better way though ?

By Jack Green @Jack_Green
    2022-08-17 09:03:20.698Z

    So after going to the Soundflow meet up last Wednesday and spending an hour with Kitch i've been trying to get going with my first scripts... I know so little java most of this is building it with macros and coping or just stealing it from the forum...

    It works which i'm super chuffed about! The idea is basically to change the Start session Timecode to the currently selected Timecode.

    I just want to check i've done it in the most effective/stable way? I especially thing there may be a better option for the end pop up menu? Where after changing the TC I want to click Maintain Timecode? currently I'm just doing this by pressing return.

    I know I Don't need the log I just put it in there to check i was getting the correct TC

    //Check App Main window//
    sf.ui.proTools.mainWindow.invalidate();
    
    //Get Current Timecode//
    
    var currentTimecode = sf.ui.proTools.mainWindow.counterDisplay.mainCounter.value.invalidate().value
    
    log(currentTimecode);
    
    //Open Session Setup and Add new TC//
    sf.ui.proTools.appActivateMainWindow();
    
    sf.ui.proTools.menuClick({
      menuPath: ["Setup","Session"],
    });
    
    sf.ui.proTools.windows.whoseTitle.is('Session Setup').first.groups.whoseTitle.is('Session Format').first.textFields.whoseTitle.is('NumericEntryText').first.elementClick();
    
    sf.keyboard.type({
      text: currentTimecode,
    });
    
    sf.keyboard.press({
      keys: "return",
    });
    
    //Popup Click Maintain Relative Timecode//
    
    sf.ui.proTools.confirmationDialog.elementWaitFor();
    
    sf.keyboard.press({
      keys: "return",
    });
    
    sf.ui.proTools.windows.whoseTitle.is("Session Setup").first.windowClose();
    
    
    
    
    • 2 replies
    1. Kitch Membery @Kitch2022-08-17 19:08:02.324Z

      Nice one @Jack_Green,

      I'll take a look :-)

      1. In reply toJack_Green:
        Kitch Membery @Kitch2022-08-17 19:54:19.053Z

        Great work @Jack_Green,

        I've refactored the script for you to make it a little more robust.

        Amongst other small tweaks;

        • The script now ensures that you are in "Timecode" before getting the Main Counter's Timecode Value.
        • The Main Counter value is now collected using sf.ui.proTools.selectionGet().mainCounter
        • Working with Numeric Text fields can be a little tricky at times so I added a function I built for working with them.
        • I changed the "return" key press to an element click on the "OK" button in the dialog. (It's always best to avoid keyboard and mouse simulation if at all possible.)
        function setNumericEntryTextField({ window, field, value }) {
            window.elementWaitFor()
            window.elementRaise();
            field.elementClick();
        
            sf.keyboard.type({ text: value });
        
            var i = 0;
        
            while (field.value.invalidate().value !== value && i < 20) {
                sf.wait({ intervalMs: 100 });
                i++
            };
        
            if (field.value.invalidate().value !== value) {
                throw `Could not set value of ${value}`
            }
        
            sf.keyboard.press({ keys: 'return' });
        }
        
        function main() {
            //Activate Pro Tools Main Window
            sf.ui.proTools.appActivateMainWindow();
            //Invalidate Pro Tools Main Window
            sf.ui.proTools.mainWindow.invalidate();
        
            //Declare variable
            let currentTimecode;
        
            sf.ui.proTools.mainCounterDoWithValue({
                targetValue: "Timecode",
                action: () => {
                    //Get Current Timecode
                    currentTimecode = sf.ui.proTools.selectionGet().mainCounter
                }
            })
        
            const sessionSetupWindow = sf.ui.proTools.windows.whoseTitle.is('Session Setup').first;
            const sessionFormatGroup = sessionSetupWindow.groups.whoseTitle.is('Session Format').first;
            const sessionFormatField = sessionFormatGroup.textFields.whoseTitle.is('NumericEntryText').first;
        
            //If the Session Setup window is not open, open it
            if (!sessionSetupWindow.exists) {
                sf.ui.proTools.menuClick({
                    menuPath: ["Setup", "Session"],
                });
            }
        
            setNumericEntryTextField({
                window: sessionSetupWindow,
                field: sessionFormatField,
                value: currentTimecode,
            });
        
            //Maintain Relative Timecode Dialog
            const dlg = sf.ui.proTools.confirmationDialog;
        
            dlg.elementWaitFor();
        
            dlg.buttons.whoseTitle.is("OK").first.elementClick();
        
            sessionSetupWindow.windowClose();
        
            sessionSetupWindow.elementWaitFor({
                waitForNoElement: true
            });
        }
        
        main();
        

        I hope that helps. And great to meet you in the SoundFlow Hangout!
        Rock on!