Skip to content

Instantly share code, notes, and snippets.

View SKaplanOfficial's full-sized avatar

Stephen Kaplan SKaplanOfficial

View GitHub Profile
@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
Created May 21, 2025 05:26
AppleScript and JXA scripts to get Now Playing info. Works on macOS 15.4+.
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');
(() => {
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 / 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');
@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)
}
}
@SKaplanOfficial
SKaplanOfficial / NSImage Color At Coordinate.swift
Created February 9, 2025 06:59
NSImage class extension method to get the NSColor of the image at the specified (x,y) coordinate.
public extension NSImage {
/// Gets the color at the specified coordinate.
/// - Parameters:
/// - x: The horizontal coordinate.
/// - y: The vertical coordinate.
func colorAt(x: Int, y: Int) -> NSColor? {
if let tiffRep = self.tiffRepresentation {
if let bitmapRep = NSBitmapImageRep(data: tiffRep) {
let flippedY = Int(bitmapRep.size.height) - y
let color = bitmapRep.colorAt(x: x, y: flippedY)