Skip to content

Instantly share code, notes, and snippets.

View drosenstark's full-sized avatar

Dan Rosenstark drosenstark

View GitHub Profile
@drosenstark
drosenstark / Make255Values-RolandNibbleEncoding.py
Last active April 9, 2024 23:34
Python Script Dumps a CSV to Disk with 255 Rows of "Roland Nibble Encoded" Values (see link in code)
#!/usr/bin/env python3
#
# Make255Values-RolandNibbleEncoding
# See https://mididesigner.com/qa/10040/roland-tr-8s-sysex-whats-what for what's what
import csv
# Convert decimal value to MSB and LSB hexadecimal values
def to_msb_lsb(value):
msb = value // 16
@drosenstark
drosenstark / FixChapGTP.css
Last active November 11, 2023 18:38
Simple CSS for Stylus etc. to widen ChatGPT
/* (c) Confusion Studios LLC and affiliates. Confidential and proprietary. */
/* from https://community.openai.com/t/customize-your-interface-for-chatgpt-web-custom-css-inside/315446/3 */
/* Allow width to expand for the main chat panel */
.gizmo .gizmo\:xl\:max-w-\[48rem\] {
max-width: 95%;
}
@drosenstark
drosenstark / obsidian-pico-publish.py
Created August 7, 2023 15:46
Obsidian Pico Publish, for Publishing Obsidian Notes
#!/usr/bin/env python3
import os
import json
import re
import fnmatch
from pathlib import Path
#####################
@drosenstark
drosenstark / URL+OpenInFilesApp.swift
Created August 3, 2023 15:16
Open the directory housing a document in the Files app
// thanks to this thing!
// https://www.macstories.net/ios/fs-bookmarks-a-shortcut-to-reopen-files-and-folders-directly-in-the-files-app/
extension URL {
func openParentDirectoryInFilesApp() {
guard var components = URLComponents(url: self.deletingLastPathComponent(), resolvingAgainstBaseURL: false) else { return }
components.scheme = "shareddocuments"
guard let newURL = components.url else { return }
@drosenstark
drosenstark / Publisher.swift
Last active May 15, 2023 19:06
Prompt: I want a Swift object that will sit on an object (let's call that object A) which will allow other objects (B, C, D) to subscribe to changes in A on a certain variable.
import Foundation
/*
* Prompt:
* I want a Swift object that will sit on an object (let's call that object A) which will allow other objects (B, C, D) to subscribe to changes in A on a certain variable.
*
* So the Swift class/object that you're making allows subscribers to provide a block to be executed based on the changes in that variable. The variable could be a Bool, but if you have to box it as an NSNumber, that's fine.
*
* Response:
* You can achieve this behavior in Swift using a closure (or block in Objective-C terminology) for notifying changes. Here's a simple example with a Publisher class that wraps an object and allows other classes to subscribe for changes:
@drosenstark
drosenstark / LockingScrollView.swift
Last active May 4, 2023 18:11
It's a UIScrollView with lockout for pan and zoom
// (c) Confusion Studios LLC and affiliates. Confidential and proprietary.
import UIKit
public class LockingScrollView: UIScrollView, UIScrollViewDelegate {
public var isPanningZoomingEnabled = false
init() {
super.init(frame: .zero)
delegate = self
struct TwoStateButton<Content: View>: View {
@State private var isPressed = false
let touchDownView: () -> Content
let touchUpView: () -> Content
let action: () -> ()
let touchDownAction: (() -> ())?
var body: some View {
Button(action: action, label: {
@drosenstark
drosenstark / MakeViewsInSwiftUI.swift
Created April 28, 2023 12:38
How to resolve "Use of protocol 'View' as a type must be written 'any View'" in some cases
// MARK: - Bad: "Use of protocol 'View' as a type must be written 'any View'"
struct LeftRightView: View {
let leftView: View
let rightView: View
var body: some View {
HStack {
leftView
rightView
@drosenstark
drosenstark / ViewController.swift
Last active April 26, 2023 19:49
UIKit holding SwiftUI Holding UIKit
// (c) Confusion Studios LLC and affiliates. Confidential and proprietary.
import SwiftUI
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
view.backgroundColor = .orange
let swiftUIView = ContentView().asUIView
@drosenstark
drosenstark / MultipartRequest.swift
Created December 25, 2022 15:45
MultipartRequest for Uploading images and forms to a server using URLSession in Swift
/// MultipartRequest by Dan Rosenstark
/// Thanks to tinyurl.com/2qpbyh7v: this code is just a remix
///
/// Steps to use
/// 1. init
/// 2. add form fields (if any)
/// 3. add file data (if any)
/// 4. grab the urlRequest and use it
struct MultipartRequest {
private let boundary = "Boundary-\(UUID().uuidString)"