Automating macOS System Settings with JavaScript for Automation (JXA)


Introduction

Automation can be a powerful tool to streamline repetitive tasks and customize your computing environment. In this blog post, we’ll explore how to use JavaScript for Automation (JXA) on macOS to interact with System Settings, focusing on changing the tracking speed for the mouse.

Prerequisites

Before diving into the code, ensure that you have a basic understanding of JavaScript and macOS System Settings. Additionally, make sure that your script runner, in this case, osascript, has the necessary permissions to control System Settings.

The Code

#!/usr/bin/env osascript -l JavaScript
// Launch System Settings
Application("System Settings").reveal();
var settings = Application("System Events").applicationProcesses.byName("System Settings")
settings.frontmost = true

var mouseBtn = Application("System Events").applicationProcesses.byName("System Settings").menuBars[0].menuBarItems["View"].menus["View"].menuItems["mouse"]
mouseBtn.click();

delay(1)

function adjustSlider(sliderValueToSet, trackingSlider) {
    var currentSliderValue = trackingSlider.value();
    if (currentSliderValue < sliderValueToSet) {
        while (currentSliderValue < sliderValueToSet) {
            currentSliderValue++;
            trackingSlider.increment();
        }
    } else if (currentSliderValue > sliderValueToSet) {
        while (currentSliderValue > sliderValueToSet) {
            currentSliderValue--;
            trackingSlider.decrement();
        }
    }
}

function changeSecondaryClick() {
	// Switch Secondary Click
	var popup_btn = Application("System Events").applicationProcesses.byName("System Settings").windows[0].groups[0].splitterGroups[0].groups[1].groups[0].scrollAreas[0].groups[0].popUpButtons.byName("Secondary click")

	var trackingSlider = Application("System Events").applicationProcesses.byName("System Settings").windows[0].groups[0].splitterGroups[0].groups[1].groups[0].scrollAreas[0].groups[0].sliders["Tracking speed"]

	popup_btn.click()
	if (popup_btn.value() == "Click Right Side") {
		// left
		var sliderValueToSet = 9
		var currentSliderValue = trackingSlider.value()
		adjustSlider(sliderValueToSet, trackingSlider);
    	popup_btn.menus.menuItems["Click Left Side"].click()
	}
	else {
		// right
		var sliderValueToSet = 5
		var currentSliderValue = trackingSlider.value()
		adjustSlider(sliderValueToSet, trackingSlider);
	    popup_btn.menus.menuItems["Click Right Side"].click()
	}
}

function changeTrackingSpeed() {
	var trackingSlider = Application("System Events").applicationProcesses.byName("System Settings").windows[0].groups[0].splitterGroups[0].groups[1].groups[0].scrollAreas[0].groups[0].sliders["Tracking speed"]

	if (trackingSlider.value() == "9") {
		var sliderValueToSet = 5
		var currentSliderValue = trackingSlider.value()
		adjustSlider(sliderValueToSet, trackingSlider);
	}
	else {
		var sliderValueToSet = 9
		var currentSliderValue = trackingSlider.value()
		adjustSlider(sliderValueToSet, trackingSlider);
	}
}

// changeSecondaryClick()
changeTrackingSpeed()

// Close System Settings
Application("System Settings").quit()

Code Breakdown

Launching System Settings

The script begins by revealing and bringing the System Settings application to the front. This ensures that subsequent interactions with the UI elements are effective.

Function: adjustSlider

This function adjusts a slider to a specified value by incrementing or decrementing it until the desired value is reached. It takes the target value (sliderValueToSet) and the slider object (trackingSlider) as parameters.

Function: changeSecondaryClick

This function changes the setting for the secondary click on the mouse. It identifies the current state and adjusts the tracking speed accordingly. If the secondary click is set to the right side, it sets the tracking speed to 9; otherwise, it sets it to 5.

Function: changeTrackingSpeed

This function toggles between two predefined tracking speeds. If the current speed is 9, it sets it to 5, and vice versa.

Interacting with UI Elements

The script uses the click() method to interact with UI elements such as pop-up buttons and menus, and the value() method to retrieve or set the value of sliders.

Closing System Settings

Finally, the script closes the System Settings application.

Usage

You can customize this script based on your specific requirements. For example, uncommenting the changeSecondaryClick() line would enable the secondary click functionality.

In order to use it daily, you would need to add it as a service or as an application and give it appropriate permissions. This is how you would do it.

First of all open automator and create a workflow with your javascript as shown below

Next create this as a service. It should be shown in the Services options.

Next you can add and remove services from the System Preferences.

You can use the link to add the automator as a service

All the service scripts are stored in /Library/Services/*

Conclusion

JavaScript for Automation offers a versatile approach to automate macOS tasks. Understanding how to interact with UI elements in System Settings allows for customization of system preferences with just a few lines of code.


Feel free to expand on each section, provide more context, and add any necessary details for your audience.

In

Leave a Reply

Your email address will not be published. Required fields are marked *