pb – MacOS CLIĀ Trick

You can already interact with the system clipboard in MacOS directly from the command line with two commands: pbcopy and pbpaste. I’ve made it even easier for myself by detecting whether there is data in STDINĀ — for example if we’re sending data to the command via a pipe. If so, I’ve assumed we actually want to use pbpaste. Otherwise, we want to use pbcopy.

pb () {
	if [ -t 0 ]; then
		pbpaste
	else
		pbcopy
	fi
}

You can use it like pb < /path/to/fileĀ or pb > /path/to/other/file, for example.

CSS DarkĀ Mode

MacOS Mojave introduced Dark Mode, which allows MacOS users to enable a darker interface. According to Apple:

In Dark Mode, the system adopts a darker color palette for all windows, views, menus, and controls. The system also uses more vibrancy to make foreground content stand out against the darker backgrounds.

InĀ Safari Technology Preview 68, they’ve also added support for websites to add support for Dark Mode with a new media query:

@media (prefers-color-scheme: dark)

I’ve added support to my theme, which now looks like this in the latest Safari Technology Preview if you’ve got Dark Mode enabled:

Core Data &Ā Concurrency

It’s been a while since I last worked on any mobile apps, so I thought the last couple weeks of sabbatical would be a good time to get caught up on iOS and specifically to learn Swift, which didn’t exist last time ?. For the most part, it was pretty easy to pick up and I was able to move fairly quickly on some apps I had been thinking about.

One of the only problems I ran into was related to Core Data concurrency. Specifically, if two different threads are reading and writing data, you get errors like 'NSGenericException', reason: Collection <__NSArrayM: 0x7fabb400> was mutated while being enumerated.

The solution is private queue contexts. I won’t do a full explanation of queue contexts here, but the Apple Developer Documentation has some good information. The idea is to create a private context to operate on while we’re doing background work.

For this to work as expected there are a few things that need to happen:

  1. Set context.parent. In order for the changes to eventually be written to disk, we have to associate the new, private context with the main context by setting newContext.parent = oldContext.
  2. Use context.object(with: objectID) to make core data relationships. We need to get a reference in the current context to any objects that were created outside the context.
  3. After saving, we also need to save the parent context to commit the changes to disk. To make it thread safe, we use oldContext.perform or oldContext.performAndWait depending on whether is should be asynchronous or not.

I put together a gist to demonstrate:

https://gist.github.com/joshbetz/01d86cbfb2e04fd30df7ac92e8c7b4c5

The following articles were especially helpful to understand how to fix this problem in my case:

  1. Apple’s Core Data Programming Guide > Concurrency
  2. Core Data Concurrency & Maintaining a Silky Smooth UI

Eurotrip

This summer I took a sabbatical that included five weeks in Europe. We stayed in 10 cities in 8 countries and took some day trips that pushed the total number of cities to 12 or 13. Needless to say, it was a lot of travel. I don’t know if I would do something quite like it again, but would absolute recommend it if you have the chance.

I didn’t bring a laptop and basically just had my phone, camera, a few books, and my clothes. Here are some of the photos. 

Pi-hole

I’ve been running Pi-hole, the “black hole for Internet advertisements” for a while now. It started out as an excuse to get a Raspberry Pi, but I consider this a somewhat important security appliance now. One of the nice things about Pi-hole is, like Ghostery, it’s easy to see what’s being blocked. Unlike Ghostery, though, it works for the entire network. So things like the Xbox and smart TV are included. For example, I noticed that TCL TVs track what you’re watching even if you “Limit ad tracking”.

https://twitter.com/jshbz/status/1044042905172742144

I also run Unbound on the Raspberry Pi, which forwards to Cloudflare and OpenDNS over an encrypted connection. The Docker configuration I use for Unbound is on Github.