Quick Fix: Mount Encrypted APFS Drive with AppleScript

automatically unlock an APFS volume with AppleScript

To supplement last week’s post on automatically mounting an external drive to create a clone, here is a quick tip for doing the same thing with an encrypted APFS volume. Ideally, you should be encrypting your backups. If you’re running macOS 10.13 High Sierra, or the impending macOS Mojave, then you will be cloning your system to an APFS volume. If that’s the case, you’ll need to no how to automatically unlock APFS volume with AppleScript.

Automatically unlock APFS volume with AppleScript

There is a little more work involved here, but none of it difficult. The file system might be new, but diskutil is still the command line program doing all the work managing volumes. There is simply a couple more commands involved. This assumes you have already encrypted the drive with Disk Utility.

To mount, or rather unlock an encrypted APFS volume with AppleScript, we need the following information:

  • APFS volume ID
  • Cryptographic user ID
  • The encryption password

The password is the same one you used when you formatted the drive. Here is how to get the other two pieces of the puzzle.

  1. Find the APFS volume ID for your clone drive. You can see this information clearly in Disk Utility. For every volume listed there is a table of information, the device field has what you are looking for. It is some variation of disk1s1. Or if you prefer, with the drive already mounted you can run a terminal command to have the information of all your drives listed, like so:

diskutil apfs list

That command will take a moment, then print a whole lot of information to screen like below. Look for volume you intend to clone your system to and note down the APFS Volume Disk.

Automatically Unlock Apfs Volume With Applescript
You can find the APFS volume ID in a couple of places, if you know where to look
  1. Once you have the volume ID.  In the terminal run the following command (replace ‘apfs_volume_id’ with your disk)

diskutil apfs listcryptousers /dev/apfs_volume_id

You will get something that looks like this:

+-- B4BA200D-B0B7-4AB2-A48C-BDE9FFA7E3BA
	Type: Disk User
	Hint: 1pw

That long alphanumeric code is the Cryptographic user. Copy that code and you have everything you need to make your AppleScript work.

  1. Create the AppleScript to automatically mount your encrypted APFS volume. The script looks like this:
do shell script "diskutil apfs unlockVolume [name_of_your_drive] -user B4BA200D-B0B7-4AB2-A48C-BDE9FFA7E3BA -passphrase [enter your passphrase here]" 

Naturally, you will enter the name of your drive, and replace the user code with the one you copied above. Make sure you remove the square brackets.

  1. Find a way to launch the script when you need it. There are a bunch of options in my previous post. My preferred option is currently Keyboard Maestro, but an Automator Calendar Alarm, or Lingon X work just as well.

Congratulations, you can automatically unlock an APFS volume with AppleScript.

 

Photo by Patrick Lindenberg on Unsplash

Enable Safari Hidden Features with Debug Menu

I found Safari Browser to be a nightmare in its cross platform days. It says a lot for its progress that has become my preferred browser. The modern version is fast, efficient with resources, and proactive about tracking protection. Recent announcements also suggest that protection will continue to improve.  Using 1Blocker for iOS and macOS , I can manage my browsing experience across Mac and iOS without weighing the app down with extensions. Add to that recent additions such as iOS type privacy settings, and the already excellent continuity features like handoff, and reading list. Put simply, using Safari is easy.  It might not be as extensible as Chrome or Firefox, but like those browsers, Safari has a number of hidden features.

Enabling Safari Hidden Features

Finding Safari hidden features requires enabling a couple of menus that are disable by default. The easiest one to enable is the Develop menu. To do that, open preferences, advanced, and simply tick the box: ‘Show Develop menu in menu bar'. Restart Safari and it will appear, giving you new options to do things like disable Javascript, clear caches, or change the user agent among other things.

Safari Browser Hidden Features
Enable the Develop menu in the advanced preferences

The other hidden menu is the lessor known Debug menu, which requires some basic terminal foo to reveal. The intrepid and curious will have a range of new preferences to tweak

  • Open Terminal: If you’re a mouse jockey click on the Go menu in Finder, and select Utilities. If you’re a keyboard warrior hit ⌘ + Space and start typing Terminal.
  • Once you have the Terminal open either type, or copy and past the following command:

defaults write com.apple.Safari IncludeInternalDebugMenu 1

  • Press return, then restart Safari. Presto, you have a new menu.

Safari Hidden Features to Enable

Macos Safari Browser Hidden Features
From the debug menu users can disable inline video altogether

Until recently, this was the only way you could disable autoplay video on annoying sites. Thankfully, Safari will now allow you to set site specific preferences , content blocking, and so on. It doesn’t always work the way it should in my experience, but setting a global flag in the debug menu takes care of it. Under Media Flags, enable ‘Video Needs User Action’, or ‘Audio Needs User Action’, depending on your needs. You can also disable inline video altogether.

Other handy features include the ability to disable some of Safari’s energy management. If you have attention madness like I do, you might find your open tabs getting out of control. Rather than creating epic memory leaks, Safari will suspend background tabs that aren’t being used. The browser is smart about how it does this, but it doesn’t suit everybody. For some users, having to reload a suspended tab can be a real nuisance. For instance, if you do a lot of research you might want to keep all your tabs live. If this is you, the option to disable background tab suspension is under miscellaneous flags.

There are a lot more flags, some more useful than others. To state the obvious, you can break stuff by playing with them, but that’s half the fun.

Quick Fix: Disable Circle Jerking and Autoplay in Chrome

Disable Autoplay Chrome Browser

I use Safari by default, but I’m a still bit of a browser hopper at times. The stark reality for anyone who runs a website — whether a bonafide developer or a hack like me — is Chrome Browser accounts for around 60% of all web traffic 1. I have made the mistake of optimising this site for Safari, based on a ropey sense of potential reader preference. If you’re a chrome user reading this, I assure you I have disabused myself of that notion. 2 Given Google all but owns the web, knowing your way around Chrome is non-negotiable.

Chrome Flags

In many ways Chrome is drastically more functional than Safari. For extensibility, it leaves Safari in the dust. If there is one thing I have always appreciated about Chrome, 3it’s the ability to dig into the experimental features. To do similar things with Safari requires invoking a terminal incantation, using the Safari Technology Preview version, or both. It is a little on the janky side.

If you’re ever troubleshooting chrome, it is worth looking at the flags to see if you can’t put paid to unwanted behaviour. There are some useful features buried in there. If you don't know what chrome flags are, or you're wondering what they can do, here are a couple I set recently.

Disable Circle-Jerking

This is an old trick that douche-bags use to trap users on a site. Loading history entries with the current URL means when users hit the back button it simply reloads the same page. Chrome has a handy little flag to disable this behaviour.

  1. Type Chrome://flags into the address bar
  2. ⌘+F and search for ‘History’
  3. Set the ‘New history entries require a user gesture’ to ‘Enabled’

Disable Autoplay

There are two flags I like to set for this. One for Autoplay, and the second to automatically mute any tabs that somehow escape this rule.

  1. Same as above, type Chrome://flags into the address bar
  2. ⌘+F and search for ‘Autoplay Policy’
  3. Set to ‘user gesture is required for cross origin iframes’
  4. ⌘+F and search for ‘Tab audio muting UI control’
  5. Set to Enable

Get back to browsing, and enjoy.

  1. depending where you get your stats
  2. For anyone who cares, until recently part of the caching system used by my web host Cloudways was intermittently broken on Chrome.
  3. And Firefox, for that matter.