Skip to content

Instantly share code, notes, and snippets.

View henrik's full-sized avatar

Henrik Nyh henrik

View GitHub Profile
@henrik
henrik / bigdecimal_extensions.rb
Last active August 3, 2022 13:55
BigDecimal.with_decimal_precision
class BigDecimal
# `BigDecimal(123.456, 2)` becomes 120.0. It keeps 2 *significant digits*.
# If you want two *decimals* of precision, use this method.
def self.with_decimal_precision(value, precision)
if value.is_a?(Float)
length_before_decimal = Math.log10(value.abs).to_i + 1 # log for performance.
BigDecimal(value, length_before_decimal + precision)
else
BigDecimal(value, precision)
end
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 15:43
Proof of concept for a version of https://github.com/barsoom/attr_extras that uses block arguments to get lazy-loaded defaults.
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 March 19, 2022 18:49
Export modified (not original) photos from Photos.app to a folder, to work around a screensaver bug.
-- 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 March 2, 2022 10:08
How to restore a downloaded MongoDB.com backup dump

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 October 8, 2021 20:08
Pixelmator Pro AppleScript to export Honda e wallpaper images. Details: https://www.hondaeforums.com/viewtopic.php?f=8&t=255&p=11087#p11087
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 June 22, 2021 19:21
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…
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 June 22, 2021 17:48
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.
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 23:33
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
henrik / each_in_thread_pool.rb
Last active February 2, 2021 16:55
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.
# 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 {