Get win­dows where you want them

Mac Format - - IMPROVE -

1 Iden­tify the ac­tive app

In Au­toma­tor, choose File > New and choose Ser­vice as the type of work­flow to cre­ate. Above the work­flow on the right, set the ser­vice to re­ceive no in­put in any ap­pli­ca­tion.

Se­lect Ac­tions on the left of the win­dow and use the ad­ja­cent search bar to find the Run Ap­pleScript ac­tion. Drag it into the work­flow on the right. Re­place the line in it that’s marked as where your script should go with th­ese three lines: tell ap­pli­ca­tion "Sys­tem Events" set front­mostApp to name of first item in (ap­pli­ca­tion pro­cesses whose front­most is true) end tell

This gets a list of apps which are front­most on the desk­top. Log­i­cally, it con­tains one item, so we ex­tract that to get the front app’s name. Re­move the line that says “re­turn in­put” as we don’t need it. Click the ham­mer in the ac­tion’s tool­bar to prop­erly in­dent and colour the script to aid its read­abil­ity. If this re­turns an er­ror, check your typ­ing for mis­takes. Drag up the small han­dle just be­low the script and you see it says “Au­toma­tor”, which is the value re­trieved by the sec­ond line of the script. When we run this script as a Ser­vice later on, this value ref­er­ences the front­most app.

2 Re­po­si­tion its front win­dow

Now we can tell the ac­tive app to re­po­si­tion its front win­dow. Leave a blank line af­ter ‘end tell’ and then add: tell ap­pli­ca­tion front­mostApp set bounds of front win­dow to {100, 22, 700, 422} end tell

Com­pile and run the script again and the Au­toma­tor win­dow moves and re­sizes. The four num­bers state where to po­si­tion the win­dow’s edges rel­a­tive to the ori­gin (the desk­top’s topleft cor­ner), work­ing clock­wise from the left edge. The value of 22 puts the top edge right un­der the menu bar; well­be­haved apps snap their win­dows to that point if you try to po­si­tion their win­dows any higher. The num­bers we’ve used here are just for demon­stra­tion – try re­plac­ing them with your own val­ues and run the script again to see how its win­dow’s size and po­si­tion changes.

3 Snap to the left side

To make a win­dow snap to the left half of the screen, re­place the four val­ues from the pre­vi­ous step with: 0, 22, half of your dis­play’s hor­i­zon­tal res­o­lu­tion mi­nus one (to ac­count for the first row and col­umn of pix­els be­ing po­si­tion 0), and its full ver­ti­cal res­o­lu­tion mi­nus 1. For a 21.5-inch iMac with a 1920x1080 screen, then, they would be: 0, 22, 959, 1079. If you’re not sure of your screen’s val­ues, go to > About This Mac > More Info > Dis­plays. If you have a Retina dis­play, di­vide the val­ues shown there by two for use in your script.

Press ç+S and save the ser­vice with the name you want to ap­pear in the Ser­vices menu (which is found by click­ing the cur­rent app’s name in the menu bar).

In Sys­tem Pref­er­ences, go to Key­boards > Short­cuts. Choose Ser­vices on the left, then scroll all the way down to the Gen­eral group to find the one you just cre­ated. In its row, dou­ble-click in the col­umn that lists as­signed short­cuts, then press a com­bi­na­tion of mod­i­fier keys and a char­ac­ter key to as­sign it to the ser­vice. We chose ≈+ç+[ (the last key is left square bracket) be­cause it is un­likely to con­flict with other func­tion­al­ity, such as key­board nav­i­ga­tion in the Finder. A check mark ap­pears to the left of the Ser­vice’s name. Switch to an­other app, open a new (and re­siz­able) win­dow, and then press your short­cut key com­bi­na­tion. You might need to log out and in for it to be­come ac­tive, or you can choose it from the Ser­vices menu.

4 A choice of po­si­tions

In­di­vid­ual scripts pro­vide a one-step short­cut to sin­gle win­dow po­si­tions used reg­u­larly, but we can also dis­play a dia­log from which to choose from a list of po­si­tions. Cre­ate a new Ser­vice work­flow and set it to re­ceive no in­put. Re­place all of the prewrit­ten code with the fol­low­ing. Some lines end with ¬, which tells the ed­i­tor the next line is a con­tin­u­a­tion of this one. Press å+® to type it. prop­erty menubarHeight : 22 prop­erty desk­topWidth : 1440 prop­erty desk­topHeight : 900 prop­erty avail­ableHeight : desk­topHeight - menubarHeight on run set cho­senPo­si­tion to choose from list ¬ {"Top-right cor­ner", "Bot­tom-right cor­ner", "Max­imised"} ¬ with ti­tle "Win­dow man­ager" with prompt ¬ "Choose a po­si­tion:" with­out mul­ti­ple selections al­lowed if cho­senPo­si­tion is not equal to false then po­si­tionWin­dow(first item in cho­senPo­si­tion) end if end run

The first four lines de­fine val­ues we’ll re-use. Sub­sti­tute your dis­play’s hor­i­zon­tal and ver­ti­cal res­o­lu­tions (halved for a Retina dis­play) into desk­topWidth and desk­topHeight. This script dis­plays a list of named win­dow po­si­tions. When you choose one, its name is as­signed to the cho­senPo­si­tion vari­able. Next, the vari­able’s value is checked. If it con­tains false (which in­di­cates you pressed Can­cel), the script reaches its end with­out do­ing any­thing else. Oth­er­wise, the value is passed to a han­dler (an­other term for func­tion) called po­si­tionWin­dow, which we’ll write now.

5 Re­spond to the choice

Leave a blank line af­ter end run, and then add th­ese lines to de­fine the po­si­tionWin­dow han­dler’s behaviour, as fol­lows: on po­si­tionWin­dow(lay­out) tell ap­pli­ca­tion "Sys­tem Events" set front­mostApp to name of first item in (ap­pli­ca­tion pro­cesses whose front­most is true) end tell tell ap­pli­ca­tion front­mostApp if lay­out is equal to "Top-right cor­ner" then set bounds of front win­dow to {desk­topWidth / 2, menubarHeight, desk­topWidth, menubarHeight + (avail­ableHeight / 2) - 1} else if lay­out is equal to "Bot­tom-right cor­ner" then set bounds of front win­dow to {desk­topWidth / 2, menubarHeight + avail­ableHeight / 2, desk­topWidth, desk­topHeight} else if lay­out is equal to "Max­imised" then set bounds of front win­dow to {0, menubarHeight, desk­topWidth, desk­topHeight} else beep 1 end if end tell end po­si­tionWin­dow

The struc­ture of the two ‘tell… end tell’ blocks is familiar from our first Ser­vice. The sec­ond one’s body now checks the value re­ceived by the han­dler, and in­structs the ac­tive app to re­size its front win­dow ac­cord­ingly. The last branch is a con­tin­gency that makes the Mac beep if an un­recog­nised value is re­ceived. Save this Ser­vice and as­sign a key­board short­cut as you did in Step 3. The im­age at the top of this page il­lus­trates the dia­log that this Ser­vice dis­plays for you to choose a win­dow po­si­tion.

Au­toma­tor’s Run Ap­pleScript ac­tion al­lows you to do much more with work­flows than with other ac­tions alone.

Drag up on the han­dle be­low a non-work­ing script to check what it’s do­ing.

If a script needs to of­fer choices, such as win­dow po­si­tions, Ap­pleScript can dis­play them in a list.

The Key­board pref­er­ences pane lets you as­sign a key­board short­cut to items in the Ser­vices menu.

Newspapers in English

Newspapers from Australia

© PressReader. All rights reserved.