Jump to content

Workflow to open two finder windows but also arrange them in a specific way on my screen.


Recommended Posts

So I've been trying to make this workflow for a while but I can't find any answers online and I'm not nearly skilled enough to do it myself.

 

I currently use a few alfred workflows to open certain folders that I use for my work. A lot of the time I need to open more than one of these folders and I usually put them on right side of my screen, one above the other.

 

I've already set up a new workflow to open two finder windows but it just opens two windows on top of each other. This is fine really, but I'd like to make a workflow that would arrange the windows without me doing it.

 

Is there anyway of doing this?

Link to comment

BTW, this question doesn't really have anything to do with Alfred. Alfred is a launcher, but you're asking about application automation.

 

As a rule, Ask Different is a more appropriate place for questions like this.

 

But to answer your question: Yes, it can be done with AppleScript.

 

The AppleScript below will open the folders specified in the theFolders property at the top of the script. By default, it opens each folder as requested, in a new Finder window on the right-hand side of the screen, each below the last.

 

You can run it from Alfred using a Run Script action with Language = /usr/bin/osascript (AS).

-- Paths to folders to open
property theFolders : {"/Volumes", "/"}

-- Set to "true" to open folders on the right-hand side of the screen.
property rightAlign : true

on getScreenWidth()
	tell application "Finder"
		set screenBounds to bounds of window of desktop
		return item 3 of screenBounds
	end tell
end getScreenWidth

tell application "Finder"
	set yPos to 0 -- vertical position of created windows
	
	-- Get width of screen so we can position the windows on the right
	set screenWidth to my getScreenWidth()
	
	-- Open the folders specified above	
	repeat with aFolder in theFolders
		set theTarget to aFolder as POSIX file
		set theWin to make new Finder window
		
		-- Get size of window and move it to the right-hand side of the screen
		set theBounds to theWin's bounds
		set winWidth to (item 3 of theBounds) - (item 1 of theBounds)
		set winHeight to (item 4 of theBounds) - (item 2 of theBounds)
		
		if rightAlign = true then
			set xPos to screenWidth - winWidth
		else
			set xPos to 0
		end if
		
		set theWin's position to {xPos, yPos}
		
		-- Calculate position of next window
		if yPos = 0 then
			-- Set yPos to bottom of menubar
			set theBounds to theWin's bounds
			set yPos to item 2 of theBounds
		end if
		set yPos to yPos + winHeight
		
		-- Navigate to the correct folder
		set theWin's target to theTarget
		
		-- Bring Finder to front
		activate
	end repeat
end tell

 

Link to comment

@deanishe Thanks for writing it all out, it's a massive help as I really don't know a thing about AppleScript.

 

I've put the script into Alfred but it's not working as it should. It's opening both windows but they're too tall. The bottom one does start in the right place (right under the top one) but it goes below the screen boundaries.

 

Any ideas why this is happening?

Link to comment

My mistake. I just changed my default Finder window size, and the script uses the system default window size, not yours. There doesn't seem to be a way to use the user default :( 

 

Here's an altered version with a hardcoded window size (winWidth and winHeight):

-- Paths to folders to open
property theFolders : {"/Volumes", "/"}

-- Size of created windows
property winWidth : 800
property winHeight : 400

-- Set to "true" to open folders on the right-hand side of the screen.
property rightAlign : true

on getScreenWidth()
	tell application "Finder"
		set screenBounds to bounds of window of desktop
		return item 3 of screenBounds
	end tell
end getScreenWidth

tell application "Finder"
	set yPos to 0 -- vertical position of created windows
	
	-- Get width of screen so we can position the windows on the right
	set screenWidth to my getScreenWidth()
	
	-- Open the folders specified above	
	repeat with aFolder in theFolders
		set theTarget to aFolder as POSIX file
		set theWin to make new Finder window
		
		-- Get size of window and move it to the right-hand side of the screen
		-- Resize window
		set {winL, winT, winR, winB} to theWin's bounds
		set theWin's bounds to {winL, winT, winL + winWidth, winT + winHeight}
		
		if rightAlign = true then
			set xPos to screenWidth - winWidth
		else
			set xPos to 0
		end if
		
		set theWin's position to {xPos, yPos}
		
		-- Calculate position of next window
		if yPos = 0 then
			-- Set yPos to bottom of menubar
			set theBounds to theWin's bounds
			set yPos to item 2 of theBounds
		end if
		set yPos to yPos + winHeight
		
		-- Navigate to the correct folder
		set theWin's target to theTarget
		
		-- Bring Finder to front
		activate
	end repeat
end tell

 

Link to comment
1 hour ago, Bloopsy said:

