Jump to content

[Request] Workflow to resize window to specific size


Recommended Posts

The easiest way to do it is with AppleScript. Layouts uses Perl, which is harder, but faster. Here's a script from a workflow that I wrote a long time ago (before Layouts, which is what I use). You can adapt it to what you need.

on run {query}
	
	tell application "Finder"
		get bounds of window of desktop
	end tell
	
	
	--- This script takes an argument and set the active window to the argument
	--- Options: 
	---		(1-4) 	either of the four quadrants
	---		(5-8) 	any particular half of the screen
	---		(9) 	Maximized
	
	--- Get the size of the display
	--- send escape key: tell application System Events to key code 27
	--- set query to "maxi"
	
	tell application "Finder"
		set x to get bounds of window of desktop
		set tr to third item of x -- top right corner
		set br to fourth item of x -- bottom right corner
	end tell
	
	if query = "ulq" then
		set thissize to {0, 0, tr / 2, br / 2}
	end if
	
	if query = "urq" then
		set thissize to {tr / 2, 0, tr, br / 2}
	end if
	
	if query = "llq" then
		set thissize to {0, br / 2, tr / 2, br}
	end if
	
	if query = "lrq" then
		set thissize to {tr / 2, br / 2, tr, br}
	end if
	
	if query = "lhalf" then
		set thissize to {0, 0, tr / 2, br}
	end if
	
	if query = "rhalf" then
		set thissize to {tr / 2, 0, tr, br}
	end if
	
	if query = "thalf" then
		set thissize to {0, 0, tr, br / 2}
	end if
	
	if query = "bhalf" then
		set thissize to {0, br / 2, tr, br}
	end if
	
	if query = "maxi" then
		set thissize to {0, 0, tr, br}
	end if
	
	--- set thissize to the value of query
	
	--- get the front app name
	tell application "System Events"
		set activeapp to first application process whose frontmost is true
		set appname to name of activeapp
	end tell
	
	--- set the window
	tell application appname to set the bounds of the front window to thissize
	
end run

If you want it always to be a fixed size, then you can just use numbers, and if you want it to work only with OmniFocus, then you can use that explicitly instead of the code to find "appname".

Link to comment

I've put it on Dropbox (temporarily) as a workflow. Remember, it's incomplete because I stopped writing it because a better one with a better approach was published as I was writing it. So, not all of the corners work correctly. But I just tested it, and it seems like the all, north, east, south, and west ones work. The keyword is `fly`.

 

Open up the workflow and see if you can work from there.

Link to comment

The approach that Layouts took, especially with Perl, made it just so much faster than what AppleScript does. You can feel the lag with the workflow above, especially if you set some hotkeys and then push the window around a lot. (Although, for fun, I did write a little script that animated the window movement by changing it a few pixels at a time... it was pretty but, ultimately, annoying to use... that and I wrote one that made the window bounce of the sides of the screen the way that screensavers sometimes do with images: I was fairly bored that day).

 

But if you're going for something simple that will just apple to OmniFocus and just set it to a fixed size, then the script should be much, much simpler than what is included in the workflow above because you don't have to worry about dynamically finding variables. Actually, the script might just need to be:

try
  tell application "OmniFocus" to set the bounds of the front window to {0, 0, 500, 500}
end try

Double-check to make sure that "OmniFocus" is actually the spelling that it wants.

 

If you open Script Editor, then you can check it with the script:

delay 2
tell application "System Events"
		set activeapp to first application process whose frontmost is true
		set appname to name of activeapp
end tell

log appname

Then, execute the script and quickly switch over the OmniFocus, wait a second, and switch back. The "delay 2" makes the script wait for a second.

 

The "try" statement in the script is important just because, without it, the script will throw an error if OmniFocus is not open, and it's always best to avoid errors, even if they have little to no consequence.

 

After you set that script up in a workflow, then just mess with the numbers until you get it to be the size and position that you want. Also, if you're using more than one display, then the numbers take into account all displays. So (0, 0) is the upper left corner of the left-most screen. The upper-left corner of the right-most screen is (pixel-width-of-all-other-displays-added-together, 0).

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...