If you use your iPad as a second display for your Mac via Sidecar, you can connect to it using a keyboard shortcut with Keyboard Maestro.

You can download the macro here or create it yourself using the following instructions. (If you download it, you’ll still need to set the device name as described below.)

Create a macro with an Execute AppleScript action. Copy and paste this code into the action:

set deviceName to "The name of your iPad"

tell application "System Events"
	tell process "SystemUIServer"
		click (menu bar item 1 of menu bar 1 whose description contains "Displays")
		set displaymenu to menu 1 of result
		if ((menu item 1 where its name starts with deviceName) of displaymenu) exists then
			-- Not connected, so click the name of the device to connect
			click ((menu item 1 where its name starts with deviceName) of displaymenu)
		else
			-- Connected, so click "Disconnect" to disconnect
			click ((menu item 1 where its name starts with "Disconnect") of displaymenu)
		end if
	end tell
end tell

In the first line of the script, change "The name of your iPad" to the real name of your iPad (leave the quotation marks around the name). (You can find your iPad’s name in the AirPlay menu.)

  Name of device in AirPlay menu
Find your iPad’s name in the AirPlay menu

Set your desired hot key (I’m using ⌃ Key Pad +), and you’re all set. Press the hot key, and your Mac will connect to the iPad. Press the hot key again, and it will disconnect.

(You could probably connect to an Apple TV using the same script, but I don’t have an Apple TV, so I can’t verify it.)

Apple’s documentation shows that to open a file for writing in a JXA script, you call the openForAccess method on an Application instance. What the documentation doesn’t make clear is that you must call openForAccess on the instance returned by Application.currentApplication(). Anything else will fail.

If you run this code in Script Editor…

let app = Application('Finder');
app.includeStandardAdditions = true;
let desktop = app.pathTo('desktop').toString();
let myFile = `${desktop}/temp.txt`;
let openedFile = app.openForAccess(Path(myFile), { writePermission: true });
app.closeAccess(openedFile);

… it produces an privilege violation error.

  Script Editor error message

Script Editor even helpfully tells you that the privilege violation occurred on the line where we attempted to open the file.

But if you change Application('Finder') to Application.currentApplication(), it works.

let app = Application.currentApplication();
app.includeStandardAdditions = true;
let desktop = app.pathTo('desktop').toString();
let myFile = `${desktop}/temp.txt`;
let openedFile = app.openForAccess(Path(myFile), { writePermission: true });
app.closeAccess(openedFile);
  Script Editor no error message

(“Undefined” simply means the last statement didn’t return a value.)

If you need to open a file for writing, you must set a variable to Application.currentApplication() and call openForAccess on that Application instance — even if you’re already working with another application.

I was looking at one of my playlists and wanted to know which songs I owned and which were from Apple Music, so I wrote a JXA (JavaScript for Automation) script. It works in macOS Catalina (10.15) and Mojave (10.14). Learn more and download the script from the GitHub repository.

Sunset and trees
 Jan 7, 2020 18:18
Last light
Southwest Georgia pines and sky
 Dec 25, 2019 10:42
Southwest Georgia pines and sky