Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript - bounds etc..

Hi,


I'm trying to make a script that will resize a window to a particular aspect ratio.


a bit of info/requirements:


-when printing from this particular app, the entire window (excluding toolbars) is considered.. if i want to print USletter and the current window is set at something other that 11:8.5, the print will be letterboxed(?) accordingling (ie- whatever size the drawing window is will be best fit to the page leaving blank spaces on the sides or top/bottom).. i'd like to be able to set the window to a size matching my paper so the entire window fills the paper


-i need to get the bounds of the window -- excluding the toolbar.. just the drawing area (graphics app)


-i would like the window to be resized so the longest side fills the screen (ie- if portrait USletter, the window will use all available space from the top to bottom of the display.. excluding the dock (if the user has one at the bottom), the menu bar (as far as i can gather, it's 22px for all users?), and toolbars in the app itself.. (the problem with the toolbar is that some people will have small icons, some large, some with text etc.. so it's height is different depending on the users preferences.. this is why i need dimensions of what space is left in the window without considering the toolbars)


-some things i already can do -- get the overall display size, resize the window, do the aspect ratio calculations according to desired paper size (in other words, i can make the script work properly on my machine.. i know the environment, my dock placement, my toolbar size, etc.. i'd like to be able to make it work on other people's machines with different dock placement and toolbar settings)


....

so i guess what i'm asking is:


how can i --

get or set the bounds of the working space of a window (the part without the toolbar) and/or determine the size of a user's toolbar so i can add that in my calculations?


determine where the dock is and how big it is (i keep my dock on the side so it doesn't really matter.. most people seem to keep theirs at the bottom so it does matter



i'm sure i left some stuff out but i'll provide more info / clarify upon request.

thanks

jeff

2.66 quad mac pro/ 15"i7mbp-OTHER, Mac OS X (10.7.2)

Posted on Dec 7, 2011 11:06 AM

Reply
2 replies

Dec 9, 2011 2:25 PM in response to flat5

look here:


http://www.snaq.net/software/applescripts.php



and here is a script that evaluates windows currently displayed (focusing on safari and finder windows) and re-adjusts them based on the current monitor settings.... this code has been adopted and manipulated by me, but the original source was found elsewhere on the internet...


set primaryWidth to 1920


tell application "Safari"



-- For some reason Safari has (two) invisible windows

set allWindows to (every window where visible is true)

set n to count of allWindows



-- Get dock position

set dock to do shell script "defaults read com.apple.dock orientation"



-- Get actual width to help with left-side docks

tell application "Finder"

set screenSize to bounds of window of desktop

set actualWidth to item 3 of screenSize

if actualWidth is greater than primaryWidth then set actualWidth to primaryWidth

end tell



-- Determine space open on your screen, factoring in the Dock

set screenWidth to do JavaScript "screen.availWidth" in document 1

set screenHeight to do JavaScript "screen.availHeight" in document 1



-- Get dock size

set dockSize to actualWidth - screenWidth



-- Get rid of 4 pixel buffer on Dock

if dockSize is 4 then

set dockSize to 0

end if



-- Dividing by zero is bad

if n is equal to 0 then return



-- Determine each window's width

set windowWidth to (screenWidth / 2)

set x to n



-- Position each window in turn

repeat

tell window x


-- 21 is the size of the Menu Bar (for 10.4 at least)

if (dock is not equal to "left") then

set bounds to {40 * (x - 1), 21 + (n - x) * 20, 40 * (x - 1) + windowWidth, screenHeight}

else

set bounds to {40 * (x - 1) + dockSize, 21 + (n - x) * 20, 40 * (x - 1) + windowWidth + dockSize, screenHeight}

end if

end tell

set x to x - 1

if x is equal to (0) then exit repeat

end repeat

end tell




tell application "Finder"



-- For some reason Safari has (two) invisible windows

set allWindows to (every window where visible is true)

set n to count of allWindows



-- Get dock position

set dock to do shell script "defaults read com.apple.dock orientation"



-- Get actual width to help with left-side docks

tell application "Finder"

set screenSize to bounds of window of desktop

set actualHeight to item 4 of screenSize

set actualWidth to item 3 of screenSize

if actualWidth is greater than primaryWidth then set actualWidth to primaryWidth

end tell




-- Get dock size

set dockSize to actualWidth - screenWidth



-- Get rid of 4 pixel buffer on Dock

if dockSize is 4 then

set dockSize to 0

end if



-- Dividing by zero is bad

if n is equal to 0 then return



-- Determine each window's width get rid of everything after windowwth

set windowWidth to (screenWidth / 2)

set windowHeight to (actualHeight / 2.8)

set x to n



-- Position each window in turn

repeat

tell window x


-- 21 is the size of the Menu Bar (for 10.4 at least)

if (dock is not equal to "left") then

set bounds to {screenWidth - windowWidth + 150, (n - x) * 30 + 50, screenWidth + 20, windowHeight + (n - x) * 30 + 50}

else

set bounds to {screenWidth - windowWidth + 150, (n - x) * 30 + 50, screenWidth + 20, windowHeight + (n - x) * 30 + 50}

end if

end tell

set x to x - 1

if x is equal to (0) then exit repeat

end repeat

end tell



I hope that helps you a bit....


Alf


ps: I had to "teach myself" applescript.... it takes some getting used to, but is farely accessible.

Dec 10, 2011 1:25 AM in response to Alfred Abraham

hey Alfred..

thanks a lot.


i don't have much time to look at /experiment/ etc with this right now but there appears to be some goodness in there..

i hacked my way though a solution but i'm thinking yours is more elegant.


for one, i couldn't find the actual size of the dock so if the dock was visible and along the bottom then i gave it a guess of 90 px 😉.. for my situation, it doesn't matter if the dock is on a side.. i'll try out your style soon.


here are a few bits that i've come up with so far..



-- get screen resolution


tell application "Finder"

set rez to bounds of window of desktop

set ScreenWidth to item 3 of rez

set ScreenHeight to item 4 of rez

end tell



--this is how i dealt with the dock😁



tell application "System Events"

tell dock preferences

set DockProp to properties

get autohide of DockProp

get screen edge of DockProp


if autohide is true or screen edge is not bottom then

set DKheight to 0

else

set DKheight to 90

end if


end tell

end tell



--the rest of the script (that i have so far.. there's more to this thing eventually) is for google's sketchup app.



tell application "SketchUp"


activate

tell application "System Events" to tell process "SketchUp"



--identify the drawing window


--finally figured out the actual window i wanted to resize was the last number out of all the floating toolbars and any other windows/panels open.. usually around window 22 or so 😁



set drawwin to count of windows




-- see if there's a toolbar up top and if so, get it's size.. do the same for scene tabs


if exists (tool bar 1) of window drawwin then

set tbsz to size of (tool bar 1) of window drawwin

else

set tbsz to {0, 0}

end if

set TBsize to second item of tbsz


if exists (tab group 1) of windowdrawwin then

set scsz to size of (tab group 1) of window drawwin

else

set scsz to {0, 0}

end if

set SceneSize to second item of scsz



--the main goal of this script is to make the actual drawing area of the window have the same aspect ratio as the paper it will be print on.. hence the

-- trip to file->page setup

-- go to page setup then find the paper dimensions listed underneath 'paper size'


-- use if/then on orientation button


-- use those numbers to make the height and width varibles



click menu item "page setup..." of menu "file" of menu bar 1


set AppleScript's text item delimiters to {" "}


set psz to value of static text 2 of sheet 1 of window drawwin


if value of radio button 1 of radio group 1 of sheet 1 of window drawwin = 1 then

set papw to first text item of psz

set paph to third text item of psz

else

set papw to third text item of psz

set paph to first text item of psz


end if



clickbutton 1 of sheet 1 of windowdrawwin






--CALCULATE AND RESIZE DRAWING WINDOW


--(just some notes that i kept around so i could figure out the calculations)


--paper width = var "papw"


--paper heith = var "paph"


--screen height = var "ScreenHeight"


--screen width = var "ScreenWidth"


-- dock height = "DKHeight"


-- OSX Menu Bar = 22px


-- top of sketchup window = 22px


-- bottom of sketchup window = 30px


--toolbar = var "TBsize"


--scene tabs = var "SceneSize"


set AspectRatio to (papw / paph)


if AspectRatio is less than 1.5 then

set WinHeight to (ScreenHeight - (22 + DKheight))

set WinWidth to (AspectRatio * (WinHeight - (TBsize + SceneSize + 52)))

else

set WinWidth to (ScreenWidth * 0.87)

set WinHeight to ((WinWidth / AspectRatio) + (52 + DKheight + TBsize + SceneSize))

end if



--resize the drawing window

set position of window drawwin to {80, 22}

set size of windowdrawwin to {WinWidth, WinHeight}



end tell

end tell



so, that's the first script i ever made.. and it works.. but now that i got into it for a few days, i think i picked the wrong app to learn how to control.. all that finder stuff looks much cooler!


thanks again for the info.. once i get some time to look at it in little chunks/digest it, i'm sure it's going to help me get my head around some of this stuff a bit better.

Applescript - bounds etc..

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.