Skip to content

Instantly share code, notes, and snippets.

View simonbs's full-sized avatar
🐼

Simon B. Støvring simonbs

🐼
View GitHub Profile
@simonbs
simonbs / Create Gist.js
Last active May 6, 2024 23:07
Scriptable script for creating a gist.
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: brown; icon-glyph: file-code;
// To use this script, you need to configure an OAuth App on GitHub.
// Follow the instructions on the link below to create your OAuth App.
//
// When you are asked to put in a redirect URL, you should put the URL for running this script in Scriptable. Assuming the name of this script is "Create Gist", the URL is scriptable:///run?scriptName=Create%20Gist
//
// https://developer.github.com/apps/building-oauth-apps/creating-an-oauth-app/
//
@simonbs
simonbs / UserDefault.swift
Last active March 3, 2024 11:38
Property wrapper that stores values in UserDefaults and works with SwiftUI and Combine.
/**
* I needed a property wrapper that fulfilled the following four requirements:
*
* 1. Values are stored in UserDefaults.
* 2. Properties using the property wrapper can be used with SwiftUI.
* 3. The property wrapper exposes a Publisher to be used with Combine.
* 4. The publisher is only called when the value is updated and not
* when_any_ value stored in UserDefaults is updated.
*
* First I tried using SwiftUI's builtin @AppStorage property wrapper
@simonbs
simonbs / One More Thing.js
Last active February 17, 2024 11:04
Countdown to Apple's "One More Thing" event on November 10th, 2020
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-purple; icon-glyph: apple-alt;
const TITLE = "One More Thing"
const DATE = "2020-11-10T17:00:00Z"
const IMG_URL = "https://i.ibb.co/f2SN2Wb/bg.png"
let widget = await createWidget()
if (config.runsInWidget) {
Script.setWidget(widget)
@simonbs
simonbs / ToolbarController.swift
Created July 23, 2021 06:22
Enable/disable a toolbar item in a Catalyst app.
final class ToolbarController, NSObject, NSToolbarDelegate {
var isBackButtonEnabled = false {
didSet {
if isBackButtonEnabled != oldValue {
reloadBackItem()
}
}
}
func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
@simonbs
simonbs / Proxy.swift
Created November 4, 2023 19:09
Property wrapper for proxying values.
@propertyWrapper
struct Proxy<EnclosingType, Value> {
typealias ValueKeyPath = ReferenceWritableKeyPath<EnclosingType, Value>
typealias SelfKeyPath = ReferenceWritableKeyPath<EnclosingType, Self>
static subscript(
_enclosingInstance instance: EnclosingType,
wrapped wrappedKeyPath: ValueKeyPath,
storage storageKeyPath: SelfKeyPath
) -> Value {
@simonbs
simonbs / Append to File.js
Created August 27, 2019 20:31
Appends text to a file. To be used with Shortcuts.
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-blue; icon-glyph: file-signature;
let text = args.shortcutParameter
let fm = FileManager.iCloud()
let filePath = args.fileURLs[0]
let content = fm.readString(filePath)
let newText = content + "\n" + text
fm.writeString(filePath, newText)
Script.complete()
@simonbs
simonbs / childForStatusBarStyle.swift
Last active September 25, 2023 14:49
It seems that -childForStatusBarStyle: isn’t called on a UIViewController that is presented from a SwiftUI view using UIViewControllerRepresentable. Or am I doing something wrong? I came up with this *ugly* workaround that swizzles -childForStatusBarStyle: to return an associated object when present and uses the default implementation as fallback.
// We'll store a UIViewController as an associated object and don't want to store a strong reference to it.
private final class WeakBoxedValue<T: AnyObject>: NSObject {
private(set) weak var value: T?
init(_ value: T?) {
self.value = value
}
}
// Use associated objects to a UIViewController that should determine the status bar appearance.
@simonbs
simonbs / Find Nearby Scooter.js
Last active August 17, 2023 17:56
Scriptable script for finding nearby scooterers from the Lime and Bird rental companies.
// You must generate and insert your own API key for Google Maps + Directions JS SDK.
// The script uses Google Maps to display the scooters on a map as well as the route to the nearest scooter. Follow the guide on the link below to obtain an API key.
// https://developers.google.com/maps/documentation/javascript/get-api-key
let googleApiKey = ""
// Choose the enabled rental companies. Set to true to enable the rental company and false to disable it.
let COMPANIES = {
bird: true,
lime: true
}
@simonbs
simonbs / Import Shortcut Links.js
Created September 21, 2019 11:37
Reads a text file containing iCloud links to shortcuts and imports the shortcuts into the Shortcuts app
@simonbs
simonbs / docc.sh
Last active May 2, 2023 08:04
Generates documentation with Apple's DocC for a target that supports both iOS and macOS.
#!/bin/bash
#######################################################################
# Generates documentation for a target, for example a Swift package,
# that supports both iOS and macOS.
#
# The script is designed to be placed in the root of a Swift package.
# Change the paths as needed if your project structure is different.
#
# In order for DocC to annotate all symbols with the platforms