Skip to content

Instantly share code, notes, and snippets.

@RobTrew
RobTrew / KMVars.js
Last active March 9, 2024 04:37
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_PersistentProperties.js
Last active January 30, 2024 09:07
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 / soundDeviceToggle.js
Last active May 2, 2023 18:20
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 / safariXPathGoogleResultLinks.js
Created May 30, 2015 02:24
Harvest Google result links from Safari with XPATH (as MD) with //*[@Class='r']/a
@RobTrew
RobTrew / DraftsFunctions_ScriptEditor.js
Last active April 12, 2023 07:26
OS X 10.10 (Yosemite) definitions of the basic script editing functions used in iOS Drafts 4
// Ver 0.4 Rob Trew
// Library for using OSX Yosemite Script Editor scripts
// written in an idiom compatible with FoldingText, Drafts, 1Writer, TextWell
// In iOS Drafts 4 scripts, the header is simply:
// var drafts = this;
// For headers for FoldingText and other editors, see:
// See: http://support.foldingtext.com/t/writing-scripts-which-run-on-both-foldingtext-and-ios-drafts-4/652/7
// Call functions, after this header, with the prefix:
// drafts.
@RobTrew
RobTrew / wrapInSimpleTimingCode.js
Last active January 21, 2023 00:54
OS X 10.10 (Yosemite) Script Editor - Wrap selected line(s) in simple timing code (for performance experiments)
// ROBIN TREW 2015 MIT License
// OSX 10.10 SCRIPT EDITOR – WRAP SELECTED LINE(S) IN SIMPLE TIMING CODE (FOR PERFORMANCE EXPERIMENTS)
// Add remove '// ' from before printing characters
// Depends on a library .scpt being saved at ~/Library/Script Libraries/DraftsSE.scpt
// See: See: http://support.foldingtext.com/t/writing-scripts-which-run-on-both-foldingtext-and-ios-drafts-4/652/7
// INCLUDE LIBRARY OF IOS DRAFTS-COMPATIBLE EDITOR FUNCTIONS:
@RobTrew
RobTrew / outdentLines.js
Last active January 21, 2023 00:54
Outdent selected line(s) in OS X 10.10 Script Editor
// ROBIN TREW 2015 MIT License
// OSX 10.10 SCRIPT EDITOR – REDUCE INDENTATION OF SELECTED LINES (by Tab or 4 spaces)
// Add remove '// ' from before printing characters
// Depends on a library .scpt being saved at ~/Library/Script Libraries/DraftsSE.scpt
// See: See: http://support.foldingtext.com/t/writing-scripts-which-run-on-both-foldingtext-and-ios-drafts-4/652/7
// INCLUDE ALL OR PART OF LIBRARY OF IOS DRAFTS-COMPATIBLE EDITOR FUNCTIONS:
@RobTrew
RobTrew / indentLines.js
Last active January 21, 2023 00:54
Indent selected line(s) in OS X 10.10 Script Editor
// ROBIN TREW 2015 MIT License
// OSX 10.10 SCRIPT EDITOR – INDENT SELECTED LINES (with Tab or 4 spaces)
// Add remove '// ' from before printing characters
// Depends on a library .scpt being saved at ~/Library/Script Libraries/DraftsSE.scpt
// See: See: http://support.foldingtext.com/t/writing-scripts-which-run-on-both-foldingtext-and-ios-drafts-4/652/7
// INCLUDE ALL OR PART OF LIBRARY OF IOS DRAFTS-COMPATIBLE EDITOR FUNCTIONS:
@RobTrew
RobTrew / toggleComments.js
Last active January 21, 2023 00:53
Toggle selected comments in OS X 10.10 Script Editor
// ROBIN TREW 2015 MIT License
// OSX 10.10 SCRIPT EDITOR – TOGGLE COMMENTS
// Add remove '// ' from before printing characters
// Depends on a library .scpt being saved at ~/Library/Script Libraries/DraftsSE.scpt
// See: See: http://support.foldingtext.com/t/writing-scripts-which-run-on-both-foldingtext-and-ios-drafts-4/652/7
// INCLUDE LIBRARY OF IOS DRAFTS-COMPATIBLE EDITOR FUNCTIONS:
@RobTrew
RobTrew / tidyJSCode.applescript
Last active January 21, 2023 00:53
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';