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?

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.

Without GUI scripting:


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

Perfect! Thank you!

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’.

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…”

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

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.

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

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

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.

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

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.

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.

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.


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

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

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!

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!

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.

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!