Anyone know how to do this?
thanks
ben
- Chris Shaw @Chris_Shaw2023-03-04 17:39:21.696Z
sf.ui.finder.menuClick({ menuPath: ["File", "New Tab"] })
Ben Rubin @Ben_Rubin
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
benChris Shaw @Chris_Shaw2023-04-13 17:33:05.246Z
Just use it as is.
If a window is already focused in the Findersf.ui.finder.menuClick({ menuPath: ["File", "New Tab"] })
will open a new tab in itChris Shaw @Chris_Shaw2023-04-13 17:35:07.289Z
Then use the finder menu Go > Go to Folder... to go to the desired directory in the new tab
Ben Rubin @Ben_Rubin
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
bChris Shaw @Chris_Shaw2023-04-15 15:54:03.045Z
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 fromevent.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 usingsf.ui.finder.appActivate()
notsf.ui.finder.appActivateMainWindow()
because if no window is open then you will get an error.Ben Rubin @Ben_Rubin
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.
Ben Rubin @Ben_Rubin
Hey @Chris_Shaw, just a ping regarding this issue. 🙏
Chris Shaw @Chris_Shaw2023-05-04 17:03:38.925Z
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" });
Ben Rubin @Ben_Rubin
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
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!
- In reply toBen_Rubin⬆:Ben Rubin @Ben_Rubin
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 ` });