No internet connection
  1. Home
  2. How to

How to select a file in open/save dialog by 1) file type and 2 )date added

By DJH @DJH
    2022-04-20 23:25:03.632Z

    I'm creating a macro that imports an AAF and Video. I've got everything working correctly right up until its time to select the relvant file (I'd post the script but it uses a bunch of other macros so theres not much code to post without going to each sub-macro and copy/pasting to a new script...which i'm happy to do it needed).

    Heres the step I'm at after automating the import sesion data process:

    99% of the time, there will only be one AAF (and one .mov) in the folder the macro directs to. So my main priority would be to script selecting an item by file type (i.e., an .AAF , or a .mov for the import video dialog).

    It would also be nice to have the script select the most recently added version of a file type in the cases where i need to import an updated AAF or .mov.

    How might i do this?

    • 25 replies

    There are 25 replies. Estimated reading time: 25 minutes

    1. Are you trying to select the file in the finder or in an open/save dialog?

      1. DDJH @DJH
          2022-04-21 04:03:58.907Z

          Hey Chris - good call - open/save dialog. I amended my thread title.

        • O
          In reply toDJH:

          Hey DJH here's my import video script I cobbled together from posts here.

          /**
           * @param {AxWindow} win
           * @param {string} path
           */
          function navigateToInDialog(win, path) {
              const numberOfTriesClickingGo = 20;
              const timeToWaitBetweenGoAttempts = 100;
          
              //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, try up to 10 times
              var goSuccess = false;
              for (var i = 0; i < numberOfTriesClickingGo; i++) {
                  try {
                      if (sheet.invalidate().buttons.whoseTitle.is('Go').first.elementClick({ onError: 'Continue' }).success) {
                          goSuccess = true;
                          break;
                      }
                  } catch (err) { }
          
                  sf.wait({ intervalMs: timeToWaitBetweenGoAttempts });
              }
              if (!goSuccess)
                  throw "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');
          }
          
          function importSession(sessionPath) {
              const numberOfTriesClickingOpen = 40;
              const timeToWaitBetweenOpenAttempts = 200;
          
              sf.ui.proTools.appActivateMainWindow();
          
              // Open up Import Session Data
              sf.ui.proTools.menuClick({ menuPath: ['File', 'Import', 'Video...'] });
          
              // Wait for window to open
              var importWin = sf.ui.proTools.windows.whoseTitle.is('Open').first;
          
              // Navigate to folder
              navigateToInDialog(importWin, sessionPath);
          
              // Click Open
              //Press Open, try up to 10 times
              var openSuccess = false;
              for (var i = 0; i < numberOfTriesClickingOpen; i++) {
                  try {
                      if (importWin.invalidate().buttons.whoseTitle.is('Open').first.elementClick({ onError: 'Continue' }).success) {
                          openSuccess = true;
                          break;
                      }
                  } catch (err) { }
          
                  sf.wait({ intervalMs: timeToWaitBetweenOpenAttempts });
              }
              if (!openSuccess)
                  throw "Could not click 'Open'";
          
              // Wait for import window to close
              importWin.elementWaitFor({ waitForNoElement: true });
          }
          
          
          function askForAafFile(subFolderName) {
              //path of current session
              var folderPath = (sf.ui.proTools.mainWindow.sessionPath.split('/').slice(0, -2).join('/'))
          
              var originalFilesPath = folderPath + "/" + subFolderName;
          
              var aafFileCandidates = sf.file.directoryGetFiles({
                  path: originalFilesPath,
                  isRecursive: true,
                  searchPattern: '*.mov'
              }).paths;
          
              var selectedPath = sf.interaction.popupSearch({
                  items: aafFileCandidates.map(p => ({
                      name: p.replace(originalFilesPath, '').substring(1),
                      path: p,
                  })),
              }).item.path;
          
              return selectedPath;
          }
          
          function main() {
          
              //selectLastTack();
              //setTimecode("10:00:00:00");
          
              var ptxPath = askForAafFile("TURNOVER");
              importSession(ptxPath);
          
              sf.ui.proTools.windows.whoseTitle.is("Video Import Options").first.elementWaitFor();
              sf.ui.proTools.windows.whoseTitle.is("Video Import Options").first.buttons.whoseTitle.is("OK").first.elementClick();
          
              
          
          
          }
          
          main();
          

          The parts you want to edit for your file path are here

          function askForAafFile(subFolderName) {
              //path of current session
              var folderPath = (sf.ui.proTools.mainWindow.sessionPath.split('/').slice(0, -2).join('/'))
          

          This goes up a number of folders from your session file path in my case I have to go up 2 steps because I go into 'Session folder' up one more to 'episode folder' then I drill down one folder below :

            var ptxPath = askForAafFile("TURNOVER");
          

          My 'TURNOVER' is your 'ASSETS' folder.

          My AAF verison of this is a template so i can do .ptx files and .omf files with it as well. I can share that in the store if you'd like, it works the same way as above.

          But with these controls.

          Dunno if my words make sense but give it a spin and see if it works for you. It's a little different from yours because it just goes ahead and prompts you to pick a .mov file from that folder instead of relying on date modified.

          1. DDJH @DJH
              2022-04-21 04:02:01.203Z

              Hey thanks a bunch, this looks really cool, I’ll give it a shot. Looks like it might get me most of the way there. How do you turn raw scripts into a template like that?

              1. Here's the documentation they have on Templates https://www.facebook.com/watch/live/?ref=watch_permalink&v=633065107406660 it takes a second to get your head around it but once you do it's great! I'll try and find some time to clean up my AAF template and post it to the store, it's just a little rough currently. If you're chomping at the bit I can just post it there in Alpha for you though.

                1. DDJH @DJH
                    2022-04-21 18:49:17.569Z

                    Thx for sharing that, def worth watching and got my gears turning. And thx for the offer to share…I think I’m gonna use this as an opportunity to get my hands a little dirty, maybe make my own template. Though I may come back soon with my tail between my legs and beg you to share, haha.

                    1. Haha, you got me inspired to stop slacking on finalizing these templates and posting them too. So like it or not they'll be in the Store soon. However I do think they'll need some itterations. It's one of those templates I think should just exist for all starting users anyways, I might try and pass the package off to @Kitch to put in the basic protools package after some QC from their team. (as if he doesn't have enough to do already :P)

                      1. 'Automated Protools Imports' is in the store now. Give it a spin see if you get errors.

                        1. DDJH @DJH
                            2022-04-23 23:05:03.322Z

                            thanks @Owen_Granich_Young - as you mentioned in the other thread you started, there is an issue with big sur. I'm on big sur which is why i'm assuming its not working for me. Getting an "exception in file directories..." error.

                            I am curious, though, for the sake of my existing script: Does your script/template automatically grab whatever movie file is in the folder? Or, once at the folder, do you still have to select it? BC I've actually got my existing script getting to the right place, just hoping to get it to automatically grab the file that's there...

                            1. Hey DJH,

                              Here's a video of my script working as intended. As you can see it looks in the folder path you have asked it to search in and after choosing your file it runs everything automatically.

                              helpful?
                              Owen

                              Hoping at some point to make the script big sur friendly.

                              1. DDJH @DJH
                                  2022-04-25 20:59:17.745Z

                                  This is cool, owen. Also like the aspect that spots to a certain time code. I'd still love to come up with a version of this type of macro that will just grab whatever video file is in the folder by some criteria, without user input...

                                  Curious: is the the big-sur related code issue is the same code (or same type of code) as the stock SF command ""Navigate to Folder of Current Session"? Because I have another macro (discussed here: Can't navigate to session folder in PT "save a copy" dialog) where that command/code is not working properly, though it does work in other places i've tried it and I'm not sure what the difference is.

                                  That said, I am planning on a Monterey update at some point soonish, and if things work better there, i may move the update up the priority list. You are on Monteray?

                                  1. Oh no, I'm in Mojave.

                                    I'm not sure if it's the same problem as there. When I had a buddy install it and try and take it for a spin the /Sheets window looks totally different in Big Sur and I kind of assumed it was that fact that was bugging it out. Been hoping to sus it out maybe at the webinar but I think I have to be on the stage on Wednsday.

                                    1. In reply toDJH:

                                      I think I've repaired Big Sur Compatibility, try it out if you will and let me know.

                                      1. DDJH @DJH
                                          2022-04-28 15:53:02.140Z

                                          Hey @Owen_Granich_Young - still some issues for me.

                                          • I got a little jumbled up with the folder path. By "steps back" I'd thought you meant the target folder lived above the session folder in the finder hierarchy. But this part of the script refers to subfolders within the session folder, yes? At least that's how it seems to work on my rig.

                                          • The video script does not return options for mp4s present in the folder (which is fine by me, don't typically use them much)

                                          • While the script does prompt me to select the video via the SF pop-up, once the script launches the actual PT import dialog, none of the selections I've made translate. In other words, I have to re-select the desired video, and the import options are not filled in according to the preset. I've gotten a couple of SF error notifications around this point, one related to go to sheets, and I think one related to UI elements.

                                          • when "timecode spot on" is "yes" in the preset, the first thing the script does is change PT counter to the correct timecode. It succeeds, however it seems to continue to perform the action several times before finally returning an error about how the script could not "ensure" the action was complete.

                                          Here is a log file with a bunch of attempts if helpful:
                                          https://www.dropbox.com/s/qxacziu25qa2eg6/soundflow.20220427.main.log?dl=0

                                          1. Aww bummer. Yeah it only looks for .mov files. I suppose I should build a toggle in for .mp4. But if it's not working for you anyways kinda of silly.

                                            Yeah steps back is probably not the greatest way to describe the process I wasn't sure how, 1 step is the session folder, 2 steps is the folder above session. Then it drills back down a step to the folder path no / required. Like the above import aaf one.

                                            Interesting that it kept failing on '78 - could not ensure main counter' I haven't seen that one fail before, most of the time it just bombed out on the Sheets in big sur.

                                            Will you take a screenshot of how you have your preset setup for me out of curiosity. And of your finder path you're trying to get it to follow?

                      2. D
                        In reply toDJH:
                        DJH @DJH
                          2022-04-28 17:03:24.638Z

                          The Folder path is working as you described. I’d thought “1” step pback would go one level up, and that perhaps “0” would be main session folder. but “2” will successfully find a folder one level above. And “1” will find all MOVs in the session folder or subfolders, and “1” + the name of a session folder-subfolder goes straight to the subfolder.

                          Here is screenshot of the preset that produces the line timecode command error:

                          1. Use SELECTION with Timecode Spot On. Or use SPOT in Locaiton Menu and toggle timecode spot off. Basically i built that so you don't have to use the spot dialog.

                            But all of it doesn't seem to matter because it's not working at the navigate SHEETS dialog, right?

                            1. DDJH @DJH
                                2022-04-29 03:20:01.123Z

                                Unfotunately, i get the same error with selection when timecode spot on is selected. And, yes, it can’t find the go to sheet.

                                1. Well, I may just let this die right now. Without a Big Sur rig to test and try again with, the coding is a bit beyond me as I've mostly just pirated and templated scripts off of the forum. When I'm not working a Wednesday I'll bring this script up in the Webinar and see if @Kitch can shine some light on where I'm going wrong. For now all 3 do work like gang busters in Mojave on 2021.12

                                  1. DDJH @DJH
                                      2022-04-29 19:42:46.152Z

                                      hear ya. idk if it helps, but the stock “navigate to folder of current session” command works for me on Big Sur in my current version of an import video/session data macro. maybe you can borrow code from that.

                                      I wish i was able to attend those webinars more easily. I’ve got fiddly questions that i’m sure a knowledgable person could answer EZ, but the forum here is kinda tumbleweeds. made a tinnnyyyyy start trying to learn JS, but thats gonna take some time (that i don’t have, haha).

                                      look forward to seeing how it goes once u can get to it. Thx

                                      1. Yeah, that's what I did this time actually. Replaced the code with the stock "navigate" was hoping that would do the trick. But something seems to be tripping it up still sadly.

                                        1. DDJH @DJH
                                            2022-04-29 19:51:32.404Z

                                            not that i know wtf i’m talking about, and maybe i’m just missing it, but taking a quick look at your code i don’t notice this line:

                                            if (sf.ui.frontmostApp.focusedWindow.title.value.match(/open|import/i)) {
                                            navigateToInDialog(folderPath);

                                            which i ran into an issue with on another macro i was trying to make...

                                            1. Yeah that's a line 38 although in my case it's selectedPath generated from another part of the script :

                                              I had literally hoped that would solve the problem lol.

                                              1. In reply toDJH:

                                                Hey @DJH somebody posted another Sheets fix so I updated in attempt to make it work with Big sur. If you have a free moment will you update and see if it works for you?

                                                Thanks
                                                Owen

                                                1. DDJH @DJH
                                                    2022-05-08 19:47:13.123Z

                                                    sorry, it still doesn't work in all the same ways, plus a new quirk:

                                                    • spot "yes" enabled does that same thing where it never completes an attempt to change the time code. Also, with spot selected, pro tools does not get activated. Previously when I tested presets, I ran the command straight from the "run command" button in soundflow. So this time, I had to make a new macro where ProTools is activated as the first step, then triggers the preset. But the same net result.

                                                    • With spot/timecode off, it still can't find the sheets.

                                                    FWIW, when it gets to the step where it should be doing sheets stuff, if I manually press "/" the go-to dialog does appear. So I wonder if it's a matter of putting the right kind of wait in there?

                                                    If it helps, in my "import" macro, this code does work for me to import a video and navigate to the correct folder:

                                                    sf.ui.proTools.appActivateMainWindow();
                                                    sf.ui.proTools.trackGetByName({ name: "MOV", makeVisible: true }).track.trackSelect();
                                                    //Calling command "Import - Video..." from package "undefined" (installed from user/pkg/version "srAasovvDiQacRZ2mcId4RrOA8R2/ckp49i4j60000a2100yfwywgf/ckyu15xww00004v10dq15rpgq")
                                                    sf.soundflow.runCommand({
                                                        commandId: 'user:ckp49i4j60000a2100yfwywgf:cktcnf0ra000x9r109i8wen1u#cktcmu1ek000l9r107c7eyej0',
                                                        props: {}
                                                    });
                                                    sf.ui.proTools.windows.whoseTitle.is("Open").first.children.whoseRole.is("AXStaticText").whoseValue.is("Select Video File To Import:").first.elementWaitFor();
                                                    
                                                    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('/') + '/ASSETS';
                                                    
                                                    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();
                                                    }
                                                    sf.ui.proTools.windows.whoseTitle.is("Video Import Options").first.elementWaitFor({
                                                        timeout: 20000,
                                                    });
                                                    sf.ui.proTools.windows.whoseTitle.is("Video Import Options").first.checkBoxes.whoseTitle.is("Import audio from file").first.checkboxSet({
                                                        targetValue: "Enable",
                                                    });
                                                    sf.ui.proTools.windows.whoseTitle.is("Video Import Options").first.buttons.whoseTitle.is("OK").first.elementClick();
                                                    sf.ui.proTools.windows.whoseTitle.is("Video Import Options").first.elementWaitFor({
                                                        waitType: "Disappear",
                                                        timeout: 2000,
                                                    });
                                                    sf.ui.proTools.windows.whoseTitle.is("Open").first.children.whoseRole.is("AXStaticText").whoseValue.is("Choose a destination folder").first.elementWaitFor({
                                                        waitType: "Appear",
                                                    });
                                                    sf.ui.proTools.windows.whoseTitle.is("Open").first.buttons.whoseTitle.is("Open").first.elementClick();
                                                    sf.ui.proTools.windows.whoseTitle.is("Open").first.children.whoseRole.is("AXStaticText").whoseValue.is("Choose a destination folder").first.elementWaitFor({
                                                        waitType: "Disappear",
                                                        timeout: 5000,
                                                    });
                                                    
                                                    

                                                    There's more code after that specific to my needs, but that's the gist of getting the video into a session and onto a track that I have established in my session template for video imports: "MOV". Line 3 is just pulling from the stock file menu command in soundflow.

                                                    Disclaimer: I had to copy-paste this code from a bunch of commands bc my actual macro pulls in several other macros, so there is no way to generate a script straight from my macro that doesn't have a bunch of command ID's. So not being used to doing this, it's possible I made an error copying/pasting all the code into one place.

                                                    I hope that helps.