Let's say you want to control the windows on your Mac with precision.
You want to set exactly where they are on the screen.
Now there are a lot of great third party apps that you can install to allow you to control
your windows.
Putting them in the left half, the right half, putting them in certain spots.
They have very, kind of, complex interfaces I think and unless you're doing it all the
time it's probably not worth putting an entire other extension running on your Mac.
You can do it with just one line of code and get precision control.
So I'm going to show you how to do it using the Script editor.
Let's run Script Editor.
I'm just going to use Spotlight to search for it so Command space and search for Script
Editor.
This is the editor where you could write AppleScript and also JavaScript or JXA.
So I'm going to create a new document here.
The first thing I'm going to do, and this is very important, is I'm going to switch
from AppleScript to JavaScript.
Now you can do this in AppleScript.
It's different coding.
But I'm going to do it in JavaScript because you can do it in one line and I think JavaScript,
JXA, is the future and AppleScript is kind of the old way of doing things.
So I'm going to paste in the text here and we can take a look at it.
Basically it just has a bunch of parts and I love that in JXA you can do this in one
line and AppleScript it would take several lines.
So you've got Application, and then with parentheses and quotes the name of the application.
Let's do Safari then let's dot windows and then bracket zero.
In other words the first window.
In coding you usually start counting at zero.
So window number one is zero.
So you take the first window or in this case the only window in Safari.
Then you access a property called bounds.
Bounds, of course, is just like it sounds.
It's the rectangle that forms the window.
Then here we have a bunch of different properties.
We put those in quotes with a colon and then the value and then a comma between each one
with curly brackets around it.
We've got the x and the y, the left position and the top position, horizontal and vertical,
and then we've got the width and the height.
So if we want to set the Safari window to be the upper left hand corner, 700 pixels
wide and 400 high we use this exact piece of code.
Now just this one line.
All we need to do is execute it to run it.
So I'm going to hit the Play button.
Let me move it down a bit here and you can see the Safari window in the background.
I'll hit Play and you can see it does exactly that.
It moves the window to the upper left hand corner and sets its width to 700 and height
to 400.
Now incidentally since Safari cannot go behind the Menu Bar there setting it to zero really
didn't do anything.
Setting the y coordinate to zero.
For instance if I were to set it to 25 you can see it just moves down two pixels.
That's because 23 is equivalent to zero and zero is going to push it down 23 pixels so
it's not behind the Menu Bar.
But the height stays the same.
It's still 400 high.
So using zero is just a quick way to getting it down.
The corner is fine.
If you want something more specific like you want it to be ten pixels below the Menu Bar
then you would have to do 33.
You can do trial and error to figure out other similar things.
You can do the same thing in Automator.
Command space here.
Run Automator and I'm going to create in Automator a new Service which will be handy.
I'm going to say it takes no input and runs only in Safari.
Then I'm going to search for the run JavaScript action.
Put that there.
Put the same line of code there.
So this is exactly what I had in the Script Editor.
I can test it out.
So with Safari here I can move this.
I can run this.
In Automator you can see it snaps it up there to the top left.
Then I can Save it as To Upper Left.
Save.
Now I can find it in the Safari Services menu.
So say I change Safari to be some other size.
I go to Safari Services.
You can see To Upper Left and it snaps it in place there.
So a handy little thing.
Of course you can go and expand that if you're experienced in using JXA to pull the application
from whatever application is running or having a variety of things.
You can even have multiple lines here.
So you could have a script that sets the Safari window to one location and maybe a TextEdit
window to another location.
Things like that and run these.
A lot of cool things that you can do with these if you want this kind of precision in
handling your windows.
For me a big thing would be to maybe make it 1280 x 720.
Therefore when I shoot videos that show a window ,like Safari window, they can be exact
size to match a video that I create.