Skip to content

Instantly share code, notes, and snippets.

🦆 🦆 🦆 🦆

Henrik Nyh henrik

🦆 🦆 🦆 🦆
View GitHub Profile
henrik / example.jxa
Created Feb 5, 2020
JXA script (JavaScript AppleScript) to set titles ("names") from filenames, but changing the first letter to uppercase.
View example.jxa
var Photos = Application("Photos")
var currentSelection = Photos.selection()
for (var item of currentSelection) {
const oldFilename = item.filename()
const newFilename = oldFilename.charAt(0).toUpperCase() + oldFilename.slice(1) = newFilename
View factory_bot_extensions.rb
# Do this in a factory to get this method:
# require "factory_bot_extensions"
# We should just load it once somewhere, but have yet to figure out where because FactoryBot is loaded in some fancy way.
# Example usage:
# factory :item do
# transient do
# company {
# if passed_in?(:contract)
henrik / sureflap.rb
Last active Dec 16, 2019
SureFlap API Ruby example.
View sureflap.rb
# SureFlap API code example.
# By Henrik Nyh <> 2019-12-16 under the MIT license.
# Heavily based on the PHP code by Alex Toft.
# Has no dependencies outside the Ruby standard library (uses Net::HTTP directly and painfully).
require "net/http"
require "json"
require "pp"
henrik /
Last active Apr 22, 2020
Automatically launch Cuphead (from Steam) on a Mac when a Nimbus Bluetooth game controller is connected.

The only game I play on the Mac Mini connected to our TV is Cuphead.

I wanted the game to launch automatically when I turn on one of our Nimbus game controllers, which connect via Bluetooth.

This is how:

  • Save the .scpt file below as e.g. ~/Library/Scripts/LaunchCupheadIfControllerIsConnected.scpt
    • In the .scpt, change "Nimbus" if your Bluetooth game controllers have some other name.
  • Save the .plist below as e.g. ~/Library/LaunchAgents/henrik.launch_cuphead_if_controller_is_connected.plist
    • In the .plist, change media to whatever your username is.
henrik /
Created Aug 15, 2019
Wetherspoon Bubblegin recipe

Written down for the benefit of googlers, since I've tried to figure it out to make it at home.

This is my best guess from overhearing things and glancing behind the bar:

  • One part Zymurgorium "Realm of the Unicorn" gin
  • One part Funkin Pro bubblegum syrup
  • Add lemonade
  • Add ice and strawberries cut in half

I've tried the Imaginaria gin and I've tried the Monin bubblegum syrup, but they don't taste the same.

henrik / ignore_exceptions_for_a_while.rb
Last active Jun 10, 2020
Ruby/Redis code to ignore exceptions unless they've consistently happened for the past X amount of time. Useful e.g. in recurring background workers: And outside workers, too: Goes well with
View ignore_exceptions_for_a_while.rb
class IgnoreExceptionsForAWhile
REDIS_KEY = "ignore_exceptions_for_a_while"
# This exception should be ignored in our exception logger (e.g. Honeybadger) config, so devs won't be notified about it.
# This can be used to let a UI show some user-facing error without notifying devs until after the `raise_after` timeout.
# Either by just relying on e.g. Ajax request error callbacks to show a helpful error message, or by rescuing this error and showing a message.
# You can call `#cause` on this exception to get the original exception.
henrik / spanish_organization_number.rb
Created Jun 27, 2019
Validate Spanish NIFs ("CIFs") in Ruby.
View spanish_organization_number.rb
# By "organization number" we mean an organization NIF (Spanish tax identification number). We don't mean personal NIFs such as DNI/NIE (see `SpanishIdentityNumber` for that).
# This class also supports personal NIFs starting with the letters K, L or M, though it's inaccurate to call these "organization numbers". More about these:
# It's easier to support them than not to, and means that any such users can provide this number, even though it's represented a bit inaccurately in our system. They're likely to be very rare. Feel free to think up a way to represent it more accurately. (Renaming `spanish_organization_number` to something like `non_dni_or_nie_spanish_tax_identification_number`?)
class SpanishOrganizationNumber
# NIFs do not have the letter prefixes "I" and "O" since those can be confused for the digits "1" and "0".
# Don't know why "T" is not a p
henrik / 1-exhaustive_case_exact_match.rb
Last active Apr 15, 2019
Experimental Ruby exhaustive conditionals that raise if no branch matches. A bit like in e.g. Elm.
View 1-exhaustive_case_exact_match.rb
# This replaces conditions like: `case …; when …; when …; else raise "Nothing matched!"` if an exact match is all you need.
def cond(input, hash)
hash.fetch(input) { raise "No branch for #{input.inspect}!" }.call
input = :good
good: -> { puts "So good!" },
bad: -> { puts "So bad!" },
View gist:42ed93465e7d6299de54aef8636f2a84
<!DOCTYPE html>
<script type="text/javascript" src=""></script>
<script type="text/javascript">
henrik / tz_identifiers_to_rails_identifiers.rb
Created Aug 23, 2018 — forked from jpmckinney/tz_identifiers_to_rails_identifiers.rb
Maps tz database time zone identifiers to Rails time zone identifiers
View tz_identifiers_to_rails_identifiers.rb
# blog post:
"Australia/Adelaide" => "Adelaide",
"Australia/Broken_Hill" => "Adelaide",
"America/Anchorage" => "Alaska",
"America/Juneau" => "Alaska",
"America/Nome" => "Alaska",
"America/Yakutat" => "Alaska",
"Pacific/Gambier" => "Alaska",
"Asia/Almaty" => "Almaty",