Invert Colors and Toggle Grayscale with JavaScript on macOS

Via reboot81:

Open Script Editor and paste each block into a new script for you to use.

Invert Colors

var systemEvents = Application("System Events");
var systemPreferences = Application("System Preferences");

systemPreferences.activate();

systemPreferences.panes.byId(
    "com.apple.preference.universalaccess"
  ).anchors.byName("Seeing_Display").reveal();


chkInvert = systemEvents.applicationProcesses.byName(
    "System Preferences"
  ).windows.byName("Accessibility").checkboxes.
      byName("Invert colors");

systemEvents.click(chkInvert);

systemPreferences.quit();

Toggle Grayscale

var systemEvents = Application("System Events");
var systemPreferences = Application("System Preferences");

systemPreferences.activate();

systemPreferences.panes.byId(
    "com.apple.preference.universalaccess"
  ).anchors.byName("Seeing_Display").reveal();


chkInvert = systemEvents.applicationProcesses.byName(
    "System Preferences"
  ).windows.byName("Accessibility").checkboxes.
      byName("Use grayscale");

systemEvents.click(chkInvert);

systemPreferences.quit();