First off, regex is shorthand for a regular expression. And what, exactly, is a regular expression? According to the linked Wikipedia page, a regular expression is…
…in theoretical computer science and formal language theory, a sequence of characters that define a search pattern. Usually this pattern is then used by string searching algorithms for “find” or “find and replace” operations on strings.
That’s a mouthful, but what it means is that you can write some really bizarre looking code that will transform text from one form to another form. And if you know just a bit of regex, and where to go to look up what you don’t know, then you can use regex to do many useful things.
For example, consider this filename on a scanned-to-PDF receipt:
The Party Place [party supplies] - 02-06-2017
Perhaps you’d prefer it if the date came first, in year-month-day order, so that your receipts were ordered by date, like this:
2017-02-06 - The Party Place [party supplies]
Sure, you could manually rename this one file, but what if you have 500 receipts that you need to rename? Enter regular expressions—they’ll let you do this text manipulation, and many more. What follows is a very brief summary of my knowledge of regex, along with pointers to sites where I go when (very often) the problem I need to solve is beyond my regex skill level.
Over the weekend, I was testing how some of our apps work when the CPUs are busy. One way to load the CPUs is to rip a Blu-ray disc, but I was looking for a more controllable CPU load.
A quick search through the Mac OS X Hints archive (use this tip to search the site) found the answer from 15 years ago: Just say yes in Terminal to generate sizable CPU loads…
More specifically, use this command in Terminal:
yes > /dev/null &
If there’s an award for strangest Unix utility, yes might just be the winner. All it does is output y (or whatever you list after the y; the man page suggests an expletive) until you kill the task.
The above command sends the output (via the > redirect) to the null device, which discards it. The ampersand sends the job to the background, so you get your Terminal prompt back.
You can run this command multiple times, each loading the CPU even more heavily (the screenshot shows three yes tasks running). Keep an eye on Activity Monitor to see just how much CPU it takes—as shown above, it does a great job at loading the CPU.
You can kill the tasks by issuing the command killall yes in Terminal, or by quitting Terminal—you’ll be told that quitting will terminate the tasks.
Fair warning: Today’s tip is over-the-top geeky.
After many years of not doing anything with web serving on my local Mac, I recently had a need to look at some mySQL/PHP-based web packages. While I could install these on robservatory as tests, I generally like to install things locally first, because I’ll install a handful of packages until I find the one I like. But it’d been so long, I didn’t have anything configured. (Remember when enabling Apache—i.e. web sharing—was a feature of the System Preferences’ Sharing panel? Ah, good times!)
I could have opted to use the built-in Apache, but I wanted something that I could more easily keep up to date, and that, if I chose, would be easy to remove. The good news is that Homebrew has packages available for Apache, PHP, and MySQL. Homebrew installs everything in its own directory tree, and adding and removing packages is simple—exactly the setup I wanted.
So in theory, installation was as easy as three brew tap commands. The reality, though, is that the installation is a bit more complex. OK, it’s a lot more complex. The good news is that, this being the age of the internet, help is but a search away. Or the advice of a friend away, which is what I used in this case.
A friend pointed me to this excellent installion guide that walks through the entire process, including installing two versions of PHP with the ability to switch between them on the fly.
It was this on-the-fly switching, though, that gave me troubles: I couldn’t get a site to load unless I specified “index.php” or “index.html” as part of the URL. (Apache is configured to grab those files automatically.) Solving that one took a bit of time…
Really long-time Unix users—as in mainframe-based Unix—are probably familiar with fortune. This silly little program grabs a random line from a collection of files holding quotes, sayings, jokes, etc. The Unix I used many decades ago would print an entry from fortune each time you started a new session. Here are some examples of what might greet me each time…
"It's a dog-eat-dog world out there, and I'm wearing Milkbone underware."
-- Norm, from _Cheers_
Mobius strippers never show you their back side.
All constants are variables.
Years ago, I had set up my Mac’s Terminal to output a fortune each time I opened a new session (window). At some point, though, I forgot to set it up on a new system, so it was gone. While fortune isn’t included in macOS’ Unix core by default, there are many ways to get it back, and it’s relatively simple to do so. Here’s one way…
I recently explained how I captured a series of screenshots and turned them into a movie. While I was working on my tweet about the write-up, I thought an animated GIF of the final movie would be a nice way to show what it was I was trying to do. So that’s what I wound up doing:
So how did I create the animated GIF from the movie file? I know there are any number of great apps that will do this (ScreenFlow, for one), but I had another thought: While working on the animated screenshot movie (which I created using ffmpeg), I had happened to read about ffmpeg’s ability to create high quality animated GIFs, so I thought I’d give that a try.
While working on my massive Blu-ray ripping comparison, I wanted more information about what some of the transcode-video presets were doing. That is, if you pick --target big, exactly what settings are being used to rip the video?
It turns out there’s --dry-run option for transcode-video that will tell you exactly that. (I’ve added some line breaks for readability here.)
$ transcode-video --dry-run --target big --mp4 Miss_Peregrine\'s_Home.mkv
--output=Miss_Peregrine\'s_Home.mp4 --markers --encoder=x264
--crop=0:0:0:0 --strict-anamorphic --rate=30 --pfr --encoder-preset=medium
--encoder-profile=high --encoder-level=4.0 --quality=1 --audio=1,1
What’s neat is that you can also use this to see what the default options are for transcode-video when you don’t supply it with any options. Just use the --dry-run parameter option but leave off any of the presets (i.e. --target big), and the output will show you the defaults.
In addition, you can use it on already-ripped media to get their details as well, regardless as to how you ripped the movie.
In a related vein, I was having issues with the above rip, because I thought that the surround sound track wasn’t being ripped. Again, thanks to Don, I learned about a second command line option for transcode-video that reveals exactly what’s in a ripped video.
As I’ve written about in the past, I use Don Melton’s video transcoding tools to rip Blu-Ray discs. I also use Homebrew to install some of the transcode video dependencies, as well as other Unix tools.
Keeping these tools current isn’t overly difficult; it only requires a few commands in Terminal:
$ brew update
$ brew upgrade
$ sudo gem update video_transcoding
My problem is that I often forget to do this, because—unlike most GUI Mac apps and the Mac App Store—there’s no built-in “hey, there’s an update!” system. Suddenly, two months and many revisions later, I finally remember (usually when I see a tweet about a new version of something.) So I thought I’d try to write my own simple update reminder.
I didn’t really want a scheduled task, like a launchd agent—it’s not like these tools need to stay current on a daily basis. (And one of them needs to run with admin privileges, which complicates things.) I just wanted something that would remind me if it’d been a while since I last checked for updates, and then install the updates if I wanted it to do so.
After mulling it over, I came up with a script that runs each time I open a Terminal window (which I do daily). The referenced script looks at the date on a check file, and asks me if I’d like to check for updates if that date is more than a week older than today’s date. This is perfect for my needs: The reminder is automatic, but I can choose when to install the updates based on what I’m doing at the time. If it’s been under a week since I last checked, nothing at all is different about my Terminal launch.
Read on for the script and implementation details. (Note: This is not written for a Terminal beginner, as it assumes some knowledge about how the shell works in macOS.)
Quite often, I find myself with a number of images (screenshots, typically) that I’ll want to convert from one format to another. If you search the Mac App Store, there are probably 300 apps that will let you do this; many are probably free. You could also use Automator, which has some good image conversion abilities, but can’t (for example) specify the quality of a JPEG conversion.
But the best way I’ve ever found is to use a tool that’s been included with every copy of macOS since the release of Mac OS X 10.3 (Panther) in October of 2003: A command line tool called sips. Yes, it requires using Terminal, but it’s quite easy to use. sips can modify one file, or any number of files, converting from one format to another. You can also use sips to resize images, rotate images, and more.
Basic usage of sips is straightforward. (The following is written for Terminal neophytes, so apologies for any over-explaining). Assume you have an image named Beach party.tiff that you’d like to convert into a smaller JPEG, but with a relatively high quality setting. Here’s how you’d do it using sips:
- Open Terminal, in Applications > Utilities.
- Type cd, then press the Space Bar, then drag in the folder that contains the image(s) to be converted. (Alternatively, you can use this tip to directly open the selected Finder folder in Terminal.)
- Type this, then press Return: sips -s format jpeg -s formatOptions 80 "Beach party.tiff" --out "Beach party.jpg"
When you press Return, sips will convert your image file—and it’s really fast, even on larger files. The formatOptions item lets you set the quality of the JPEG in either percentage (as I used), or you can use words: low, normal, high, or best. Hopefully obviously, you specify the new filename after the --out string.
Note that the filename is enclosed in quotes. Those quotes are required, otherwise any spaces in your filenames will cause the command to break.
The real power of sips isn’t in converting one file, though; it’s in batch converting many files. Here’s how to do that…