Skip to content

Instantly share code, notes, and snippets.

@RobTrew
RobTrew / tidyJSCode.applescript
Last active December 3, 2024 10:40
Use js-beautify to reformat code in OS X 10.10 (Yosemite) Script Editor
// Ver 0.2
// Restores syntax highlighting by recompiling
// either restoring cursor or placing it at first error found
// Assumes installation of node.js
// http://nodejs.org/download/
// with npm at:
var pathNPM = '/usr/local/bin';
@RobTrew
RobTrew / KMVars.js
Last active December 3, 2024 10:39
Example: accessing Keyboard Maestro variables from Yosemite JXA Javascript for Applications
function run() {
// KEYBOARD MAESTRO VARIABLES IN YOSEMITE JXA JAVASCRIPT FOR APPLICATIONS
// FINDING, CREATING, READING, UPDATING
// SIMPLE EXAMPLE - TOGGLING A BOOLEAN KM VARIABLE FROM .js
var strKMVarName = "flag";
@RobTrew
RobTrew / JXA_UIToggle.applescript
Created February 24, 2015 13:06
OS X Yosemite Javascript for Applications (JXA): Toggling background, dark mode, and screen color inversion between day ⇄ night settings
function run() {
// Rob Trew 2015
// Ver 0.01
// UI day/night toggling through OS X 10.10 JXA Javascript for Automation
// SWITCH FROM DAY-TIME DARK MENU BAR AND DOCK WITH DARK BACKGROUND
// TO NIGHT-TIME *ALL DARK*
// (TOGGLE MODE AND BACKGROUND TO BRIGHT, THEN INVERT ALL)
@RobTrew
RobTrew / soundDeviceToggle.js
Last active January 6, 2025 21:45
Toggling OS X Sound Output Devices with JXA Javascript for Automation (OS X 10.10)
// Rob Trew @complexpoint 2015
function run() {
"use strict";
var blnUseKeyboardMaestro = false;
// EDIT TO NAMES OF TWO ALTERNATIVE SOUND OUTPUT DEVICES
var dctSources = { // A unique substring for each is enough
primary: "Internal",
secondary: "Elgato"
@RobTrew
RobTrew / jxaClickAppSubMenuItem.applescript
Last active January 7, 2025 04:19
Yosemite JXA Javascript Function for clicking application sub-menu items
// Click an OS X app sub-menu item
// 2nd argument is an array of arbitrary length (exact menu item labels, giving full path)
// e.g. menuItemClick("InqScribe", ['View', 'Aspect Ratio', 'Use Media Ratio'])
// Note that the menu path (spelling & sequence) must be exactly as in the app
// See menuItemTestClick() below for a slower version which reports any errors
// For macOS Yosemite to Sierra
@RobTrew
RobTrew / bashSelectByGlobInFinder.sh
Last active April 5, 2021 15:18
Bash command `lf` - like ls but selects glob-matched files in an OS X Finder window
# An lf command in my ~/.bash_profile
# like a simple ls, but selects the glob-matched files in the Finder
# `lf *.txt` to Finder-select all *.txt files in the working directory
# `lf *.[^t]?? to Finder-select all files with a 3-char extension which doesn't start with t
lf() {
local IFS=":"; local f=$@; local seln=""; fldr="$(pwd)"
for l in ${f[@]}; do
if [[ ! -d $l ]]; then
seln=$seln"file \"$l\", "
fi
@RobTrew
RobTrew / safariXPathGoogleResultLinks.js
Created May 30, 2015 02:24
Harvest Google result links from Safari with XPATH (as MD) with //*[@Class='r']/a
@RobTrew
RobTrew / JXA_PersistentProperties.js
Last active December 3, 2024 10:37
Persistent 'properties' for OS X JavaScript for Applications (JXA)
(function () {
'use strict';
// OSX JavaScript for Applications lacks the persistent 'properties'
// of AppleScript (in which global variables and properties persist between script runs)
// but we can, of course, serialise to JSON or plist at the end of a script
// parsing it at the start of the next run.
// Here is one approach to persistence between script runs
// using JSON.stringify() and JSON.parse()
@RobTrew
RobTrew / dtSheetTableMMD.js
Last active October 3, 2022 18:43
Selected DEVONthink Sheet -> DEVONthink markdown record (MMD table)
((options) => {
'use strict';
// (NB JavaScript for Automation – JXA: Save as .scpt )
// Selected DEVONthink Sheet -> DEVONthink markdown record (MMD table)
// (New record containing MMD table created is same group as selection)
// Uses original fileName stem, with '.md' appended.
// Default alignment string is centered ':--:'