Skip to content

Instantly share code, notes, and snippets.

@tempelmann
tempelmann / create mail.applescript
Created February 15, 2021 14:08
Create a sample email with attachment in AppleScript
tell application "Mail"
set newMessage to make new outgoing message with properties {subject:"theSubject", content:"theBody"}
set theFile to (POSIX file "/etc/hosts") as alias
tell newMessage
make new attachment with properties {file name:theFile} at after the last word of the last paragraph
end tell
end tell
@tempelmann
tempelmann / recycleURLs_crash_test.m
Last active February 7, 2021 03:14
Demonstrates crashing bug in [NSWorkspace recycleURLs:completionHandler:] when trashing over 500 files at once
//
// recycleURLs_crash_test
//
// Created by Thomas Tempelmann on 01Feb21.
//
// Purpose:
// Demonstrates that using [NSWorkspace recycleURLs:] on > 600 files often leads to a crash on macOS 10.12 and 10.13.
// In my testing (on 10.13.6), up to 500 items never crash, while it starts crashing sometimes with 650 items
// and nearly every time when it's over 800. I also get spurious errors on the first recycle interation when
// using 600 items. This could be a precursor to the crash, but not sure.
@tempelmann
tempelmann / create_files_with_unicode_composition_differences.sh
Created December 3, 2020 15:43
Demonstrates an issue in SMB + macOS when having files with decomposed characters on a shared Linux volume
#!/bin/bash
#
# This script demonstrates an issue with SMB (and probably AFP as well)
# sharing, when accessing files by macOS that are hosted on a Linux share.
#
# Run this script on a Linux system to create two file in the current dir.
# Make sure it runs in bash, not sh, or the codes won't work.
#
# Make the generated files acessible via SMB, then try to access both files
# from macOS. The one called "decomposed_ü" won't be accessible - that's the bug.
@tempelmann
tempelmann / ipredator-nat.ovpn
Last active September 22, 2020 09:58
IPredador Tunnelblick config (NAT)
# VER: 0.28
client
dev tun0
proto udp
remote nat.openvpn.ipredator.se 1194
remote nat.openvpn.ipredator.me 1194
remote nat.openvpn.ipredator.es 1194
#remote 46.246.46.100 1194
#remote 46.246.46.101 1194
#remote 46.246.46.102 1194
@tempelmann
tempelmann / main.m
Last active September 15, 2020 16:23
Demonstrates a bug in Big Sur (b6) with APFS volumes having non-ASCII names. To reproduce, rename any APFS volume so that it contains an "ü" in it.
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
NSArray<NSURL*> *vols = [NSFileManager.defaultManager mountedVolumeURLsIncludingResourceValuesForKeys:nil options:0];
for (NSURL *url1 in vols) {
NSURL *url2 = [NSURL fileURLWithPath:url1.path];
if (! [url1 isEqual:url2]) {
NSLog(@"url mismatch: %@ - %@", url1, url2);
if (! [url1.path isEqualToString:url2.path]) {
NSLog(@"path mismatch: %@ - %@", url1.path, url2.path);
@tempelmann
tempelmann / AppDelegate.m
Last active October 7, 2020 17:22
Determine APFS Volume Group relationships in macOS Catalina and Big Sur
//
// Sample code for determining all invididual mounted volumes, including volume groups.
// Tested on macOS 10.13.6, 10.15.6 and 10.16 (11.0) b6
//
// See https://stackoverflow.com/questions/63876549/
//
// Created by Thomas Tempelmann on 14 Sep 20 to 17 Sep 20 (yes, this took me several days to get right).
//
#import <IOKit/IOKitLib.h>
// See https://twitter.com/tempelorg/status/1305559310760644612?s=20
// Turns out this is correct behavior.
// No more need for testing.
#include <stdio.h>
#include <sys/mount.h>
int main(int argc, const char * argv[]) {
struct statfs fsinfo;
statfs("/", &fsinfo);
@tempelmann
tempelmann / trans.m
Last active August 24, 2020 00:51
Provides a smart LocalizedString() function (short: "LS") that gives you more control over where you pick the translations from
// Written 2019 by Thomas Tempelmann, [email protected]
// Tested on macOS, but may work on iOS as well.
NSMutableSet *reportedMissingLocs = nil;
#define MyAppSupportFolderName "your.bundle.id" // or your app's name
static NSBundle *mainBundle = nil;
static NSArray<NSString*> *availableLangs; // list of langs the app knows (possibly sorted alphabetically)
static NSDictionary<NSString*,NSBundle*> *bundlePerLang = nil;
@tempelmann
tempelmann / AppDelegate_NSAlert.m
Last active March 8, 2020 14:15
A solution to the question "How can I make the cancel button react to both the Return the the Esc keys?"
// A solution to the question "How can I make the cancel button react to both the Return the the Esc keys?"
//
// See https://stackoverflow.com/a/60570407/43615
#import "AppDelegate.h"
@interface AlertEscHandler : NSView
@end
@implementation AlertEscHandler
@tempelmann
tempelmann / regex with normalization variations.c
Created April 28, 2019 16:44
Search unicode (UTF-8) text in any normalization form, case-insensitive
//
// Created by Thomas Tempelmann on 28.04.19.
// See also: https://stackoverflow.com/q/55884928/43615
//
// This code relies on string functions only available on macOS, sorry.
//
#include <Foundation/Foundation.h>
#include <string.h>
#include <stdlib.h>