1

I want to write a code to resize a window by setting its bounds, and I need to consider where the dock is positioned and the size of the dock. I can get the dock's position and its size, but the problem is the size I'm getting is not in an immediately usable form. For example, when I do:

tell application "System Events"
    tell dock preferences
        properties
    end tell
end tell

I get

{show indicators:true, minimize into application:false, autohide:false, magnification size:1.0, class:dock preferences object, dock size:0.428571432829, autohide menu bar:false, double click behavior:zoom, magnification:false, show recents:true, minimize effect:genie, screen edge:left, animate:true}

How do I translate the dock size of 0.428571432829 into, for example, 100 so that I can set a window (in a 1024x768 resolution) to position x=100, y=0, h=768, w=924?

Notice x started after the dock, and the w is minus the dock width.

5
  • Maybe I’m remembering wrongly, but I thought if you resized a window, AppleScript would take into account the dock and menu bar without the need to do any arithmetic. Certainly, if I set a window’s position and height to {0, 0} and 768 respectively, then—given a menu bar located at the top edge of the screen, with a height of 23px—the window would find itself positioned at {0, 23} and its height would be 745.
    – CJK
    Commented Dec 27, 2020 at 0:27
  • No that's not the case. I learned about it after running the same script on another laptop with a dock positioned at the left (instead of the usual at the bottom). What happens is the window is positioned all the way to the left, behind the dock. I use bounds property only to move and resize the window as the frame and position are marked as deprecated. Commented Dec 30, 2020 at 1:25
  • I wouldn't worry about things being marked as deprecated in AppleScript—it means very little, generally, although bounds would be the better one to use as frame was not universally implemented in the first place rather than deprecation being the issue. But bounds is only applicable to scriptable applications. For non-scriptable apps (i.e. most) you'll need to manipulate the window by way of System Events, and that will be using the size and position properties common to all subclasses of UI element class objects. Shame the dock is such a pain.
    – CJK
    Commented Dec 30, 2020 at 20:52
  • Oh, and since you'll be accessing System Events anyway, you might like to know the screen dimensions can be obtained like this: tell app id "com.apple.systemevents" to tell process "Finder" to set [screenW, screenH] to the size of scroll area 1, which in your case will return [1024, 768]. That way, if you change resolution at any point, your script won't need rewriting.
    – CJK
    Commented Dec 30, 2020 at 20:59
  • Thank you for the tip, @CJK. I do believe what you say about deprecation in AS. I did write a small library to help me position windows, and I learned how differently we should position Terminal, Console, and god knows what the 3rd app is (all Apple first-party). Commented Jan 1, 2021 at 13:39

1 Answer 1

3

The following example AppleScript code will return the size of the Dock:

tell application "System Events" to ¬
    tell application process "Dock" to ¬
        set dockSize to ¬
            the size of list 1

return dockSize

If you want the width and height:

tell application "System Events" to ¬
    tell application process "Dock" to ¬
        set {dockWidth, dockHeight} to ¬
            the size of list 1


return {dockWidth, dockHeight}
1
  • Thank you, it works. I tried to figure out how you came up with your answer, and I found out that you can get the list by getting the entire contents of the application process dock, then getting its properties contained the attributes I need. Commented Dec 23, 2020 at 4:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .