Skip to content

Instantly share code, notes, and snippets.

View SKaplanOfficial's full-sized avatar

Stephen Kaplan SKaplanOfficial

View GitHub Profile
@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")
@SKaplanOfficial
SKaplanOfficial / set_custom_icon_spacing.sh
Created June 20, 2024 07:16
Script to configure custom icon spacing in the macOS menubar
defaults -currentHost write -globalDomain NSStatusItemSpacing -int 6
defaults -currentHost write -globalDomain NSStatusItemSelectionPadding -int 6
killall ControlCenter
@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)