Oliver Pearmain
Oliver Pearmain

Reputation: 20590

Applescript - Set value of System Preferences dock size slider on MacOS Monterey

I am trying to use AppleScript to change the dock size to a specified value. My OS is MacOS Monterey v12.0 which is likely important.

I am able to get a handle on the appropriate "Dock Size" slider but I cannot work out how to set its value directly.

Given I am in a tell slider block I have tried...

What DOES work but IS NOT precise enough for my requirements, is using increment/decrement

repeat while value is less than targetValue
    increment
end repeat
repeat while value is greater than targetValue
    decrement
end repeat

...but this is very imprecise and ultimately sets the value to a range that's not precise enough for my liking.


My full script is below. I am invoking it from the command line with

$ osascript -s eo /path/to/file/Resize-Dock.applescript 0.3

Resize-Dock.applescript

#!/usr/bin/osascript

on run argv

    set targetValue to item 1 of argv 

    if running of application "System Preferences" then
        quit application "System Preferences"
        delay 1
    end if

    tell application "System Preferences"
        activate
        reveal pane id "com.apple.preference.dock"
        delay 1
        
        tell application "System Events"
            
            tell slider 1 of group 1 of window "Dock & Menu Bar" of application process "System Preferences"
                                
                set currentValue to value of value indicator 1
                log "  Dock size value BEFORE = " & currentValue
            
                set focused to true
                
                ######## HERE IS WHERE I NEED HELP PLEASE ########
                set value of value indicator 1 to targetValue
                
                set currentValue to value of value indicator 1
                log "  Dock size value AFTER = " & currentValue
                
            end tell
            
        end tell
    
    end tell

    if running of application "System Preferences" then
        quit application "System Preferences"
    end if

end run


PS: Yes I am aware that I have the option of avoiding AppleScript and writing straight to the defaults with something like...

defaults write com.apple.dock tilesize -int 60
killall Dock

However this has the MAJOR drawback that it borks the application badge counts. I've spent much time trying to solve that directly and now I'm just looking to drive the change via AppleScript to specifically avoid this.


Really appreciate any help πŸ™πŸΌπŸ™πŸΌπŸ™πŸΌπŸ™πŸΌπŸ™πŸΌπŸ™πŸΌ

Upvotes: 1

Views: 689

Answers (1)

user3439894
user3439894

Reputation: 7555

This works for me all by itself in macOS Monterey.

Example AppleScript code:

tell application "System Events" to Β¬
    tell dock preferences to set its dock size to 0.3


Notes:

The example AppleScript code, shown above, was tested in Script Editor under macOS Monterey with Language & Region settings in System Preferences set to English (US) β€” Primary and worked for me without issue1.

  • 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.

Note that while tested under macOS Monterey, nonetheless, this should also work in other versions of macOS.

Also works from Terminal as:

osascript -e 'tell application "System Events" to tell dock preferences to set its dock size to 0.3'

Or can be used in a shell script with a #!/usr/bin/osascript shebang using an on run argv handler., e.g.:

Example AppleScript code:

#!/usr/bin/osascript

on run argv
    set targetValue to item 1 of argv
    tell application "System Events" to tell dock preferences to set its dock size to targetValue
end run

Upvotes: 3

Related Questions