No internet connection
  1. Home
  2. How to

Reveal and select a file in Finder without opening it

By Andrew Sherman @Andrew_Sherman
    2021-02-22 17:01:33.114Z

    I'm looking for a way to reveal/select a file in Finder, but not to open the file with the default application.

    I've tried using something like this, but the file opens in Quicktime. I just want the command to open a new (or existing) Finder window and select the file so that I can do something further with it manually.

    sf.directory.open({
        path: /Users/name/path/file.mp4,
    });
    
    Solved in post #14, click to view
    • 18 replies

    There are 18 replies. Estimated reading time: 12 minutes

    1. Chris Shaw @Chris_Shaw2021-02-22 17:25:07.155Z2021-02-22 17:32:50.913Z

      This script navigates to my template session. Should work for what you're looking to do. Just change the path in the sf.keyboard.type line:

      // Navigate to template session file
      sf.ui.finder.menuClick({
          menuPath: ["Go", "Go to Folder…"],
      });
      
      sf.keyboard.type({
          text: '/Users/cshaw340/Desktop/Pro Tools Templates etc/NEW Mix Setup Template - PT2020.ptx'
      });
      sf.ui.finder.windows.whoseTitle.is("Go to Folder").first.elementWaitFor();
      sf.ui.finder.mainWindow.getElement("AXDefaultButton").elementClick();
      
      
      1. I've edited this since I first posted.

        1. AAndrew Sherman @Andrew_Sherman
            2021-02-22 18:03:18.614Z

            This is great, thank you Chris. i found that it was pasting the path into Soundflow editor if I triggered it from there so I added:

            sf.ui.finder.appActivate();
            

            However I can only get bit to work by including a 500ms wait at the end, and keypress of return afterwards, it doesn't can't find the windows. I'm on Apple Silicon Big Sur 11.0.1 if that makes any difference. I can see in the "Go to Folder" dialog that it says "Go to the folder" instead of "Go to folder", but when I change it in the code it still doesn't find it. Do you have any tips on how to get it to recognise the correct windows?

            1. My apologies for not including sf.ui.finder.appActivate();.
              Here's a revised script:

              // Navigate to template session file
              
              sf.ui.finder.appActivate();
              
              sf.ui.finder.menuClick({
                  menuPath: ["Go", "Go to Folder…"],
              });
              sf.wait ({intervalMs:300});
              
              sf.keyboard.type({
                  text: '/Users/cshaw340/Desktop/Pro Tools Templates etc/NEW Mix Setup Template - PT2020.ptx'
              });
              
              sf.wait ({intervalMs:300});
              
              sf.keyboard.press({keys:'return'})
              
              

              You may want to double check that the path is correct. To do so select the file in the Finder and while holding the option key select Edit from the Finder menu. You'll notice that Copy has changed from "Copy" to "Copy [filename] as Pathname". (Alternatively you can use opt-cmd C). Paste this into the ``sf.keyboard.type` line in the script.

              Generally speaking, you should try to avoid using sf.keyboard.type whenever possible but I do in this instance because the script may fail depending if there is a window open in the Finder or not (which opens the "Go to Folder" in either an existing window or a Go to Folder dialog if there is no Finder window open)

              1. AAndrew Sherman @Andrew_Sherman
                  2021-02-22 18:53:48.713Z

                  Thanks for this Chris, perhaps I didn't explain myself properly (apologies); I was asking if I was missing something by trying with the wrong UI window elements. When I pick the Finder elements they're named slightly differently from yours. Now that we've got the "activate app" section in there (and the path isn't getting pasted into the script!), it's working perfectly.

                  Which of the following would you recommend using for the ending?

                  Option 1:

                  sf.ui.finder.mainWindow.sheets.first.elementWaitFor();
                  sf.ui.finder.mainWindow.sheets.first.getElement('AXDefaultButton').elementClick();
                  

                  Option 2:

                  sf.wait ({intervalMs:500});
                  sf.keyboard.press({keys:'return'})
                  

                  Script that's working for me:

                  // Pathname goes here
                  var itemName = '/Users/username/path/file.mp4';
                  
                  sf.ui.finder.appActivate();
                  
                  sf.ui.finder.menuClick({
                      menuPath: ["Go", "Go to Folder…"],
                  });
                  
                  sf.keyboard.type({
                      text: itemName
                  });
                  
                  sf.ui.finder.mainWindow.sheets.first.elementWaitFor();
                  sf.ui.finder.mainWindow.sheets.first.getElement('AXDefaultButton').elementClick();
                  
                  1. Chris Shaw @Chris_Shaw2021-02-22 19:14:50.960Z2021-02-22 19:24:33.453Z

                    The first option would only work if there is already a window open in the Finder. If no window is open then "Go to Folder" would open a dialog box and since there are no sheets in a dialog box the dialog will not close and the file won't be selected. (Close all Finder windows to see what I'm talking about.)

                    So let's cover each situation

                    // Navigate to template session file
                    
                    //Switch to Finder
                    sf.ui.finder.appActivate();
                    
                    //Set path to desired file
                    sf.ui.finder.menuClick({
                        menuPath: ["Go", "Go to Folder…"],
                    });
                    sf.wait({ intervalMs: 300 });
                    
                    sf.keyboard.type({
                        text: '/Users/cshaw340/Desktop/Pro Tools Templates etc/NEW Mix Setup Template - PT2020.ptx'
                    });
                     
                     //If "Go to File" is dialog box then click it's Go button otherwise click "Go" in Finder window sheet
                    sf.wait({intervalMs:300})
                    if (sf.ui.finder.windows.whoseTitle.is("Go to Folder").exists) {
                        sf.ui.finder.mainWindow.buttons.whoseTitle.is("Go").first.elementClick();
                    
                    } else {
                        sf.ui.finder.mainWindow.sheets.first.elementWaitFor();
                        sf.ui.finder.mainWindow.sheets.first.getElement('AXDefaultButton').elementClick();
                    }
                    
                    1. AAndrew Sherman @Andrew_Sherman
                        2021-02-22 19:22:45.358Z

                        Excellent - that seems pretty robust. Thank you Chris!

                        1. I've edited it slightly (again :) )
                          Glad I was able to help.
                          //CS//

            2. M
              In reply toAndrew_Sherman:
              Metod @metod.komatar
                2021-09-29 17:26:33.671Z

                Suddenly stoped working... I have this long code in my script and it has worked since a few days ago... now it says no files found. Than I have tried new script / shorter, which can be found in pt package and it also doesn't work - error says "could not open popup menu"

                1. When you get that error, does it also give you a line #? If so what is it?

                  1. MMetod @metod.komatar
                      2021-09-30 09:19:01.675Z

                      Yes in second option - when using script from pt package, it gives line #3... line 3 in this script is:
                      " selectedClipRows[0].children.allItems[1].children.first.popupMenuSelect({ "

                  2. M
                    In reply toAndrew_Sherman:
                    Metod @metod.komatar
                      2021-10-08 10:36:06.641Z

                      No idea?

                      1. In reply toAndrew_Sherman:

                        The canonical way of doing this is:

                        const path = '/Users/cshaw340/Desktop/Pro Tools Templates etc/NEW Mix Setup Template - PT2020.ptx';
                        sf.system.exec({
                            commandLine: `open -R "${path}"`
                        });
                        
                        Reply2 LikesSolution
                        1. AAndrew Sherman @Andrew_Sherman
                            2021-10-08 12:14:29.696Z

                            This is very handy to know!

                            1. Yay :)

                              Can also be done via:

                              const path = '/Users/chr/Desktop/Screenshot 2021-09-19 at 21.40.12.png';
                              sf.appleScript.finder.select(path);
                              sf.ui.finder.appActivateMainWindow();
                              
                              1. However, due to the limitations in Apple's security sandbox, I would recommend the sf.system.exec option, as it will work more reliably.

                          • M
                            In reply toAndrew_Sherman:
                            Metod @metod.komatar
                              2021-10-24 15:47:42.776Z

                              One of two commands suddenly started working again... second one in my library doesn't work... but it's ok for me.

                              This is the code that works:

                              function getClipFilename() {
                              sf.ui.proTools.appActivateMainWindow();

                              //Clear search, to ensure Clip Rename will work
                              sf.keyboard.press({
                                  keys: "cmd+shift+d",
                              });
                              
                              // Open Rename, copy Filename and close Rename
                              sf.ui.proTools.menuClick({
                                  menuPath: ["Clip", "Rename..."],
                              });
                              
                              var nameWin = sf.ui.proTools.windows.whoseTitle.is('Name').first;
                              nameWin.elementWaitFor({}, `Clip Rename didn't work`);
                              
                              let nameElement = nameWin.groups.whoseTitle.is('Clip Info').first.children.whoseRole.is("AXStaticText").allItems[2];
                              let name = nameElement.value.value;
                              
                              nameWin.buttons.whoseTitle.is('OK').first.mouseClickElement();
                              nameWin.elementWaitFor({ waitForNoElement: true });
                              
                              return name;
                              

                              }

                              function locateFile({
                              filename,
                              searchPaths
                              }) {
                              var ext = filename.split('.').slice(-1)[0].toLowerCase();
                              var filenameLowerCase = filename.toLowerCase();

                              var candidates = [];
                              for (let searchPath of searchPaths) {
                                  for (let candidatePath of sf.file.directoryGetFiles({
                                      path: searchPath,
                                      searchPattern: `*.${ext}`,
                                      isRecursive: true
                                  }).paths.filter(p => p.split('/').slice(-1)[0].toLowerCase() === filenameLowerCase)) {
                                      candidates.push(candidatePath);
                                  }
                              }
                              
                              return candidates;
                              

                              }

                              function main() {

                              let sessionFolder = sf.ui.proTools.mainWindow.sessionPath.split('/').slice(0, -1).join('/');
                              let audioFilesFolder = `${sessionFolder}/Audio Files`;
                              
                              let clipFilename = getClipFilename();
                              
                              let candidates = locateFile({
                                  filename: clipFilename,
                                  searchPaths: [
                                      audioFilesFolder,
                              

                              //Specify additional search paths here, like this:
                              // '/Volumes/Intern_SSD1/Projects/202008_YOUNG',
                              ],
                              });

                              if (candidates.length === 0)
                                  log('No files found');
                              else if (candidates.length === 1)  {
                                  sf.system.exec({ commandLine: `open -R "${candidates[0]}"` });
                              } else {
                                  log('Multiple files found.');
                                  let selectedPath = sf.interaction.popupSearch({
                                      items: candidates.map(c => ({
                                          name: c,
                                      }))
                                  }).item.name;
                                  sf.system.exec({ commandLine: `open -R "${selectedPath}"` });
                              }
                              

                              }

                              main();

                              1. Hi Metod,

                                Please open a new thread for this and use the Script Help method to do so, so we can understand what you're trying to do and get your log files.

                                Please see this link for how to best get help with SoundFlow.