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

SF does not return focus to Pro Tools

By Ben Rubin @Ben_Rubin
    2023-05-05 14:11:44.466Z

    Title

    SF does not return focus to Pro Tools

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

    Open a searchable Insert window on selected track

    Are you seeing an error?

    What happens when you run this script?

    the issue is that if the search window is open and then I close the window via the "esc" key, Soundflow does not return focus to the main window of pro tools. PT is the focused app but the frontmost window is not in focus. This can be corroborated by the fact that the window title is greyed out. I think this is part of a larger SF issue where I notice that it hasn't returned focus to pro tools. will add other examples if I can find any.

    How were you running this script?

    I used a keyboard shortcut within the target app

    How important is this issue to you?

    5

    Details

    {
        "inputExpected": "Open a searchable Insert window on selected track\n",
        "inputIsError": false,
        "inputWhatHappens": "the issue is that if the search window is open and then I close the window via the \"esc\" key, Soundflow does not return focus to the main window of pro tools. PT is the focused app but the frontmost window is not in focus. This can be corroborated by the fact that the window title is greyed out. I think this is part of a larger SF issue where I notice that it hasn't returned focus to pro tools. will add other examples if I can find any.",
        "inputHowRun": {
            "key": "-Mpfwh4RkPLb2LPwjePT",
            "title": "I used a keyboard shortcut within the target app"
        },
        "inputImportance": 5,
        "inputTitle": "SF does not return focus to Pro Tools"
    }

    Source

    //Get the current window name
    const focusedWindow = sf.ui.proTools.focusedWindow.title.invalidate().value.split(" ", 1)[0].slice(0, -1);
    
    //If not already in the Edit window, switch to the Edit window
    if (focusedWindow !== 'Edit') {
        sf.ui.proTools.menuClick({ menuPath: ['Window', 'Edit'] });
    }
    
    function getCurrentTrackHeight(h) {
    
        let originalTrackHeight;
        let isTrackTooSmall = (h <= 43) ? true : false;
    
        //this is all likely tied to screen resolution, highDPI etc
        switch (true) {
            case (h <= 16):
                originalTrackHeight = 'micro';
                break;
            case (h === 23):
                originalTrackHeight = 'mini';
                break;
            case (h === 43):
                originalTrackHeight = 'small';
                break;
            case (h === 97):
                originalTrackHeight = 'medium';
                break;
            case (h === 192):
                originalTrackHeight = 'large';
                break;
            case (h === 300):
                originalTrackHeight = 'jumbo';
                break;
            case (h > 300):
                originalTrackHeight = 'extreme';
                break;
        }
        return { originalTrackHeight, isTrackTooSmall }
    }
    
    /**
    * @param {'micro'|'mini'|'small'|'medium'|'large'|'jumbo'|'extreme'|'fit to window' } size
    */
    
    function setTrackSize(size) {
        const f = sf.ui.proTools.selectedTrack.frame;
        let popupMenu = sf.ui.proTools.selectedTrack.popupMenuOpenFromElement({
            relativePosition: { x: f.w - 10, y: 5 },
            isOption: true,
            isShift: true,
        }).popupMenu;
        popupMenu.menuClickPopupMenu({
            menuPath: [size]
        });
    
        //close popup menu
        sf.ui.proTools.appActivateMainWindow();
    }
    
    /// Get Selected track heights to variable
    //globalState.originalTrackHeight = getCurrentTrackHeight()
    
    // get selected track names
    const selectedTracks = sf.ui.proTools.selectedTrackNames
    
    // scroll to view first selected track
    //sf.ui.proTools.trackGetByName({ name: selectedTracks[0] }).track.trackScrollToView()
    
    //  select original track selection
    sf.ui.proTools.trackSelectByName({ names: selectedTracks })
    
    //get the height of the target track
    const selectedTrackHeight = sf.ui.proTools.selectedTrack.frame.h;
    
    //get original track height and if it's too small
    let { originalTrackHeight, isTrackTooSmall } = getCurrentTrackHeight(selectedTrackHeight);
    
    //resize track if necessary
    if (isTrackTooSmall) setTrackSize("small");
    
    function getFirstFreeInsertIndex() {
        var btns = sf.ui.proTools.selectedTrack.invalidate().insertButtons;
        for (var i = 0; i < 10; i++)
            if (btns[i].value.invalidate().value === "unassigned") return i;
        return -1;
    }
    
    sf.ui.proTools.selectedTrack.trackInsertOrSendSearch({
        insertOrSend: 'Insert',
        pluginNumber: getFirstFreeInsertIndex() + 1,
        selectForAllSelectedTracks: true,    
    });
    
    if (isTrackTooSmall) {
    
        var f = sf.ui.proTools.selectedTrack.frame;
        var popupMenu = sf.ui.proTools.selectedTrack.popupMenuOpenFromElement({
            relativePosition: { x: f.w - 10, y: 5 },
            isOption: true,
            isShift: true,
        }).popupMenu;
        popupMenu.menuClickPopupMenu({
            menuPath: [originalTrackHeight]
        });
    }
    
    //close popup menu
    sf.ui.proTools.appActivateMainWindow();
    
    //Switch back to Mix window if needed.
    if (focusedWindow !== "Edit") {
        sf.ui.proTools.menuClick({ menuPath: ['Window', 'Mix'] });
    }
    
    

    Links

    User UID: yesbmwLVy0dRQLFNZzxo8kQx4ri2

    Feedback Key: sffeedback:yesbmwLVy0dRQLFNZzxo8kQx4ri2:-NUgFH9h-dbXaY7n-YWd

    Feedback ZIP

    • 8 replies
    1. Chris Shaw @Chris_Shaw2023-05-06 16:44:26.791Z2023-05-06 17:49:34.557Z

      try replacing lines 88 - 92 with this:

      //Search for plugin and check if user cancelled
      try {
          sf.ui.proTools.selectedTrack.trackInsertOrSendSearch({
              insertOrSend: 'Insert',
              pluginNumber: getFirstFreeInsertIndex() + 1,
              selectForAllSelectedTracks: true,
              onCancel: "ThrowError"
          });
      } catch (err) {
          sf.ui.proTools.appActivateMainWindow()
      }
      
      1. Actually, remove the throw 0 so the script will continue and reset your views
        (Never mind - I edited the code above)

        1. Ben Rubin @Ben_Rubin
            2023-05-06 21:25:10.434Z

            thanks so much @Chris_Shaw! This fixed that issue. Now I just have to pay attention to when it happens in other situations, often associated with a virtual deck closing.

            1. That is a known issue. If you're closing an on screen deck in a script you should follow it with a sf.ui.proTools.appActivateMainWindow()

        2. In reply toBen_Rubin:

          There seems to be an issue with sf.ui.proTools.selectedTrack.trackInsertOrSendSearch()

          If I run this on a selected track:

              sf.ui.proTools.selectedTrack.trackInsertOrSendSearch({
                  insertOrSend: 'Insert',
                  pluginNumber:  1,
                  selectForAllSelectedTracks: true,
                  onCancel: "Continue"
              });
          
              log ("Hello")
          

          The script will not continue and "Hello" isn't logged.
          Also, neither

          log(sf.ui.proTools.selectedTrack.trackInsertOrSendSearch({
              insertOrSend: 'Insert',
              pluginNumber: 1,
              selectForAllSelectedTracks: true,
              onCancel:"Continue"
          }).cancelledByUser)
          

          or

          log(sf.ui.proTools.selectedTrack.trackInsertOrSendSearch({
              insertOrSend: 'Insert',
              pluginNumber: 1,
              selectForAllSelectedTracks: true,
              onCancel:"Continue"
          }).userCancelled)
          

          return / log a boolean.
          Perhaps this is a bug.
          As a workaround I enclosed this method in a try/catch block and set the method to throw an error when the user cancels.

          1. In reply toBen_Rubin:

            FYI - I filed a bug report on this in the beta group.

            1. In reply toBen_Rubin:
              Ben Rubin @Ben_Rubin
                2023-05-06 23:34:24.381Z

                I've been using this for Input searches:

                sf.ui.proTools.selectedTrack.trackInputSearch
                

                and getting the same non-focus on cancel. What can I do to fix this?

                thanks
                ben

                1. Ben Rubin @Ben_Rubin
                    2023-05-07 01:41:55.277Z

                    ok this one I figured out on my own.

                    try {sf.ui.proTools.selectedTrack.trackInputSearch({ onCancel: "ThrowError" }); } catch (err) { sf.ui.proTools.appActivateMainWindow() }