TextExpander: Mac Version Details

Occasionally, I have a need to know the precise version of the OS of the Mac I’m using. This is particularly true when I get involved with beta testing software for developers. Whilst I can get the information manually each time, this was something that was ripe for automation. To that end I put together a shell script-based TextExpander snippet to output the information for me.

The key components I pull in are the name of the operating system, the major and minor versions of the operating system, the codename of the operating system, and the build number of the operating system. While those first three I could usually have a go at jotting down for either of the two Macs I have that I might be using, the build number is one that changes every month or two and isn’t one I’d so easily recall.

On the command line, sw_vers allows us to get the product version (e.g. 10.13.6), and the build number (e.g. 17G7024), which in the script below are stored directly, and respectively, in the variables pver and bver. What ‘sw_vers’ doesn’t give us is the name, or the code name, of the operating system.

For the name and the codename I’m using the trick of parsing the license agreement file to get the details. Because the name and the code name could each be one or two words (currently/historically), I’ve opted to use a check versus known OS names (‘macOS’ and ‘OS X’), to identify the name, and then from that, the codename can be derived.

With the licence file, I use awk to grab the pertinent line, and then sed to trim away the redundant content of the line. This gets stored in the lic variable. A grep instance count of lic is used to identify the name of the operating system which is then stored in the name variable. Subsequently, a sed based substitution name with a null in lic provides the codename, which is unsurprisingly stored in the variable codename.

The final output is then put together through an echo of the variables with a bit of additional formatting, and it displays as something like macOS 10.13.6, High Sierra (17G7024).

To create the snippet that outputs this, create a new TextExpander snippet with a Content type of Shell Script, and then copy and paste the script below directly into the body of the snippet. After that just set your label, abbreviation and case sensitivity to your personal preference.

pver=$(sw_vers -productVersion)
bver=$(sw_vers -buildVersion)
lic=$(awk '/SOFTWARE LICENSE AGREEMENT FOR/' '/System/Library/CoreServices/Setup Assistant.app/Contents/Resources/en.lproj/OSXSoftwareLicense.rtf' | sed 's/\\f0\\b SOFTWARE LICENSE AGREEMENT FOR //')

if [ `echo $lic | grep -oc 'macOS'` -eq 1 ]
elif [ `echo $lic | grep -oc 'OS X'` -eq 1 ]
	name='OS X'

codename=$(echo $lic | sed "s/$name //")

echo $name $pver, $codename '('$bver')'

I haven’t tried this on all of the possible variations, but hopefully I’ve covered most if not all the variations with this approach.

Author: Stephen Millard
Tags: | textexpander | scripting |

Buy me a coffeeBuy me a coffee

Related posts that you may also like to read