Saturday, November 27, 2021

#1 2021-11-15 06:59:27 am

emendelson
Member
Registered: 2008-10-31
Posts: 395

Get position of dock?

I'm working on a script that needs to know the location of the dock - left, foot, or right. Out of complete ignorance, I wrote this partly useless code that determines whether the dock is horizontal or vertical (is the height greater than the width), but I can't figure out how to determine whether a vertical dock is on the left or right. Is there something obvious that I'm missing?

Applescript:

tell application "System Events" to tell process "Dock"
       try
           set dockDimensions to size in list 1
           set dockWidth to item 1 of dockDimensions
           set dockHeight to item 2 of dockDimensions
           if dockWidth is greater than dockHeight then
               -- do different things if dock is horizontal or vertical
           end if
       end try
end tell

It's in a try block, because it errors out if the script does not have assistive access. If there's a way to  get this information without assistive access, I'll be very glad to know it.


Filed under: dock

Offline

 

#2 2021-11-15 09:30:47 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2154

Re: Get position of dock?

Without GUI scripting:

Applescript:


tell application "System Events" to tell dock preferences
   if screen edge is in {left, right} then
       display dialog "The dock is vertical"
   else
       -- it is on bottom
       display dialog "The dock is horizontal"
   end if
end tell

Last edited by KniazidisR (2021-11-15 09:33:49 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#3 2021-11-15 09:35:59 am

emendelson
Member
Registered: 2008-10-31
Posts: 395

Re: Get position of dock?

Perfect! Thank you!

Offline

 

#4 2021-11-15 03:14:44 pm

wch1zpink
Member
Registered: 2011-08-20
Posts: 69

Re: Get position of dock?

KniazidisR wrote:

Without GUI scripting:

Applescript:


tell application "System Events" to tell dock preferences
   if screen edge is in {left, right} then
       display dialog "The dock is vertical"
   else
       -- it is on bottom
       display dialog "The dock is horizontal"
   end if
end tell


Even though your code does function, it does produce an error in the log.  The ‘display dialog’ command is a ‘StandardAdditions’ command but your code sends it to ‘System Events’.

2VH1q9w.png?1

Applescript works through the error and figures out what to do.  I know it's a minor detail of minor significance, but in the spirit of teaching others proper Applescript practice, the ‘display dialog’ commands in your code can be sent to ‘StandardAdditions’ from within the ‘System Events’ tell block by prefacing it with "my". If using "my" throws an error then use "tell current application to…"

Applescript:

tell application "System Events" to tell dock preferences
   if screen edge is in {left, right} then
       my (display dialog "The dock is vertical")
   else
       -- it is on bottom
       my (display dialog "The dock is horizontal")
   end if
end tell

zbn3OXY.jpg?1

Last edited by wch1zpink (2021-11-15 03:55:53 pm)

Offline

 

#5 2021-11-15 03:39:00 pm

emendelson
Member
Registered: 2008-10-31
Posts: 395

Re: Get position of dock?

And one more question: if the dock is horizontal, is it possible to get the height of the dock in pixels, without GUI scripting?

Offline

 

#6 2021-11-15 03:59:45 pm

wch1zpink
Member
Registered: 2011-08-20
Posts: 69

Re: Get position of dock?

emendelson wrote:

And one more question: if the dock is horizontal, is it possible to get the height of the dock in pixels, without GUI scripting?


This will show you all of the properties without GUI scripting.

Applescript:

tell application "System Events" to set dockProperties to properties of dock preferences

Cn5D9Wt.png?1

Applescript:

tell application "System Events" to set dockSize to dock size of dock preferences

Last edited by wch1zpink (2021-11-15 04:11:32 pm)

Offline

 

#7 2021-11-15 04:17:45 pm

emendelson
Member
Registered: 2008-10-31
Posts: 395

Re: Get position of dock?

Thank you again for this. The property dock size on my system is the number 0.2589... which I've been trying to translate into the height of the dock in pixels, but I can't see how to do so. This question has come up often in various forums, but the only answer that I've found requires GUI scripting. Any help will be gratefully received.

Offline

 

#8 2021-11-15 05:43:04 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1133

Re: Get position of dock?

emendelson wrote:

Thank you again for this. The property dock size on my system is the number 0.2589... which I've been trying to translate into the height of the dock in pixels, but I can't see how to do so. This question has come up often in various forums, but the only answer that I've found requires GUI scripting. Any help will be gratefully received.



If a more direct method is not available, the following will return the dock height in pixels. This assumes the dock is bottom aligned, although the script could be expanded to return this figure if the dock is side aligned. There are a few circumstances when this won't work correctly--such as when an app is full screen. On my computer, the dock height ranges from 32 to 150 pixels.

Applescript:

use framework "AppKit"
use framework "Foundation"
use scripting additions

set visibleScreenBounds to (current application's NSScreen's mainScreen()'s visibleFrame())
set dockHeight to item 2 of item 1 of visibleScreenBounds

Last edited by peavine (2021-11-15 05:46:26 pm)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#9 2021-11-15 07:00:42 pm

emendelson
Member
Registered: 2008-10-31
Posts: 395

Re: Get position of dock?

Thank you! This is perfect for my application, where I only need the height of the dock if the dock is horizontal. And I'm very happy to have a solution that uses only two lines of code...

I still wonder what that dock size number means, but my application now works correctly, and this question is just a matter of curiosity.

Offline

 

#10 2021-11-15 07:19:50 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1133

Re: Get position of dock?

emendelson wrote:

still wonder what that dock size number means, but my application now works correctly, and this question is just a matter of curiosity.



In System Preferences, dock size is a slider from small to large. The number returned by the dock-size property is a number between 0.0 for small to 1.0 for large. It's not very useful by itself but is useful for setting the relative size of the dock in a script.

Last edited by peavine (2021-11-15 07:29:53 pm)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#11 2021-11-16 02:47:43 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2154

Re: Get position of dock?

I think, following is more related to the horizontal dock's height (or width, when it is vertical). It gives different results from @Peavine's script. It should work on full screen as well.

Applescript:


set smallSize to 16
set largeSize to (do shell script "defaults read com.apple.dock largesize") as integer --> 128

tell application "System Events" to tell dock preferences
   set dockSize to dock size
   set pixels to round (smallSize + dockSize * (largeSize - smallSize))
   
   if screen edge is in {left, right} then
       -- is vertical
       tell scripting additions to display dialog "Dock width = " & pixels & " pixels"
   else
       -- is horizonrtal
       tell scripting additions to display dialog "Dock height = " & pixels & " pixels"
   end if
end tell

Last edited by KniazidisR (2021-11-16 03:05:46 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#12 2021-11-16 03:20:20 am

alastor933
Member
From:: Utrecht, NL
Registered: 2008-09-12
Posts: 587

Re: Get position of dock?

Dock settings are in a preferences file com.apple.dock.plist.
You can read/write this plist directly with the defaults command:

Applescript:

do shell script "defaults read com.apple.dock tilesize"
-- note that this returns a string, but the actual value is an integer

-- to set:
do shell script "defaults write com.apple.dock tilesize -int 37"

-- and
do shell script "defaults read com.apple.dock orientation"
-- possible values: left|bottom|right

-- change it:
do shell script "defaults write com.apple.dock orientation -string bottom"

-- when done changing values you should
do shell script "killall Dock"
-- to restart the Dock

NEVER modify plists by hand - changes will be overwritten by the system.
ONLY use defaults!

Offline

 

#13 2021-11-16 07:20:18 am

emendelson
Member
Registered: 2008-10-31
Posts: 395

Re: Get position of dock?

Many thanks for both of these. I'll test the two size-getting scripts and see which produces the best results. This is a wonderfully helpful forum. Thanks again to everyone!

Offline

 

#14 2021-11-16 07:41:49 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1133

Re: Get position of dock?

I decided to run some tests with my script and alastor933's read script with the dock sizes set to 0.0, 0.5, and 1.0 by way of System Events. The results were:

DOCK SIZE - PEAVINE - ALASTOR933
Small (0.0) - 32 - 16
Medium (0.5) - 93 - 72
Large (1.0) - 150 - 128

My script is based on the visibleFrame property, which is defined below, and it clearly does not return the actual pixel size of the dock. So, I'm sure alastor933's method is correct.

This rectangle defines the portion of the screen in which it is currently safe to draw your app’s content. The returned rectangle is always based on the current user-interface settings and does not include the area currently occupied by the dock and menu bar.... Even when dock hiding is enabled, the rectangle returned by this method may be smaller than the full screen. The system uses a small boundary area to determine when it displays the dock.

Last edited by peavine (2021-11-16 07:52:48 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#15 2021-11-16 08:06:50 am

emendelson
Member
Registered: 2008-10-31
Posts: 395

Re: Get position of dock?

That's very interesting and helpful.

Actually the visibleFrame property may be useful for my script. The script runs the DOSBox-X emulator, which runs Windows 3.11 in a 1024x768 window, and I want to center that window on the main screen. The script doesn't need GUI scripting, because I can set an environment variable that tells DOSBox-X the x and y coordinates of the upper left corner of the screen.

It's easy to get the x-position by getting the screen width, subtracting 1024, and dividing by 2. The y-position is the problem. I start with the screen height, subtract the 22 pixels for the menu bar, and then want to test the height of the dock (if it's horizontal) so that I can center the window between the menu bar and the dock. The visibleFrame property may be the most useful.

I'll continue to experiment later today and report back. Thank you again!

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)