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');
@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)
@SKaplanOfficial
SKaplanOfficial / ASObjC Resize NSImage to Pixel Dimensions.applescript
Created February 9, 2025 06:57
ASObjC handler to resize an NSImage to specific pixel dimensions rather than points.
on resize(sourceImage, newSize)
set rep to current application's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(newSize's width) pixelsHigh:(newSize's height) bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:no colorSpaceName:(current application's NSCalibratedRGBColorSpace) bytesPerRow:0 bitsPerPixel:0
rep's setSize:newSize
current application's NSGraphicsContext's saveGraphicsState()
current application's NSGraphicsContext's setCurrentContext:(current application's NSGraphicsContext's graphicsContextWithBitmapImageRep:rep)
sourceImage's drawInRect:(current application's NSMakeRect(0, 0, newSize's width, newSize's height)) fromRect:(current application's NSZeroRect) operation:(current application's NSCompositeCopy) fraction:1.0
current application's NSGraphicsContext's restoreGraphicsState()
set newImage to current application's NSImage's alloc()'s initWithSize:newSize
@SKaplanOfficial
SKaplanOfficial / NSImage Color Swatch.swift
Created February 9, 2025 06:56
NSImage class extension initializer for quickly creating swatches of NSColors.
public extension NSImage {
/// Initializes and returns a new color swatch image.
/// - Parameters:
/// - color: The fill color of the image.
/// - size: The width and height of the image.
convenience init(color: NSColor, size: NSSize) throws {
self.init(size: size)
guard let ciColor = CIColor(color: color) else {
assertionFailure("Could not create CIColor from NSColor")