Skip to content

Instantly share code, notes, and snippets.

View SKaplanOfficial's full-sized avatar

Stephen Kaplan SKaplanOfficial

View GitHub Profile
@SKaplanOfficial
SKaplanOfficial / SummarizeObjCMethod.scpt
Last active October 26, 2023 01:25
JXA/ASObjC script to briefly summarize methods and properties of an Objective-C class using objc runtime methods (e.g. class_copyMethodList, method_getTypeEncoding, etc.). The script reports method names, the number of arguments they accept, the argument types, property names, property types, and property attributes such as read-only and nonatomic.
(() => {
ObjC.import('objc');
ObjC.import('AppKit')
// Get methods of ObjC class, store # of methods and properties in reference objects
const targetClass = $.NSImage;
const num_methods = Ref();
const num_properties = Ref();
const methods = $.class_copyMethodList(targetClass, num_methods);
const properties = $.class_copyPropertyList(targetClass, num_properties)
@SKaplanOfficial
SKaplanOfficial / SpeechRecognizer.scpt
Last active January 3, 2024 04:10
Sample JXA script for running speech recognition on audio. Shows how you can supply code blocks to ObjC methods in JXA (lines 33-37).
ObjC.import("AVFoundation");
ObjC.import('Speech');
ObjC.import("objc");
function recordForDuration(duration, destination) {
const settings = $.NSMutableDictionary.alloc.init;
settings.setValueForKey($.kAudioFormatAppleIMA4, $.AVFormatIDKey);
// Some macOS versions fail to link $.AVAudioFormat, so we manually get the class
const format = $.objc_getClass("AVAudioFormat").alloc.initWithSettings(settings);
@SKaplanOfficial
SKaplanOfficial / MailAction.scpt
Created September 7, 2023 04:34
Example JXA script to perform mail actions. Place the script in ~/Library/Application Scripts/com.apple.mail/.
function performMailActionWithMessages(messages, options) {
// do stuff, e.g.:
const app = Application('System Events');
app.includeStandardAdditions = true;
// messages are provided as a list
app.displayDialog(messages.map((message) => message.subject()).join(', '))
// options are provided as an object with two properties: inMailboxes and forRule
// both options can be undefined
@SKaplanOfficial
SKaplanOfficial / List Installed Applications.applescript
Created July 20, 2023 17:53
AppleScriptObjC script to get a list of the currently installed applications using a Spotlight search.
use framework "Foundation"
property ca : current application
property theResult : ""
property query : missing value
try
set result to ""
ca's NSNotificationCenter's defaultCenter's addObserver:me selector:"queryDidFinish:" |name|:"NSMetadataQueryDidFinishGatheringNotification" object:(missing value)
set predicate to ca's NSPredicate's predicateWithFormat:"kMDItemContentType == 'com.apple.application-bundle'"
@SKaplanOfficial
SKaplanOfficial / ScreenCapture.scpt
Created July 4, 2023 23:21
JXA script to take a screenshot and output a PNG file using ScreenCaptureKit. Provides an example of how to use the dispatch framework from JXA.
function run(argv) {
const outputPath = argv[2] // Path to output a .png to
const windowOnly = argv[3] // Whether to screenshot just the frontmost window (true/false)
ObjC.import('/System/Library/Frameworks/ScreenCaptureKit.framework');
ObjC.import('CoreMedia');
ObjC.import('CoreGraphics');
ObjC.import('CoreImage');
ObjC.import('dispatch');
ObjC.import('objc');
(() => {
ObjC.import("AppKit");
ObjC.import("WebKit");
ObjC.import("objc");
app = Application("iCab");
const baseURL = app.windows[0].currentTab.url();
// Size of WebView
const width = 1080;
@SKaplanOfficial
SKaplanOfficial / RecordAudio.scpt
Last active June 27, 2023 14:14
JXA script to record audio and store it into an wav file.
// Reference: https://developer.apple.com/documentation/avfaudio/avaudiorecorder?language=objc
(() => {
ObjC.import("objc");
ObjC.import("AVFoundation");
const outputURL = $.NSURL.alloc.initFileURLWithPath("/Users/exampleUser/Downloads/test.mp4");
const settings = $.NSMutableDictionary.alloc.init
settings.setValueForKey($.kAudioFormatAppleIMA4, $.AVFormatIDKey)
@SKaplanOfficial
SKaplanOfficial / RaycastWindowState.js
Created May 30, 2023 12:40
JXA script to check if the Raycast main window is currently open.
(() => {
ObjC.import("CoreGraphics");
const windowList = ObjC.castRefToObject($.CGWindowListCopyWindowInfo($.kCGWindowListOptionAll, $.kCGNullWindowID))
const raycastWindow = windowList.js.find((win) => win.allKeys.containsObject("kCGWindowIsOnscreen") && win.js["kCGWindowLayer"].js == 8 && win.js["kCGWindowOwnerName"].js == "Raycast")
return raycastWindow != undefined
})()
@SKaplanOfficial
SKaplanOfficial / SummarizeText.js
Created May 25, 2023 01:59
JXA script to summarize text using SKSummary from the CoreServices framework.
(() => {
ObjC.import("CoreServices")
const unsummarizedText = `Inventore eveniet eum veritatis inventore necessitatibus provident. Sit reprehenderit non ad. Blanditiis impedit sapiente. Incidunt harum neque commodi rerum nobis dolor aliquam. Ipsam consequatur distinctio ratione veniam hic. Possimus iure molestiae tempore cumque reiciendis repudiandae quibusdam.`
const numOutputSentences = 3
const CFStringRef = $.CFStringCreateWithCString(null, unsummarizedText, $.kCFStringEncodingMacRoman)
const summaryObj = $.SKSummaryCreateWithString(CFStringRef)
const summaryText = ObjC.castRefToObject($.SKSummaryCopySentenceSummaryString(summaryObj, numOutputSentences)).js
return summaryText
@SKaplanOfficial
SKaplanOfficial / GetFirstFrameOfVideo.scpt
Created May 25, 2023 01:33
JXA script to extract the first frame of a video and save it into a TIFF file.
(() => {
ObjC.import("objc");
ObjC.import("CoreMedia");
ObjC.import("Foundation");
ObjC.import("AVFoundation");
ObjC.import("CoreGraphics");
ObjC.import("CoreImage");
ObjC.import("AppKit");
const outputPath = "/Users/example/Downloads/example.tiff"