Jump to content
Bloopsy

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?

Share this post


Link to post

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

 

Share this post


Link to post

@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?

Share this post


Link to post

I deliberately designed the script to use your default Finder window size. It would have been a lot easier to specify a size.

 

Do you want to change the default window size or should I add a specific window size to the script?

 

Share this post


Link to post

I guess I'd prefer to change the default size.

 

I'm confused though as when I open a new finder window it's the size I want, but when I run the script, the windows are much bigger. So I don't think they're using the default size?

Share this post


Link to post

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

 

Share this post


Link to post
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

Share this post


Link to post

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.

Share this post


Link to post
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.

Share this post


Link to post

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.

Share this post


Link to post
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

 

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...