Skip to content

Instantly share code, notes, and snippets.

View SKaplanOfficial's full-sized avatar

Stephen Kaplan SKaplanOfficial

View GitHub Profile
@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 / 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 / AppleScriptCheatsheet.md
Created April 5, 2023 23:47
Cheatsheet providing an overview of AppleScript's features.
title updated layout category prism_languages intro
AppleScript
2023-04-05
2017/sheet
macOS
applescript
AppleScript is a scripting language for automating macOS.

Running

@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 / saved_current_url.py
Last active September 15, 2023 16:22
PyXA script to save the current Safari tab's URL to a "Saved URLs" note
#!/usr/bin/env python
# Test with PyXA 0.1.0
import PyXA
safari = PyXA.Application("Safari")
notes = PyXA.Application("Notes")
# Get info for current Safari tab
current_tab = safari.front_window.current_tab
@SKaplanOfficial
SKaplanOfficial / select_photos_for_mosaic.py
Last active September 15, 2023 16:22
Using PyXA, Automator, and PIL to create a mosaic of selected images.
import PyXA, math
from PIL import Image
# Execute Automator workflow and receive list of image paths
automator = PyXA.Application("Automator")
workflow = automator.open("/Users/exampleuser/Library/Mobile Documents/com~apple~Automator/Documents/Ask For Photos.workflow")
image_paths = workflow.execute()
# Set base dimensions of mosaic images
base_width = 400
@SKaplanOfficial
SKaplanOfficial / count_shortcuts.py
Last active September 15, 2023 16:22
Using PyXA to get the number of shortcuts in each shortcuts folder
# Tested with PyXA 0.1.0
import PyXA
app = PyXA.Application("Shortcuts")
folders = app.folders()
# Method 1 - Standard iteration
summary = []
for folder in folders:
folder_name = folder.name
num_shortcuts = len(folder.shortcuts())
@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 / RaycastAIPrompts.md
Created April 4, 2023 20:04
My ad-hoc prompts for Raycast AI

Raycast AI Prompts

Content Generation

Title Prompt
Brainstorm Ideas Based On This Brainstorm 5 project ideas based on this text:
Create Action Items Generate a markdown list of action items to complete based on the following text, using a unique identifier for each item as bold headings. If there are any errors in the text, make actions items to fix them. In a sublist of each item, provide a description, priority, estimated level of difficulty, and a reasonable duration for the task. Here is the text:
Create Flashcards Create 3 Anki flashcards based on the following text. Format the response as markdown with the bold questions and plaintext answers. Separate each entry with ‘—‘. Here’s the text:
Generate Cheatsheet Generate a concise cheatsheet for the concepts in this text. Add additional details based on your own knowledge of the topic.