Skip to content

Instantly share code, notes, and snippets.

🦆 🦆 🦆 🦆

Henrik Nyh henrik

🦆 🦆 🦆 🦆
View GitHub Profile
View daring_fireball.rb
require "open-uri"
r = -> { URI(_1).read }
post_dates = r.("")
.map { |(x)| Date.parse(x.gsub("&nbsp;", " ")) }
link_dates = r.("")
henrik / attr_extras_using_blocks_proof_of_concept.rb
Last active May 4, 2022
Proof of concept for a version of that uses block arguments to get lazy-loaded defaults.
View attr_extras_using_blocks_proof_of_concept.rb
module AttrExtras
def pattr_initialize(&block)
arg_names =
arg_names.each do
attr_reader _1
private _1
define_method(:__attr_extras_init_block, &block)
henrik / export_modified_photos_for_screensaver.scpt
Created Mar 19, 2022
Export modified (not original) photos from to a folder, to work around a screensaver bug.
View export_modified_photos_for_screensaver.scpt
-- Works around a bug where Photos screensavers show original rather than edited versions:
-- This script exports edited versions to a folder, which the screensaver can then be configured to use.
-- Run it on a schedule e.g. via crontab.
-- By Henrik Nyh <> under the MIT licence.
set albumName to "Skärmsläckare" -- Export from this album.
set exportFolder to POSIX file (do shell script "echo ~/Library/original_photos_for_screensaver") -- To this folder.
henrik /
Last active Mar 2, 2022
How to restore a downloaded backup dump

We had a downloaded MongoDB dump that we wanted to restore on This is how we did it:

  1. Download the backup via the MongoDB web UI.

  2. Unzip it. In this example we assume it unzipped to /tmp/foo.

  3. Have MongoDB installed locally, e.g. via:

     brew tap mongodb/brew
     brew install mongodb-community
  4. Start a local Mongo server:

henrik / honda_e_wallpaper.scpt
Last active Oct 8, 2021
Pixelmator Pro AppleScript to export Honda e wallpaper images. Details:
View honda_e_wallpaper.scpt
set currentTimestamp to do shell script "date +%s"
set exportFile to (path to desktop as text) & "honda_e_wallpaper_" & currentTimestamp & ".jpg"
tell application "Pixelmator Pro"
tell application "System Events" to tell process "Pixelmator Pro" to click menu item "Duplicate" of its menu of menu bar item "File" of menu bar 1
set sourceDoc to the front document
set targetDoc to make new document with properties {width:3840, height:720}
henrik / photos_app_filename_as_name.scpt
Created Jun 22, 2021 Set name to filename if name is missing. Since otherwise sorting by name will sort nameless photos first, but still show the filename as a grayed-out name…
View photos_app_filename_as_name.scpt
tell application "Photos"
repeat with x in every media item
if name of x is missing value then
set name of x to (filename of x)
end if
end repeat
end tell
henrik / photos_app_zeropad.scpt
Last active Jun 22, 2021 Rename albums to zeropad numbers. Use case: Relying on sorted events and migrating from iPhoto which sorted "100" after "2" to Photos which didn't.
View photos_app_zeropad.scpt
tell application "Photos"
repeat with theAlbum in albums of folder "Events"
set theName to name of theAlbum
-- Zeropad initial numbers, e.g. turning "12.3 Hi" into "0012.3 Hi".
set newName to (do shell script "ruby -e 'n = ARGV.first; num, rest = n.split(%{.}, 2); puts num.match?(/\\A\\d+\\z/) && rest ? [ num.rjust(4, %{0}), rest ].join(%{.}) : n' " & (quoted form of theName))
set name of theAlbum to newName
end repeat
end tell
henrik /
Created May 7, 2021
Export Plex library titles via Node

I wanted to export all titles from the Plex library on my Mac and couldn't find a good tool.

This is a tiny, quick-and-dirty Node JS script, using node-plex-api.

It assumes you have node and npm installed.

Then just download this "script.js" and run:

npm install plex-api --save

node script.js your_plex_username your_plex_password > plex.txt

henrik / each_in_thread_pool.rb
Last active Feb 2, 2021
Run a block on a list of things in a limited number of concurrent threads. Mostly for the fun of it – there are more featureful libs like
View each_in_thread_pool.rb
# Lets you call a block for each item in a list, just like `each`.
# But instead of running serially, it runs in a limited number of parallel threads.
# This is useful when you don't just want one thread per item, e.g. to avoid rate limiting or network saturation.
class EachInThreadPool
def, pool_size:, &block)
queue =
inputs.each { queue << _1 } {
henrik /
Created Jan 1, 2021
IRCC codes for Sony Bravia KD55XH9296BU. For my own reference for future automation.