No internet connection
  1. Home
  2. How to

Open Finder Window in New Tab, rather than window?

By Ben Rubin @Ben_Rubin
    2023-03-03 17:03:55.318Z

    Anyone know how to do this?

    thanks
    ben

    Solved in post #13, click to view
    • 12 replies
    1. sf.ui.finder.menuClick({ menuPath: ["File", "New Tab"] })

      1. Ben Rubin @Ben_Rubin
          2023-04-13 15:40:55.363Z

          Thanks much for this @Chris_Shaw.

          How would I use this in the context of this template, which I'm using to specific folders in the finder. But I'd like like the script to open a new tab in the existing focused finder window, rather than another window.

          thanks much
          ben

          1. Just use it as is.
            If a window is already focused in the Finder sf.ui.finder.menuClick({ menuPath: ["File", "New Tab"] }) will open a new tab in it

            1. Then use the finder menu Go > Go to Folder... to go to the desired directory in the new tab

              1. Ben Rubin @Ben_Rubin
                  2023-04-15 03:24:48.502Z

                  Thanks for your help, @Chris_Shaw! I understand what you're saying.

                  But I'm trying to use it in the following script, which is a template for different folder locations.

                  const folderPath = event.props.folderPath;
                  
                  sf.ui.finder.appActivateMainWindow();
                  
                  sf.directory.open({
                      path: folderPath
                      
                  });
                  

                  thanks for any help.
                  best
                  b

                  1. Maybe I'm being obtuse here but could you outline the steps of your script?
                    From what I've been able to tell form the script above:
                    You have a folder path from event.props.folderPath - are you trying to open that path in a tab in an existing window or are you trying to open a new window with that folder path?
                    Also, you should be using sf.ui.finder.appActivate() not sf.ui.finder.appActivateMainWindow() because if no window is open then you will get an error.

                    1. Ben Rubin @Ben_Rubin
                        2023-04-15 17:17:01.126Z

                        Thanks for the reply, Chris. I am simply trying to open the folder path specified in "folderPath" to open a new tab in the top-most finder window.

                        thanks also for clarifying appActivate vs appActivateMainWindow. Makes sense.

                        1. Ben Rubin @Ben_Rubin
                            2023-05-04 15:43:39.358Z

                            Hey @Chris_Shaw, just a ping regarding this issue. 🙏

                            1. Sorry about that - I thought you'd be able to piece it together with the info in this thread.
                              The only new thing here is a check to see if any Finder windows are open. If not then it will open a new one.

                              sf.ui.finder.appActivate();
                              
                              const folderPath = event.props.folderPath;
                              
                              //Check if any finder windows are open and create new tab
                              if (sf.ui.finder.windows.length > 1) {
                              
                                  // open tab in new window
                                  sf.ui.finder.getMenuItem("File", "New Tab").elementClick();
                              
                              } else {
                                  //Open new Finder window
                                  sf.ui.finder.getMenuItem("File", "New Finder Window").elementClick();
                              };
                              
                              //Goto to path
                              sf.ui.finder.getMenuItem("Go", "Go to Folder…").elementClick();
                              
                              //Set path
                              sf.ui.finder.mainWindow.sheets.first.comboBoxes.first.elementSetTextFieldWithAreaValue({
                                  value: folderPath
                              })
                              sf.wait({ intervalMs: 75 })
                              
                              sf.keyboard.press({ keys: "return" });
                              
                              
                              1. Ben Rubin @Ben_Rubin
                                  2023-05-05 13:07:24.166Z

                                  Thanks @Chris_Shaw, I've learned a ton about coding from you, but I guess I'm not that far just yet. Really appreciate your time.

                                  The script is failing at line 20. It opens the Go To Folder box, but the script is not "finding the text field". When the box opens, it is pre-filled with the most recent location.

                                  Now, for full disclosure, I've figured out a way to force the finder to open tabs rather than windows, so not necessary to take this further if you dont want.

                • In reply toBen_Rubin⬆:
                  Brenden @nednednerb
                    2023-03-04 22:27:28.079Z

                    then

                    sf.ui.finder.menuClick({ menuPath: ["Go", "Go to Folder..."] })
                    

                    That opens a prompt, where on my Ventura OS, I can search for any folder (or paste a full pathname!!)

                    Bingo!

                    1. In reply toBen_Rubin⬆:
                      Ben Rubin @Ben_Rubin
                        2025-03-20 19:18:59.104Z

                        just an update to anyone who discovers this old thread.

                        Here is some code generated by AI that does a nice job of opening a Finder Path in the current Finder Window. Does not open a new window or tab, but replaces the path of the frontmost Window. "folderPath" is the variable to set up in your template. Could be replaced with a specific path if this is what you need.

                        const folderPath = event.props.folderPath; // This should be your desired folder path
                        
                        // Execute AppleScript using sf.system.execAppleScript
                        sf.system.execAppleScript({
                            script: `
                                tell application "Finder"
                                    try
                                        -- Check if any Finder window is open
                                        if (count of windows) > 0 then
                                            -- Get the front Finder window
                                            set frontWindow to front Finder window
                                            -- Set the target of the front window to the specified folder path
                                            set target of frontWindow to (POSIX file "${folderPath}") as alias
                                        else
                                            -- If no Finder window is open, just open the folder
                                            make new Finder window to (POSIX file "${folderPath}") as alias
                                        end if
                                    on error
                                        -- Handle error if the folder path is invalid
                                        display dialog "Could not open the folder: ${folderPath}" buttons {"OK"}
                                    end try
                                end tell
                            `
                        });
                        
                        ReplySolution