Is there any way to change the mac dock's position with a keyboard shortcut?
This AppleScript code works for me using the latest version of macOS Mojave.
tell application "System Events" to tell dock preferences
if screen edge is bottom then
set screen edge to left
else if screen edge is left then
set screen edge to bottom
end if
end tell
In Automator, create a new Service or Quick Action and add a run AppleScript action to that workflow, adding the above AppleScript code. Then in System Preferences you can create a keyboard shortcut for that new service.
The reason you get the Syntax Error:
Expected “given”, “with”, “without”, other parameter name, etc. but found identifier.
From:
defaults write com.apple.Dock orientation -string left;killall Dock
In Script Editor is because it is not AppleScript code!
It's a compound command to run in Terminal; however, it can be run in AppleScript by using the do shell script
command:
do shell script "defaults write com.apple.Dock orientation -string left;killall Dock"
That said though, I'd choose not to use it because the result of killall Dock
with unhide all hidden windows and can be very disruptive and messy.
An alternative is to use UI Scripting1 with System Preferences, and this can be done in a less disruptive manner then killall Dock
. 1Note that using UI Scripting may require accessibility privileges be granted.
The following example AppleScript code will toggle the position of the Dock between the Bottom and the Left side of the screen:
This was tested in Script Editor and works under macOS High Sierra.
if running of application "System Preferences" then
try
tell application "System Preferences" to quit
on error
do shell script "killall 'System Preferences'"
end try
end if
repeat while running of application "System Preferences" is true
delay 0.01
end repeat
tell application "System Preferences" to ¬
reveal anchor "Main" of pane id "com.apple.preference.dock"
tell application "System Events" to tell application process "System Preferences"
repeat while not (exists (window "Dock"))
delay 0.1
end repeat
tell radio group 1 of window "Dock"
if value of radio button "Bottom" is 1 then
click radio button "Left"
else if value of radio button "Left" is 1 then
click radio button "Bottom"
end if
end tell
end tell
quit application "System Preferences"
This example AppleScript code can be used in an Automator Service (Quick Action in macOS Mojave) using a Run AppleScript action, and assigned a keyboard shortcut in: System Preferences > Keyboard > Shortcuts > Services
Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5
, with the value of the delay set appropriately.