Jump to content

Finder scripts (including opening in a single window)

Recommended Posts

This is a workflow I've cobbled together from various sources, notably some modifications to https://github.com/NapoleonWils0n/bashfinder , which is actually a bash front end for a lot of AppleScript.


It contains the following commands:


fo - open a folder in the frontmost Finder window (also available as a File Action)

fo:i - open a folder in the frontmost Finder window with Icon view (also available as a File Action)

fo:c - open a folder in the frontmost Finder window with Column view (also available as a File Action)

fo:l - open a folder in the frontmost Finder window with List view (also available as a File Action)

all - open "All My Files" (Sierra) or "Recents" (High Sierra and later) in the frontmost Finder window - this uses a Key Combo action ⌘⇧F

icloud - open iCloud Drive in the frontmost Finder window - this uses a Key Combo action ⌘⇧I

fat - choose and open an existing tab or window


rf - open a recent folder in the frontmost Finder window

fi - set frontmost Finder window to Icon view

fc - Set frontmost Finder window to Column view

fl - Set frontmost Finder window to List view

fg - Set frontmost Finder window to Gallery view (i.e. icon view with big icons and a dark background - this is my own take on a Gallery view, which predated the official one introduced in Mojave)

fw - Close frontmost window and reopen active tab (i.e. close all tabs except the active one)

merge - Merge All Windows (same as menu command)

fcfix - set filename column width to 400

