No internet connection
  1. Home
  2. How to

How to create a new sub folder under my Pro Tools session folder with a template name

By Jonathan Grossman @Jonathan_Grossman
    2020-04-23 17:58:11.469Z

    I'd like to make a new empty folder with the session name and the suffix "_MIXES AND STEMS V1"

    Then I'd like another button that would make a new empty folder with the same name of an existing folder while amending the name to be the next version.

    For example, my session is named BALD GUY. I have a folder in my sessions Bounce Files folder named "BALD GUY MIXES AND STEMS V1". I'd like to have a new folder named "BALD GUY MIXES AND STEMS V2"

    Solved in post #2, click to view
    • 15 replies

    There are 15 replies. Estimated reading time: 17 minutes

    1. This script should get you started:

      function makeNewDirectory({ templatePath }) {
          var parentDirectory = templatePath.split('/').slice(0, -1).join('/');
          var directoryTemplateName = templatePath.split('/').slice(-1)[0];
      
          for (var i = 1; ; i++) {
              var directoryName = `${directoryTemplateName}${i}`;
              var directoryPath = parentDirectory + '/' + directoryName;
      
              if (!sf.file.directoryExists({ path: directoryPath }).exists) {
                  sf.file.directoryCreate({ path: directoryPath });
                  return directoryPath;
              }
          }
      }
      
      function main() {
          var sessionDirectory = sf.ui.proTools.mainWindow.sessionPath.split('/').slice(0, -1).join('/');
          var sessionName = sf.ui.proTools.mainWindow.sessionPath.split('/').slice(-1)[0].split('.').slice(0, -1).join('.');
      
          var newDirectory = makeNewDirectory({
              templatePath: `${sessionDirectory}/Bounced Files/${sessionName}_MIXES AND STEMS V`
          });
      
          sf.system.exec({ commandLine: `open "${newDirectory}"` });
      }
      
      main();
      

      It takes the current session, and in the sub folder Bounced Files it creates a new sub folder of the form "SESSIONNAME_MIXES AND STEMS V#" where SESSIONNAME is the name of the current PT session and # is the next free number, starting from 1.
      Finally, it opens the newly created folder in Finder.

      ReplySolution
      1. I LOVE PROGAMMING!
        Seems like magic to me. Makes me feel like I'm 3 years old watching bubble for the first time.

        1. In reply tochrscheuer:

          What would I adjust in the script if I now want to make a folder here:
          users/NSALAPTOP/Dropbox/04. 2 Clients/Mix Reviews +SessionName_MIXES AND STEMS V?

          Basically a duplicate of the folder I just created in the MIX REVIEW folder on dropbox for client download?

          1. You'd change the main function to something like this - make sure you get the path correct in the templatePath field - you had a plus sign which I'm assuming you meant to go in a sub folder, but anyway make sure to put the slashes as you want the folder structure to be:

            function main() {
                var sessionName = sf.ui.proTools.mainWindow.sessionPath.split('/').slice(-1)[0].split('.').slice(0, -1).join('.');
            
                var newDirectory = makeNewDirectory({
                    templatePath: `/Users/NSALAPTOP/Dropbox/04. 2 Clients/Mix Reviews/${sessionName}_MIXES AND STEMS V`
                });
            
                sf.system.exec({ commandLine: `open "${newDirectory}"` });
            }
            
            1. When this folder is created, the window size is small so I have to extend the columns to see everyting in it. I have adjusted my overall finder window setting to a preffered size but this script won't adhere to those settings. See below screen shots

              this is how it looks now.

              want it to come up this size.

              1. Instead of this line:

                 sf.system.exec({ commandLine: `open "${newDirectory}"` });
                

                Try using this - and fill in the coordinates and size accordingly:

                sf.appleScript.finder.open(newDirectory);
                sf.ui.finder.appActivateMainWindow();
                sf.ui.finder.windows.first.windowMove({
                    position: { x: 0, y: 0 },
                    size: { x: 1920, y: 400 },
                });
                
                1. Great! That worked. Tiny thing, but can I adjust where it lands on the screen? I work with a pretty wide center screen and would love it to land in the middle. Right now it's upper left

                  1. Just change the x and y properties under position above :)

        2. J
          Jonathan Grossman @Jonathan_Grossman
            2020-05-04 19:10:23.773Z2020-10-21 18:01:58.248Z

            Loving this script...
            what do I add to it so I automatically go back to ProTools after the local mix folder has been created. Right now it's just staying on the finder view where I created the latest folder. Here's the code I am using.

            function makeNewDirectory({ templatePath }) {
                var parentDirectory = templatePath.split('/').slice(0, -1).join('/');
                var directoryTemplateName = templatePath.split('/').slice(-1)[0];
            
                for (var i = 1; ; i++) {
                    var directoryName = `${directoryTemplateName}${i}`;
                    var directoryPath = parentDirectory + '/' + directoryName;
            
                    if (!sf.file.directoryExists({ path: directoryPath }).exists) {
                        sf.file.directoryCreate({ path: directoryPath });
                        return directoryPath;
                    }
                }
            }
            
            function main() {
                var sessionDirectory = sf.ui.proTools.mainWindow.sessionPath.split('/').slice(0, -1).join('/');
                var sessionName = sf.ui.proTools.mainWindow.sessionPath.split('/').slice(-1)[0].split('.').slice(0, -1).join('.');
            
                var newDirectory = makeNewDirectory({
                    templatePath: `${sessionDirectory}/Bounced Files/${sessionName}_MIXES AND STEMS V`
                });
            
                sf.system.exec({ commandLine: `open "${newDirectory}"` });
            }
            
            main();
            
            1. Hey Christian - hope you're welll. You made a script for me to save the ProTools session as MIX_V1 and create the named bounced files folder, and move the current session to the Old Sessions folder. It has suddenly stopped working. Here's the log:
              20.10.2020 18:04:43.06 [Backend]: #App: Activate "com.avid.ProTools" -> PT Deck [ck99zpsq5000j6e10f9hx9wg8]

              20.10.2020 18:04:43.07 [Backend]: Showing Deck " PT Deck " on Device "NSA TOWER STREAMDECK" (We were called with device "NSA TOWER STREAMDECK")
              [StreamDeck] Device 'NSA TOWER STREAMDECK' instantiating deck ' PT Deck '

              20.10.2020 18:04:45.90 [Backend]: #StreamDeck: KeyDown (8,3) -> SAVE SESSION AS _MIX_V1 + create bounce folder

              20.10.2020 18:04:45.90 [Backend]: >> Command: SAVE SESSION AS _MIX_V1 + create bounce folder [user:ck99zpoez000h6e10rjx8vg5g:ckay4dwr300004v10jdbou8m3]

              20.10.2020 18:04:51.78 [Backend]: Logging error in action (01) ElementArrayWaitForAction: Element was not found or removed after waiting 5000 ms

              20.10.2020 18:04:51.78 [Backend]: !! Command Error: SAVE SESSION AS _MIX_V1 + create bounce folder [user:ck99zpoez000h6e10rjx8vg5g:ckay4dwr300004v10jdbou8m3]:
              Element was not found or removed after waiting 5000 ms (SAVE SESSION AS _MIX_V1 + create bounce folder: Line 19)

              << Command: SAVE SESSION AS _MIX_V1 + create bounce folder [user:ck99zpoez000h6e10rjx8vg5g:ckay4dwr300004v10jdbou8m3]

              1. btw - it seems to have a problem when the title of the ProTool session is long, perhaps goes beyond what is visible in the save as dialog bog. This session title was giving me trouble so you might test with this.

                PRD_WBP_PROMO20_INSANE_M1-104291_V5_MIX_V1

                1. Kitch Membery @Kitch2020-10-21 18:49:03.072Z

                  Hi Jonathan,

                  I tried the script with a session named "PRD_WBP_PROMO20_INSANE_M1-104291_V5_MIX_V1" It seems to be working here for me however, one posible reason why the script may not be working is that the main window may need invalidating. I've added main window invalidation in the following script;

                  function makeNewDirectory({ templatePath }) {
                      var parentDirectory = templatePath.split('/').slice(0, -1).join('/');
                      var directoryTemplateName = templatePath.split('/').slice(-1)[0];
                  
                      for (var i = 1; ; i++) {
                          var directoryName = `${directoryTemplateName}${i}`;
                          var directoryPath = parentDirectory + '/' + directoryName;
                  
                          if (!sf.file.directoryExists({ path: directoryPath }).exists) {
                              sf.file.directoryCreate({ path: directoryPath });
                              return directoryPath;
                          }
                      }
                  }
                  
                  function main() {
                      sf.ui.proTools.mainWindow.invalidate();
                  
                      var sessionDirectory = sf.ui.proTools.mainWindow.sessionPath.split('/').slice(0, -1).join('/');
                      var sessionName = sf.ui.proTools.mainWindow.sessionPath.split('/').slice(-1)[0].split('.').slice(0, -1).join('.');
                  
                      var newDirectory = makeNewDirectory({
                          templatePath: `${sessionDirectory}/Bounced Files/${sessionName}_MIXES AND STEMS V`
                      });
                  
                      sf.file.open({ path: newDirectory });
                      sf.wait({intervalMs:300});
                      sf.ui.proTools.appActivateMainWindow();
                  }
                  
                  main();
                  

                  I've also added the a 300 ms wait and the following code sf.ui.proTools.appActivateMainWindow(); so that the Pro Tools main window will come back into focus.

                  Hope that helps.

                  1. JJonathan Grossman @Jonathan_Grossman
                      2020-10-21 19:08:48.143Z2020-10-21 19:31:10.448Z

                      Thanks Kitch! Although I think the chain referenced an older script. here's the current one.

                      function saveSessionAsNewVersion() {
                      
                          //First save the current version
                          sf.ui.proTools.getMenuItem('File', 'Save').elementClick({}, function () { }); //can be disabled
                      
                          //Get our filename without extension
                          var fileNameWithoutExtension = sf.ui.proTools.mainWindow.invalidate().sessionPath.split('/').slice(-1)[0].split('.').slice(0, -1).join('.');
                      
                          //Create a new name (get the name and suffixed number - if your session name was 'My Session 42' then save 'My Session ' in grps[1] and '42' in grps[2])
                          var grps = fileNameWithoutExtension.match(/^(.+?)(\d+)$/);
                      
                          //Construct a new name based on the prefix + MIX_V1
                          var newName = (fileNameWithoutExtension) + '_MIX_V1.ptx';
                      
                          //Invoke File Save As.
                          sf.ui.proTools.getMenuItem('File', 'Save As...').elementClick();
                      
                          //Wait for the Save dialog to appear
                          var dlg = sf.ui.proTools.windows.invalidate().whoseTitle.is('Save').waitFor().element;
                      
                          //Enter the new name
                          dlg.textFields.first.value.value = newName;
                      
                          //Click Save
                          dlg.buttons.whoseTitle.is('Save').first.elementClick();
                      
                      }
                      
                      function moveFileToOldSessions(path) {
                          var oldSessionsDir = path.split('/').slice(0, -1).join('/') + '/Old Sessions';
                          var filename = path.split('/').slice(-1)[0];
                      
                          //Make sure "Old Sessions" dir exists
                          sf.file.directoryCreate({
                              path: oldSessionsDir,
                          });
                      
                          //Move file there
                          sf.file.move({
                              sourcePath: path,
                              destinationPath: oldSessionsDir + '/' + filename,
                          });
                      }
                      
                      sf.ui.proTools.mainWindow.invalidate();
                      var oldSessionPath = sf.ui.proTools.mainWindow.sessionPath;
                      
                      saveSessionAsNewVersion();
                      
                      sf.wait({ intervalMs: 1000 });
                      
                      moveFileToOldSessions(oldSessionPath);
                          
                      
                      var sessionPath = sf.ui.proTools.mainWindow.invalidate().sessionPath;
                      var sessionDir = sessionPath.split('/').slice(0, -1).join('/');
                      var sessionName = sessionPath.split('/').slice(-1)[0].split('.').slice(0, -1).join('.');
                      var mixesDir = `${sessionDir}/Bounced Files/${sessionName}_MIXES AND STEMS`;
                      
                      //Make sure the mixes directory exists
                      sf.file.directoryCreate({
                          path: mixesDir,
                      });
                      
                      1. Kitch Membery @Kitch2020-10-21 19:36:24.165Z

                        It seems like a short wait was needed before the "Save" dialog opened;

                        Let me know if this works for you;

                        function saveSessionAsNewVersion() {
                            //First save the current version
                            sf.ui.proTools.getMenuItem('File', 'Save').elementClick({}, function () { }); //can be disabled
                        
                            //Get our filename without extension
                            var fileNameWithoutExtension = sf.ui.proTools.mainWindow.invalidate().sessionPath.split('/').slice(-1)[0].split('.').slice(0, -1).join('.');
                        
                            //Create a new name (get the name and suffixed number - if your session name was 'My Session 42' then save 'My Session ' in grps[1] and '42' in grps[2])
                            var grps = fileNameWithoutExtension.match(/^(.+?)(\d+)$/);
                        
                            //Construct a new name based on the prefix + MIX_V1
                            var newName = (fileNameWithoutExtension) + '_MIX_V1.ptx';
                        
                            //Invoke File Save As.
                            sf.ui.proTools.getMenuItem('File', 'Save As...').elementClick();
                        
                            sf.wait({intervalMs:200});
                        
                            //Wait for the Save dialog to appear
                            var dlg = sf.ui.proTools.windows.whoseTitle.is('Save').waitFor().element;
                        
                            //Enter the new name
                            dlg.textFields.first.value.value = newName;
                        
                            //Click Save
                            dlg.buttons.whoseTitle.is('Save').first.elementClick();
                        }
                        
                        function moveFileToOldSessions(path) {
                            var oldSessionsDir = path.split('/').slice(0, -1).join('/') + '/Old Sessions';
                            var filename = path.split('/').slice(-1)[0];
                        
                            //Make sure "Old Sessions" dir exists
                            sf.file.directoryCreate({
                                path: oldSessionsDir,
                            });
                        
                            //Move file there
                            sf.file.move({
                                sourcePath: path,
                                destinationPath: oldSessionsDir + '/' + filename,
                            });
                        }
                        
                        sf.ui.proTools.mainWindow.invalidate();
                        var oldSessionPath = sf.ui.proTools.mainWindow.sessionPath;
                        
                        saveSessionAsNewVersion();
                        
                        sf.wait({ intervalMs: 1000 });
                        
                        moveFileToOldSessions(oldSessionPath);
                        
                        var sessionPath = sf.ui.proTools.mainWindow.invalidate().sessionPath;
                        var sessionDir = sessionPath.split('/').slice(0, -1).join('/');
                        var sessionName = sessionPath.split('/').slice(-1)[0].split('.').slice(0, -1).join('.');
                        var mixesDir = `${sessionDir}/Bounced Files/${sessionName}_MIXES AND STEMS`;
                        
                        //Make sure the mixes directory exists
                        sf.file.directoryCreate({
                            path: mixesDir,
                        });
                        
                        1. That seems to be the magic trick. Thanks Kitch!