Skip to content

Instantly share code, notes, and snippets.

View SKaplanOfficial's full-sized avatar

Stephen Kaplan SKaplanOfficial

View GitHub Profile
@SKaplanOfficial
SKaplanOfficial / displaySettingsWindow.applescript
Created December 16, 2025 12:49
AppleScript handler to open an application's Settings window via its "Settings..." menu item.
on displaySettingsWindow(theApplication)
(*
* Opens the settings window for an application via its "Settings..." menu item.
* Returns a reference to the front window of the corresponding application process.
*)
set appName to missing value
if class of theApplication is text then
set appName to theApplication
else if class of theApplication is application then
@SKaplanOfficial
SKaplanOfficial / clean-finder-tags.scpt
Last active December 16, 2025 14:43
Script to automate hiding tags from Finder's sidebar by scripting Finder's Settings → Sidebar → Tags configuration UI and unchecking all except for a predefined allowlist. Reports which tags were hidden. Both files are functionally equivalent.
--
-- clean-finder-tags.scpt
--
-- Author: Stephen Kaplan
-- Date: 2025-12-16
--
-- Description:
-- Automates hiding tags from Finder's sidebar by scripting Finder's
-- Settings → Sidebar → Tags configuration UI and unchecking all tags
-- except for a predefined allowlist. Reports which tags were hidden.
@SKaplanOfficial
SKaplanOfficial / README.md
Created August 26, 2025 04:47
Overview of Bike's outline path behavior.

Outline paths can be significantly more performant than AppleScript's filtering operations.

For example, //*/run::@strong gets interpreted as:

  1. For all descendants of the root node,
  2. Look at their associated rich text by using run:: axes, then
  3. Run predicate against the attributes associated with each rich text run’s attributes.

Within Bike, outline paths can return different kinds of values:

@SKaplanOfficial
SKaplanOfficial / NSTextStorageSubclass.swift
Created May 21, 2025 19:46 — forked from preble/NSTextStorageSubclass.swift
Base subclass of NSTextStorage in Swift
import Cocoa
@objc
class SomeTextStorage: NSTextStorage {
private var storage: NSMutableAttributedString
override init() {
storage = NSMutableAttributedString(string: "", attributes: nil)
super.init()
@SKaplanOfficial
SKaplanOfficial / now-playing.js
Last active December 1, 2025 15:38
AppleScript and JXA scripts to get Now Playing info. Works on macOS 15.4+, including macOS 26 beta 1.
function run() {
const MediaRemote = $.NSBundle.bundleWithPath('/System/Library/PrivateFrameworks/MediaRemote.framework/');
MediaRemote.load
const MRNowPlayingRequest = $.NSClassFromString('MRNowPlayingRequest');
const appName = MRNowPlayingRequest.localNowPlayingPlayerPath.client.displayName;
const infoDict = MRNowPlayingRequest.localNowPlayingItem.nowPlayingInfo;
const title = infoDict.valueForKey('kMRMediaRemoteNowPlayingInfoTitle');
@SKaplanOfficial
SKaplanOfficial / Example.md
Created April 6, 2025 14:54
Some example scripts showing how to make use of MarkEdit's AppleScript support.

MarkEdit Scripting Example Document

To run the MarkEdit example scripts, move or copy this file (Example.md) to your Downloads directory.

Very Important Content

The quick brown fox jumps over the lazy dog.

@SKaplanOfficial
SKaplanOfficial / Apple Event Debugging.sh
Created March 2, 2025 13:14
Enivronment variables for printing debug info to the Terminal when sending/receiving Apple Events.
export AEDebugSends=1 AEDebugReceives=1 AEDebugVerbose=1 AEDebugOSL=1 AEDebugFlattenedEvents=1 AEDebug=1; osascript ...

File Magic Numbers

Magic numbers are the first bits of a file which uniquely identify the type of file. This makes programming easier because complicated file structures need not be searched in order to identify the file type.

For example, a jpeg file starts with ffd8 ffe0 0010 4a46 4946 0001 0101 0047 ......JFIF.....G ffd8 shows that it's a JPEG file, and ffe0 identify a JFIF type structure. There is an ascii encoding of "JFIF" which comes after a length code, but that is not necessary in order to identify the file. The first 4 bytes do that uniquely.

This gives an ongoing list of file-type magic numbers.

Image Files

@SKaplanOfficial
SKaplanOfficial / Resize NSImage to Pixel Dimensions.swift
Created February 9, 2025 07:01
NSImage class extension method for resizing NSImages to specific pixel dimensions instead of points.
public extension NSImage {
/// Resizes the image to the specified pixel dimensions.
/// - Parameter newSize: The width and height (in pixels) to resize the image to.
func resize(to newSize: NSSize) -> NSImage {
guard let rep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: Int(newSize.width), pixelsHigh: Int(newSize.height), bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: .calibratedRGB, bytesPerRow: 0, bitsPerPixel: 0) else {
assertionFailure("Could not create NSBitmapImageRep")
return self
}
rep.size = newSize
@SKaplanOfficial
SKaplanOfficial / NSImage Pixel Dimensions.swift
Created February 9, 2025 07:00
NSImage class extension computed property for getting the raw pixel dimensions of an NSImage (rather than the point size, which can vary by screen resolution).
public extension NSImage {
/// The pixel width and height of the image.
var dimensions: NSSize {
let rep = self.representations.first
return NSSize(width: rep?.pixelsWide ?? 0, height: rep?.pixelsHigh ?? 0)
}
}