Skip to content

Instantly share code, notes, and snippets.

@macshome
macshome / Usernames.swift
Last active June 2, 2025 13:55
A playground to understand how to find the current username on macOS.
// Some Swift code for different ways to find the current username on macOS.
//
// You can run this in a Playground, or compile it with `swiftc`. Compiling
// it will let you experiment with different ways of running the command.
print("********** Foundation Usernames **********")
// If you are only importing Foundation you have a few options to get the current user.
// Note that if run from the CLI they will report the CLI user running them.
import Foundation
@maximko
maximko / borg-macos-backup.sh
Last active May 28, 2025 00:34
Backup macOS using borg and apfs snapshots
#!/usr/local/bin/bash
export BORG_REPO="ssh://user@server/borg-repo"
if [[ ! "${USER}" == "root" ]]; then
echo Start this script as root
exit 1
fi
echo === Present snapshots ===
@ole
ole / thirty-days-of-metal.md
Last active June 9, 2025 23:00
Warren Moore – Thirty Days of Metal
@stephancasas
stephancasas / close-other-vs-code-windows.jxa.js
Created April 20, 2023 22:46
Close all other VSCode windows
#!/usr/bin/env osascript -l JavaScript
function run(_) {
const pid = $.NSRunningApplication.runningApplicationsWithBundleIdentifier(
'com.microsoft.VSCode',
).firstObject.processIdentifier;
// Bring all VSCode windows to the current Mission Control view.
$.CGSProcessAssignToAllSpaces($.CGSMainConnectionID(), pid);
@stephancasas
stephancasas / new-file-using-app-dock-menu.jxa.js
Created April 6, 2023 00:19
AXUIElement JXA Objective-C Bridge Example
#!/usr/bin/env osascript -l JavaScript
const App = Application.currentApplication();
App.includeStandardAdditions = true;
ObjC.import('Cocoa');
ObjC.bindFunction('AXUIElementCreateApplication', ['id', ['unsigned int']]);
ObjC.bindFunction('AXUIElementCopyAttributeValue', [
'int',
@stephancasas
stephancasas / toggle-ventura-sidecar.jxa.js
Last active June 5, 2025 08:44
Toggle sidecar or screen mirroring from Control Center in macOS Ventura
#!/usr/bin/env osascript -l JavaScript
/**
* -----------------------------------------------------------------------------
* Activate Sidecar/Screen Mirroring from Control Center
* -----------------------------------------------------------------------------
*
* Created on February 17, 2023 by Stephan Casas
* Updated on May 18, 2023 by Stephan Casas
*
@siracusa
siracusa / Pro Display XDR at 120Hz bandwidth calculation.md
Last active November 1, 2024 07:51
Pro Display XDR at 120Hz bandwidth calculations

From Jonathan Dietz, Jr.:

Almost all new displays employ VESA Coordinated Video Timings using Reduced Blanking Timing Version 2 (CVT-RBv2) which adds a small amount of overhead to the video signal. This overhead includes 80 additional horizontal pixels and enough additional lines to meet the 460 µs minimum vertical blanking interval requirement. You can download an Excel spreadsheet from VESA that will do all the calculations for you, but the math is pretty simple:

460 / ((1000000 / [refresh rate in Hz] - 460) / [vertical resolution])

To get an integer number of lines, you round this result down and add 1. So for Pro Display XDR resolution at 120 Hz that would work out to:

460 / (1000000 / 120 - 460) / 3384) = 197.710 = 198 additional lines

@kiding
kiding / NoScrollOnInputFocusiOSSafari.html
Last active May 16, 2025 07:40
Preventing iOS Safari scrolling when focusing on input elements
<!--
When an input element gets focused, iOS Safari tries to put it in the center by scrolling (and zooming.)
Zooming can be easily disabled using a meta tag, but the scrolling hasn't been quite easy.
The main quirk (I think) is that iOS Safari changes viewport when scrolling; i.e., toolbars shrink.
Since the viewport _should_ change, it thinks the input _will_ move, so it _should_ scroll, always.
Even times when it doesn't need to scroll—the input is fixed, all we need is the keyboard—
the window always scrolls _up and down_ resulting in some janky animation.
However, iOS Safari doesn't scroll when the input **has opacity of 0 or is completely clipped.**
@kiding
kiding / HDRGainMap.swift
Last active June 10, 2025 14:04
Extracting HDR Gain Map from iOS 14.1+ (iPhone 12+) photos
import UIKit
import MobileCoreServices.UTCoreTypes
if #available(iOS 14.1, *) {
let input = Bundle.main.url(forResource: "IMG_0037", withExtension: "HEIC")!
let output = FileManager().temporaryDirectory.appendingPathComponent("IMG_0037.GAIN_MAP.BMP")
let source = CGImageSourceCreateWithURL(input as CFURL, nil)!
// urn:com:apple:photo:2020:aux:hdrgainmap
let dataInfo = CGImageSourceCopyAuxiliaryDataInfoAtIndex(source, 0, kCGImageAuxiliaryDataTypeHDRGainMap)! as Dictionary
@devnoname120
devnoname120 / My macOS setup.md
Last active June 6, 2025 14:38
My macOS setup

ℹ️ Enable iCloud end-to-end encryption:

  • Nobody knows that, but iCloud actually supports end-to-end encryption (see “Advanced Data Protection for iCloud”) but it's disabled by default. If you care about data privacy, you should turn it on.
    • System settingsApple IDiCloud → Set Advanced Data Protection to On.
    • System settingsApple IDiCloud → Disable Access iCloud Data on the Web.
  • It supports almost all the native Apple apps, with the notable exceptions of iCloud Mail, Contacts, and Calendars. See official data protection matrix for more info.

Table of Contents