clip - show the Finder clipboard (I bet you didn't know there was such a thing....)


There is a single environment variable: 


set it to 0 to open in the frontmost window (replacing the existing contents) or 1 (the default setting) to open in a new tab in the frontmost window


By using fo and its variants, you can effectively have Alfred open folders in a single window as requested here: 


and here:



v.0.5 (2019-06-08)

Added fat keyword (was going to use ft but it was constantly confused with FaceTime)


v.0.4 (2018-08-06)

Added icloud and all keywords


v.0.3 (2018-04-08)

Added file actions

Added rf and fw keywords


v.0.2 (2018-04-06)

Added openInNewTab



https://www.dropbox.com/s/u1bchxchlm4c1q3/Finder scripts.alfredworkflow?dl=0




Edited by dfay
Link to comment

Yes.  I added an environment variable - it's set to use tabs by default since that's my preference.


Adding the AppleScript to take care of the new tabs (embedded in bashfinder.sh) took all of five minutes.  Trying to set the environment variable programmatically in bash took more than an hour and I gave up.  So at this point you need to set it manually.  

Edited by dfay
Link to comment
  • 3 months later...
  • 7 months later...
  • 4 weeks later...

Okay, you've got me dazzled. How in the world do I set the environment variable to avoid opening new tabs? I have set it in .profile and in my .bashrc so that it is correctly set in bash terminal windows, but this has no effect on the Alfred shortcut. It still opens new tabs.


I have searched and found nothing clear about how to do this. Weird stuff like creating .plist files.


What's the best way?


BTW, I love this workflow. Thanks for posting it.


Link to comment

My use case for this workflow is usually this:


I am doing something incredibly creative (!) and I need to reference a document from somewhere.


I do fo (folder name) or maybe rf (select recently opened folder).


The folder opens in Finder--cool! However, Finder is in the background. If I run this workflow with Finder in the background seems reasonable that I want Finder brought to the front so I can look at what I asked for. Any ideas about how to fix this in the workflow? 

Link to comment

I got this to work the way I wanted. This workflow uses bash scripts. However, one of the bash scripts adds 


osascript -e 'tell application "Finder" to activate'


at the end to bring Finder to the front. I added this to others, like "fo" and "rf" and it works perfectly!



Link to comment
  • 2 months later...

@dfay Since you have a ton of great Finder workflows, I figured you'd be the right person to ask: Do you have a workflow - or know of another's workflow - that displays and searches for open Finder tabs?


I often have several tabs open in the same Finder window, and am just looking for an easier way to get to the right one quickly (i.e., without having to squint to read the small print when they're smashed together).


I was hoping it would operate like the many Chrome and Safari tab searching workflows that people have created. As a novice, this seems like it would be easier to accomplish in Finder than it is in Chrome or Safari - since, among other things, Finder's tabs are actually listed at the bottom of the Window menu - but what do I know! I haven't been able to find any posted to this forum or elsewhere, so perhaps it's a bit more complicated. Ha


Thanks for any help you can lend!

Link to comment
2 hours ago, Jasondm007 said:

Do you have a workflow - or know of another's workflow - that displays and searches for open Finder tabs?


Just added one to this workflow, keyword is fat - Finder active tabs - because FT kept bringing up FaceTime.


The only catch is that AppleScript treats every tab as a separate window, so if you have multiple windows, they will all appear in the results.

Link to comment

@dfay By chance, do you know if your workflow for searching open Finder tabs and windows is generalizable to other apps whose menus also list their open windows or tabs?


The reason I ask is because I was hoping to apply your approach to the Preview app, too. When I’m writing/researching I often have several different PDFs open in Preview - and often across more than one space/desktop and monitor (similar to the way I leave several Finder tabs open to folders that I’m working on). As a result, it’s often difficult to find the one that I’m looking for. To date, I’ve tried several different app and window switching applications (e.g., Witch, Contexts, etc.). However, they all have certain problems or quirks in this work environment. Most often, they fail to register Preview and Finder windows, which is why I reached out about Finder in the first place.


I’m sure there’s no perfect solution, but since your Finder tabs workflow doesn’t seem to have any of these problems, I was hoping to do the same thing in Preview (i.e., as a separate workflow) - which has a similar approach to Finder when it comes to listing windows in its own menu. However, after duplicating your Finder Tabs workflow and slightly amending its scripts for Preview, I haven’t had any luck getting it to work. Initially, when it didn’t work, I did a little digging and found blogs complaining about Preview’s lack of AppleScript support. However, these posts were all quite old. In fact, when testing, I noticed that some of the things that they complained about actually worked just fine now (e.g., like the ability to simply close a window, etc.). Before I spend a ton more time trying research why my changes to your workflow don’t work, I was just curious if you thought your approach to Finder would also work for Preview? Thanks!

Edited by Jasondm007
Link to comment
12 hours ago, dfay said:

This is proving to be more complicated....getting window details is easy and standardized but what you can and can’t do with the window varies by app.


@dfay I can only imagine! Do the stock MacOS apps at least use them similarly? I'm going to guess that I know the answer to that one ... 😉


If it's any consolation, selfishly, I was only hoping to use it when opening Preview windows, like Finder. 


However it turns out, thanks for all of your efforts!

Link to comment

@dfay This is amazing!! 🤯 Thank you so much!


You were right about Skim. I had to exclude it from the environment variable because it was prompting my computer to select an app for opening Skim files.


I also made one small change to the "launch.scpt" file. I noticed when switching to a PDF in Preview from another desktop/space that my computer was activating Preview, but not the correct window, so I added two small lines (marked below with "NEW"). They just activate the app and add a small delay prior to opening the window. Hopefully this won't break anything, but it seems to be working just fine now.


Thanks again for all of your work on this workflow! I really can't thank you enough.

set theApp to (system attribute "app")
set windowID to (system attribute "windowID") as integer
set theDocument to (system attribute "document")
set appList to words of (system attribute "appList") as list

if theApp is not in appList then
	display alert theApp & " not configured"
	--	if theApp is "Preview" then
	tell application theApp
		activate -- NEW
		delay 0.50 -- NEW
		set itsWindows to every window
		repeat with w in itsWindows
			if windowID is id of w then
				set index of w to 0
			end if
		end repeat
	end tell
	--	end if
end if


Link to comment

@dfay I used the workflow quite a bit today with Preview, and it's great!! Thanks a ton.


I'm not sure if I introduced this problem with the code above, but the workflow doesn't always activate the appropriate pdf window in Preview. Instead, it brings it to the front, while keeping the previous one active. Have you noticed this, too? Any ideas what to change in the code above? Thanks!

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...