AppleScript to change default printer

I am trying to write a script to change the default printer, so I can attach it to a Network Location switch in Keyboard Masetro. I cannot work out how to change the printer in the ‘Default printer’ dropdown menu. Here is what I have so far:

tell application “System Preferences”
activate
end tell

tell application “System Events”
tell process “System Preferences”
click menu item “Printers & Scanners” of menu “View” of menu bar 1
delay 2
tell application “System Events”
– not sure what to put here. The desired printer name is “Xerox Phaser 3160”
end tell
end tell
end tell

Model: MacBook Pro 13" 2016
AppleScript: 2.7
Browser: Safari 15608.3.10.1.4
Operating System: macOS 10.14

I have not installed any printer on my Mac. So, try the following code yourself. It isn’t tested:


do shell script "lpoptions -d \"Xerox Phaser 3160\""

To see the list of available printers try this (not tested too):

do shell script "lpstat -p -d"