I am not sure if I am asking this question properly, but I am trying write a script to change some settings on Mac OS Sierra, similar to this script which sets the clock in the menubar to appear as analog:

killall Dock;
defauls write com.apple.menuextra.clock IsAnalog -bool true;
killall SystemUIServer;

Instead, I would like to select a different keyboard layout which I have created using Karabiner elements, which also appears in the menubar, and I can change it using the mouse.

My question is basically, how can I figure out what commands to put in my script to make changes like this? Is there a way to record commands that are run when making mouse actions, and then simply paste those commands into a script and alias it to some shortcut? Any help on how to become proficient writing scripts like this would be helpful.

migrated from stackoverflow.com Aug 16 '17 at 4:42

This question came from our site for professional and enthusiast programmers.


For scripting that interacts directly with macOS, you're much better off writing AppleScript over shell. AppleScript is Apple's proprietary scripting language that contains many ways to interact with the operating system. In addition, AppleScript was designed to be a very human readable language.

Plenty of documentation and tutorials for AppleScript can be found through a quick google search. Here are some Apple webpages to get you started:

Introduction to AppleScript
AppleScript fundamentals

To write AppleScripts, open up Script Editor, located in Applications > Utilities.

An example script to toggle macOS Sierra's dark mode, would be:

tell application "System Events"
tell appearance preferences
    if dark mode is false then
        set dark mode to true
        set dark mode to false
    end if
end tell
end tell

You must log in to answer this question.