No internet connection
  1. Home
  2. How to

Open a File from Project Path

By Khonnor Wallace @Khonnor_Wallace
    2020-05-02 16:10:54.392Z

    Hello! I'm trying to open a file from the Project Path and check if it exists and if not (show a message saying that the files doesn't exists) and ask the user for the correct path and open it. Any thoughts? I think it's very easy, but I'm still learning the code ;) Thank you!

    Solved in post #4, click to view
    • 24 replies

    There are 24 replies. Estimated reading time: 15 minutes

    1. Hi Khonnor

      Can you elaborate on what you mean by opening a file from the project path? And opening it how? Is this a txt file or something that should exist in the same directory as the open Pro Tools session?

      1. Khonnor Wallace @Khonnor_Wallace
          2020-05-02 16:17:29.278Z

          Thanks for the answer Christian! A txt, excel or html file that exists in the same directory as the open PT Session.

          1. Something like this should get you going:

            
            var projectDir = sf.ui.proTools.mainWindow.sessionPath.split('/').slice(0, -1).join('/');
            var filePath = `${projectDir}/notes.txt`;
            
            if (!sf.file.exists({ path: filePath }).exists) {
                //The file doesn't exist. Ask the user
                filePath = sf.interaction.selectFile({
                    defaultLocation: projectDir,
                    prompt: 'Please select a file to open',
                }).path;
            }
            
            sf.file.open({ path: filePath });
            
            Reply2 LikesSolution
            1. Khonnor Wallace @Khonnor_Wallace
                2020-05-02 16:34:17.771Z

                Awesome Christian!! Thanks a lot! ;)

          2. In reply toKhonnor_Wallace:

            I'd like a script that does something similar to this one.

            It would search the PT session path for a file that ends with "BU" (a Synchronize! X setup document). If it doesn't find one, then it would move up to the parent directory, search there, until it reaches the main directory of the drive. If it reaches the top level of the drive then the backup file doesn't exist so it will just open my backup program - Synchronize! X

            1. Chris Shaw @Chris_Shaw2020-05-21 17:16:59.782Z2020-05-21 21:02:06.111Z

              Here's a screen shot to explain a bit better. I'm mixing a song called Becca Bell for an album project by an artist named Judson McKinney. There is no file ending with "BU" in the session folder so I'd want SF to move up one directory to BECCA BELL 5.13.2020. The backup doc doesn't exist there either,. SF moves up one more directory (Justin McKinney) and there is the document ending with "BU". SF opens it and I can start the backup. If it didn't exist there SF would move up one more directory then be in the root directory of "Work Drive" where no "BU" document exists which then would trigger SF to open up my backup program Synchronize! X

              I've tried to work it out myself but it seems above my expertise…

              Thanks

              1. Andrew Scheps @Andrew_Scheps
                  2020-05-22 17:51:42.426Z

                  Hi Chris,

                  I'm actually working on this for you because I think it would be a very cool script to have. I'm a couple of steps away from having it but I had a question for you. Would it be better to search for files with the correct file extension (I think in this case it's .syncprox) so that there can't be false positives? Would you ever have other sync X files in the directories? Otherwise it might be better to come up with a more robust naming scheme for these files.

                  1. In reply toChris_Shaw:
                    Andrew Scheps @Andrew_Scheps
                      2020-05-22 19:11:45.050Z

                      Ok, here's a script that does what you want, but instead of looking for part of a filename (which as I said above doesn't seem very robust) this searches for a filename extension which is defined as a constant at the top of the script so you can change it if you want (make sure to leave the period off the beginning of the extension). It the searches recursively from the session directory up to the root directory of the drive and if it still can't find a file it will ask you to find one (thanks Christian).

                      Only real limitation of the script is that if there is a directory with two files with the same extension it will open the first one alphabetically.

                      
                      //The file extension for saved sync files
                      const extension = 'syncprox'
                      // This sets the search directory to the current session folder
                      var sessionDir = sf.ui.proTools.mainWindow.sessionPath;
                      var searchDir = ''
                      var directoryDepth = sessionDir.split('/').length - 1 //how deep in the file structure are we, then adjust to keep from going up past the root of the drive
                      var level, index, value, filePath;
                      
                      for (level = 1; level < directoryDepth; level++) {
                          searchDir = sessionDir.split('/').slice(0, -level).join('/')
                          let entries = sf.file.directoryGetEntries({
                              path: searchDir,
                              includeDirectories: false,
                          }).paths;
                      
                          for (index = 0; index < entries.length; ++index) {
                              value = entries[index];
                              if (entries[index].split('.')[1] === extension) {
                                  filePath = value;
                                  break;
                              }
                          }
                          if (filePath != undefined){break} //We found it, don't search up a level
                      }
                      
                      if (filePath == undefined) {
                          //The file doesn't exist. Ask the user
                          filePath = sf.interaction.selectFile({
                              defaultLocation: sessionDir.split('/').slice(0, -1).join('/'),
                              prompt: 'Please select a file to open',
                          }).path;
                      }
                      
                      sf.file.open({ path: filePath });
                      
                      1. Thanks for this Andrew!
                        For some reason I can't get my Mac to show the file extension for Synchronize!X files. However I consistently label al my backup files with this format "project/artist name - BU" so searching for "- BU" should work for me.
                        I very rarely have more than one backup file document per project.

                        I'll give this a go and let you know how it works.
                        Thanks again!

                        1. Andrew Scheps @Andrew_Scheps
                            2020-05-22 22:43:31.742Z

                            No worries. If you get info in the finder on a file it should show up there and the script can see it even if they're hidden in the finder.

                            1. That's the thing - Get Info doesn't show a file type.
                              I'll email you a copy of the file - it's tiny. Perhaps you can make sense of it.

                            2. In reply toChris_Shaw:

                              Unfortunately this doesn't seem to be working. I can't get my mac to display the file extension for the Synchronize! X document which is really weird (not even in the terminal) so I can't tell if you're using the right extension. IF you know of a way to display the file type besides changing the Finder Prefs let me know.

                              In the meantime, is it possible to change the script to open any file that ends with "- BU"?

                              1. To be clear I think the script is working properly but it isn't finding the backup file. Even if I place it directly in the session folder.

                                1. Andrew Scheps @Andrew_Scheps
                                    2020-05-22 23:35:50.302Z

                                    Wow, that's so weird! I have the Pro version and the files have the file extension, these don't. It's a little different to do a part of the filename so give me a sec...

                                    1. In reply toChris_Shaw:
                                      Andrew Scheps @Andrew_Scheps
                                        2020-05-22 23:43:31.162Z

                                        Ok, here's a new one. This looks for a '-' in the filename and then sees if the only text after it is ' BU'. It's not as foolproof as using an extension but should work.

                                        
                                        //The file extension for saved sync files
                                        const string = ' BU'
                                        // This sets the search directory to the curren session folder
                                        var sessionDir = sf.ui.proTools.mainWindow.sessionPath;
                                        var searchDir = ''
                                        var directoryDepth = sessionDir.split('/').length - 1 //how deep in the file structure are we, then adjust to keep from going up oast the root of the drive
                                        var level, index, value, filePath;
                                        
                                        for (level = 1; level < directoryDepth; level++) {
                                            searchDir = sessionDir.split('/').slice(0, -level).join('/')
                                            let entries = sf.file.directoryGetEntries({
                                                path: searchDir,
                                                includeDirectories: false,
                                            }).paths;
                                        
                                            for (index = 0; index < entries.length; ++index) {
                                                value = entries[index];
                                                if (entries[index].split('-')[1] === string) {
                                                    filePath = value;
                                                    break;
                                                }
                                            }
                                            if (filePath != undefined){break} //We found it, don't search up a level
                                        }
                                        
                                        if (filePath == undefined) {
                                            //The file doesn't exist. Ask the user
                                            filePath = sf.interaction.selectFile({
                                                defaultLocation: sessionDir.split('/').slice(0, -1).join('/'),
                                                prompt: 'Please select a file to open',
                                            }).path;
                                        }
                                        
                                        sf.file.open({ path: filePath });
                                        
                                        1. Andrew Scheps @Andrew_Scheps
                                            2020-05-22 23:54:35.268Z

                                            Here's one that's slightly safer. The previous one wouldn't find your backup file if there was a hyphen anywhere else in the file name. This one checks the end of the filename for ' - BU' which should be unique I hope.

                                            
                                            //The ending string in the filename for saved sync files
                                            const string = ' - BU'
                                            // This sets the search directory to the current session folder
                                            var sessionDir = sf.ui.proTools.mainWindow.sessionPath;
                                            var searchDir = ''
                                            var directoryDepth = sessionDir.split('/').length - 1 //how deep in the file structure are we, then adjust to keep from going up past the root of the drive
                                            var level, index, value, filePath;
                                            
                                            for (level = 1; level < directoryDepth; level++) {
                                                searchDir = sessionDir.split('/').slice(0, -level).join('/')
                                                let entries = sf.file.directoryGetEntries({
                                                    path: searchDir,
                                                    includeDirectories: false,
                                                }).paths;
                                            
                                                for (index = 0; index < entries.length; ++index) {
                                                    value = entries[index];
                                                    if (entries[index].slice(-5) === string) {
                                                        filePath = value;
                                                        break;
                                                    }
                                                }
                                                if (filePath != undefined){break} //We found it, don't search up a level
                                            }
                                            
                                            if (filePath == undefined) {
                                                //The file doesn't exist. Ask the user
                                                filePath = sf.interaction.selectFile({
                                                    defaultLocation: sessionDir.split('/').slice(0, -1).join('/'),
                                                    prompt: 'Please select a file to open',
                                                }).path;
                                            }
                                            
                                            sf.file.open({ path: filePath });
                                            
                              2. In reply toKhonnor_Wallace:

                                Hell. Yeah!
                                That works! I think my "-BU" naming convention should be pretty bullet proof for me because I use it very consistently.

                                Thanks so much!

                                1. In reply toKhonnor_Wallace:

                                  This is going to be a heavily used / useful script.
                                  BTW - I broke down and got a Stream Deck (5x3). I was programming pages in the Avid control app but coming up with unique keystrokes became tiresome and moving softkeys around in it is excruciating. Plus the portability of bringing a StreamDeck to another studio and not having to copy over pref files for Eucon etc makes life so much easier.

                                  1. Andrew Scheps @Andrew_Scheps
                                      2020-05-23 00:10:23.478Z

                                      Yeah, the Stream Deck really changes things. I can't remember keystrokes no matter how logical I think I'm being so I've got tons of stuff on buttons and decks and I'm trying to be smarter about having the decks load dynamically. I'm working on some very cool scripts to use the SD more interactively too (bypassing plugins using SD buttons anyone?)

                                      1. Christian mentioned somewhere that they're working on making the buttons more dynamic in the way they display information. That will be a game changer. Plugin buttons for bypass that display the plugin names depending on track selection would be cool. Or how about having system usage percentage on a button.
                                        So many possibilities.
                                        I'm more excited about SF updates than PT updates…

                                        1. Andrew Scheps @Andrew_Scheps
                                            2020-05-23 01:45:28.038Z

                                            I have the first one working already, just trying to fix one last bug.

                                      2. In reply toKhonnor_Wallace:
                                        Daniel Perez @daniel_perez
                                          2022-08-12 13:46:46.696Z

                                          I'd like to just open the Audio Files Folder from the current session I'm using. Could you paste the right code for me?

                                          @Chris_Shaw

                                          1. LLukas paschke @Lukas_paschke
                                              2024-08-08 16:12:05.840Z

                                              same here

                                              1. Here you go!

                                                function openCurrentSessionAudioFilesFolder() {
                                                    if (!sf.app.proTools.hasOpenSession) throw "No session is currently open.";
                                                
                                                    const sessionPath = sf.ui.proTools.mainWindow.sessionPath;
                                                    const parentPath = sessionPath.split("/").slice(0, -1).join("/");
                                                    const audioFilesPath = `${parentPath}/Audio Files`;
                                                
                                                    sf.file.open({
                                                        path: audioFilesPath
                                                    });
                                                }
                                                
                                                openCurrentSessionAudioFilesFolder();