Skip to content

Instantly share code, notes, and snippets.

@SKaplanOfficial
Created April 25, 2023 20:42
Show Gist options
  • Save SKaplanOfficial/e9087061af9420bed7df585f00508270 to your computer and use it in GitHub Desktop.
Save SKaplanOfficial/e9087061af9420bed7df585f00508270 to your computer and use it in GitHub Desktop.
JXA script to extract spoken word content from an audio file
(() => {
ObjC.import('Foundation');
ObjC.import('Speech');
var app = Application.currentApplication();
if (!$['SFSpeechRecognitionTaskDelegate']) {
ObjC.registerSubclass({
name: 'SFSpeechRecognitionTaskDelegate',
superclass: 'NSObject',
protocols: ['SFSpeechRecognitionTaskDelegate'],
properties: {
spokenContent: 'id'
},
methods: {
'speechRecognitionTask:didFinishRecognition:': {
types: ['void', ['id', 'id']],
implementation: function(task, result) {
this.spokenContent = result.bestTranscription.formattedString.js
},
}
}
})
}
const fileURL = $.NSURL.fileURLWithPath('/Users/exampleUser/Documents/Audio 1.wav');
const locale = $.NSLocale.localeWithLocaleIdentifier('en-US');
const recognizer = $.SFSpeechRecognizer.alloc.initWithLocale(locale);
const request = $.SFSpeechURLRecognitionRequest.alloc.initWithURL(fileURL);
const delegate = $.SFSpeechRecognitionTaskDelegate.alloc.init
recognizer.recognitionTaskWithRequestDelegate(request, delegate);
while (delegate.spokenContent.js == undefined) {
delay(0.1)
}
return delegate.spokenContent.js
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment