Skip to content

Instantly share code, notes, and snippets.

Avatar
🦆
🦆 🦆 🦆 🦆

Henrik Nyh henrik

🦆
🦆 🦆 🦆 🦆
View GitHub Profile
View daring_fireball.rb
require "open-uri"
r = -> { URI(_1).read }
post_dates = r.("https://daringfireball.net/archive/")
.scan(%r{<small>(.+?)</small>})
.map { |(x)| Date.parse(x.gsub("&nbsp;", " ")) }
link_dates = r.("https://daringfireball.net/linked/")
.scan(%r{href="(.+?/linked/20\d\d/.+?)"})
@henrik
henrik / attr_extras_using_blocks_proof_of_concept.rb
Last active May 4, 2022
Proof of concept for a version of https://github.com/barsoom/attr_extras 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 = block.parameters.map(&:last)
arg_names.each do
attr_reader _1
private _1
end
define_method(:__attr_extras_init_block, &block)
@henrik
henrik / export_modified_photos_for_screensaver.scpt
Created Mar 19, 2022
Export modified (not original) photos from Photos.app 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:
-- https://discussions.apple.com/thread/7881820
--
-- 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 <https://henrik.nyh.se> 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
henrik / how_to_restore_a_downloaded_mongodb_com_backup_dump.md
Last active Mar 2, 2022
How to restore a downloaded MongoDB.com backup dump
View how_to_restore_a_downloaded_mongodb_com_backup_dump.md

We had a downloaded MongoDB dump that we wanted to restore on cloud.MongoDB.com. 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
henrik / honda_e_wallpaper.scpt
Last active Oct 8, 2021
Pixelmator Pro AppleScript to export Honda e wallpaper images. Details: https://www.hondaeforums.com/viewtopic.php?f=8&t=255&p=11087#p11087
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"
activate
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
henrik / photos_app_filename_as_name.scpt
Created Jun 22, 2021
Photos.app: 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
henrik / photos_app_zeropad.scpt
Last active Jun 22, 2021
Photos.app: 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
henrik / README.md
Created May 7, 2021
Export Plex library titles via Node
View README.md

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
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 https://github.com/grosser/parallel.
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 self.call(inputs, pool_size:, &block)
queue = Queue.new
inputs.each { queue << _1 }
pool_size.times.map {
@henrik
henrik / README.md
Created Jan 1, 2021
IRCC codes for Sony Bravia KD55XH9296BU. For my own reference for future automation.
View README.md