spaced evenly from the center of the screen?

 

What does that mean? Just vertically? Vertically and horizontally? I'm not going to rewrite the script again after this, so you need to be very clear about what you want.

Edited by deanishe
Link to comment

Evenly spaced from the centre of the screen vertically. So like in the diagram I sent, the windows are both the same vertical distance from the centre of the screen. I thought it was clear that they are not evenly space horizontally.

 

Thanks again.

Link to comment
2 hours ago, Bloopsy said:

I thought it was clear that they are not evenly space horizontally.

 

They aren't right-aligned with the right edge of the screen, which is where the current script puts them. The implication is that you don't want that behaviour. The boxes appear to be left-aligned with the centre of the screen.

 

The script must assign an X position as well as a Y one. If you don't specify one, then you're stuck with whatever I pick.

Link to comment

Ah I see, my bad. I meant the graphic to just display the vertical spacing but I should have considered what else it implied.

 

It would be great if you could add a distance from the right edge of the screen also.

So just to clarify, the windows are equally spaced vertically from the centre of the screen but also a set distance from the right side of the screen.

 

If this is a pain to do, I would just appreciate them being spaced vertically from the centre if nothing else.

 

Thanks again, I owe you :D.

Link to comment
20 minutes ago, Bloopsy said:

If this is a pain to do

 

Not at all. But like I said, I'm not going to keep rewriting the script, so we need to be clear about it's behaviour.

 

Here you go. You can specify the vertical distance to the centre and the horizontal distance to the (left or right) screen edge.

 

-- Paths to folders to open
property topFolder : "/Volumes"
property bottomFolder : "/"

-- Size of created windows
property winWidth : 800
property winHeight : 400

-- How many pixels above/below screen centre to position windows
property distanceFromCentre : 10

-- How many pixels from side of screen to position windows
property distanceFromSide : 50

-- Set to "true" to open folders on the right-hand side of the screen.
property rightAlign : true


-- Open a directory in a new Finder window
on openDirectory(thePath, aboveCentre)
	
	tell application "Finder"
		
		-- Calculate vertical centre of screen
		set {screenL, screenT, screenWidth, screenHeight} to bounds of window of desktop
		set yCentre to screenHeight / 2
		
		-- Create and resize new Finder window		
		set theWin to make new Finder window
		set {winL, winT, winR, winB} to theWin's bounds
		set theWin's bounds to {winL, winT, winL + winWidth, winT + winHeight}
		
		-- Position window on screen
		if rightAlign = true then
			set xPos to screenWidth - (winWidth + distanceFromSide)
		else
			set xPos to distanceFromSide
		end if
		
		set theOffset to winHeight + distanceFromCentre
		
		if aboveCentre = true then
			set yPos to yCentre - (winHeight + distanceFromCentre)
		else
			set yPos to yCentre + distanceFromCentre
		end if
		
		set theWin's position to {xPos, yPos}
		
		-- Open folder in window
		set theWin's target to (thePath as POSIX file)
		
	end tell
	
end openDirectory

on run (argv)
	my openDirectory(topFolder, true)
	my openDirectory(bottomFolder, false)
	
	tell application "Finder" to activate
end run

 

Link to comment
  • 4 years later...

Hi and good day Deanishe,

 

When running this script there was a Finder error: AppleEvent handler failed.

 

the row concerned:

 

set theWin's target to (thePath as POSIX file)

 

these are my paths

 

property topFolder : "/Volumes/Users/okn/Desktop/_Autoconvert"
property bottomFolder : "/Volumes/Users/okn/LC3b-wout-Movies/_Pictures-LC3/_autoconverted pics"


Otherwise I used the script from the last iteration above, if you could make sense out of it. Would be great!

Link to comment

Hi and good day Vitor,

As a start I tried to use "Set Custom Window Position," with bounds:x = 15 and y = 85, but something is missing, do you know?

 

This is just for the 1st folder, later there will be 2 more.


good bye,

OmarKN

 

PS:How to avoid writing in rich text?

Screenshot 2023-01-20 at 22.08.01.jpg

Link to comment

transfer.sh - - xxx

curl: Can't open './Users/okn/LC3b-wout-Movies/Documents-LC3/_Wie_HowTo/w-Alfred5/Workflows1.alfredworkflow'!
 

Possible would be wetransfer.com or dropbox, but then I need an email adr.


Otherwise let me just show the ingredients:
 

259685015_Screenshot2023-01-21at16_18_30.thumb.jpg.79b826336bbe99109c82724b21105757.jpg

 

/

with best regards, Omar KN, Stockholm, Sweden

Edited by OmarKN
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...