unixorn/tumult.plugin.zsh

View on GitHub
bin/set-cursor-size

Summary

Maintainability
Test Coverage
#!/usr/bin/env bash
#
# Based on https://github.com/wookayin/dotfiles/blob/master/bin/cursor-size
#
# set-cursor-size: A command line utility that controls macOS pointer size.

function fail() {
  printf '%s\\n' "$1" >&2  ## Send message to stderr. Exclude >&2 if you don't want it that way.
  exit "${2-1}"  ## Return a code specified by $2 or 1 by default.
}

function has() {
  # Check if a command is in $PATH
  which "$@" > /dev/null 2>&1
}

function usage() {
  cat << EOF
Usage: $0 CURSOR_SIZE
CURSOR_SIZE ranges from 1 to 4, where 1 is the smallest and 4 is the largest.
  (See System Preferences - Accessbility - Display - Pointer Size)
EOF
}

# Ensure that this script is running on macOS
if [ "$(uname)" != "Darwin" ]; then
  fail "This script can run only on macOS."
  exit 1;
fi

VALUE="$1"

if [[ -z "$VALUE" ]]; then
  usage;
  exit 1;
# shellcheck disable=SC2046
elif [ 1 -eq $(echo "$VALUE > 4.0" | bc -l) ] || [ 1 -eq $(echo "$VALUE < 1.0" | bc -l) ]; then
  usage;
  exit 1;
fi

# Based on https://apple.stackexchange.com/questions/298550/how-to-increase-cursor-size-programmatically
osascript -l JavaScript << EOF
function cursor_size() {
    const isRunningSystemPreferences = Application('System Preferences').running();
    Application('System Preferences').panes.byId('com.apple.preference.universalaccess').anchors.byName('Seeing_Cursor').reveal()
    const process = Application('System Events').processes.byName('System Preferences');
    while (process.windows.length == 0);
    const window = process.windows[0]
    while (window.groups.length == 0);
    const group = window.groups[0]
    while (group.tabGroups.length == 0);
    const tabGroup = group.tabGroups[0]
    while (tabGroup.sliders.length == 0);
    const slider = tabGroup.sliders[0]
    slider.value = $VALUE;
    if (!isRunningSystemPreferences) {
        Application('System Preferences').quit()
    }
}
cursor_size()
EOF