Created
November 1, 2019 17:30
-
-
Save freecodecampster/8679de0432c545e7ed45f8a252553fdb to your computer and use it in GitHub Desktop.
Using JavaScript instead of AppleScript to automate a QuickTime Player Audio Recording for a specified time on macOS Catalina.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
JXA JavaScript for Automation | |
Working in macos Catalina 10.15.1 | |
Resources: | |
https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/index.html#//apple_ref/doc/uid/TP40016239-CH56-SW1 | |
*/ | |
editor = Application.currentApplication(); // Script Editor | |
editor.includeStandardAdditions = true; | |
var app = Application('QuickTime Player'); | |
// Need standard additions for UI Scripting | |
app.includeStandardAdditions = true | |
console.log("How many minutes would you like to record for?"); | |
var response = editor.displayDialog("How many minutes would you like to record for?", { | |
defaultAnswer: "", | |
withIcon: "note", | |
buttons: ["Cancel", "Continue"], | |
defaultButton: "Continue" | |
}) | |
/* Set variables */ | |
var recordingTimeInMinutes = response.textReturned; | |
var recordingTimeInSeconds = recordingTimeInMinutes * 60; | |
/* Examples of date and path variables */ | |
/* You have to use `backticks` for string interpolation to work. Single and double quotes don’t work! */ | |
var dateString = editor.currentDate().toString(); | |
// Not used in this example | |
var path = Path(`/Users/yourusername/${dateString}.m4a`) | |
/* Start a QuickTime Player audio recording and stop after delay */ | |
app.activate() | |
var audioRec = app.newAudioRecording(); | |
audioRec.start(); | |
delay(recordingTimeInSeconds); | |
app.activate(); | |
audioRec.stop(); | |
/* UI Scripting to bring up save sheet, enter a name, and press return */ | |
events = Application("System Events") | |
app.activate(); | |
// Wait for QuickTime Player to be on top | |
delay(1); | |
events.keystroke( | |
"s", | |
{using:"command down"} | |
); | |
// Wait for save sheet | |
delay(2); | |
/* ` Backticks ` required for string interpolation */ | |
events.keystroke( | |
`${dateString}.m4a` | |
); | |
delay(2); | |
/* press return key | |
https://eastmanreference.com/complete-list-of-applescript-key-codes | |
*/ | |
events.keyCode( | |
36 | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment