This is the second part of a twelve-day series on my new year’s resolutions to become a better Mac admin. During these twelve days my book “Packaging for Apple Administrators” is on sale! (Previous Post: “Read a Book”)
Knowing how to write scripts and code is very helpful for an administrator. Even basic scripting skills will go a long way.
macOS provides has a plethora of scripting and programming languages available. Even if you already know one language, adding another to your tool box can be very helpful. Different languages have different strengths and weaknesses. Also, sometimes just looking at a problem through a different view point, i.e. trying to solve it with a different language can be very insightful.
I believe these languages are most important for an Apple administrator:
- shell scripting, bash
- Swift, Objective-C, Cocoa
Shell Scripting, bash
Since macOS is a UNIX, there is a shell to execute Unix commands and scripts. On macOS the default shell is
bash has been around for a long time. You can write very complex and powerful scripts. However the syntax is often quite arcane and there are many pitfalls for the unexperienced scripter.
bash on macOS has one major advantage over Python in that
bash is included in the minimal OS used on the Recovery Partition or installer systems (for NetInstall). So if you want to use scripts there, you can’t use Python, but bash will work. Since custom packages can be installed at this stage, scripts included in packages should also be shell.
- “Classic Shell Scripting” by Arnold Robbins and Nelson H. F. Beebe, iBooks, Amazon US UK DE
- “Learning the bash Shell: Unix Shell Programming” by Cameron Newham, iBooks, Amazon US UK DE
- “bash Cookbook: Solutions and Examples for bash Users” by Carl Albing, JP Vossen, Cameron Newham, iBooks, Amazon US UK DE
- also, check out the
bashsection on free programming books
Python is very popular among Apple administrators. Its learning curve is not as steep as
bash or Perl. It’s syntax is much less cryptic, even though Python is odd about whitespace. (I think that mandatory indenting is a good idea, but Python can be very picky about what it considers ‘good’ indenting.)
While Python is comparatively easy to learn, you can build complex and powerful tools with it. Munki, AutoPkg and Reposado are some tools for admins written in Python. Tim Sutton has a very thorough list of open source tools for admins in Python.
When you learn Python, note that the Python version included with macOS is 2.7, which is distinctly different from Python 3. It’s not that you can’t switch from one Python to the other if you’ve learnt the ‘wrong’ version. However, you will have to download and install Python 3 seperately on macOS. (It can co-exist with Python 2.7.)
- Dive into Python
- Python section on Free Programming Books
- “Learning Python” by Fabrizion Romano (free eBook)
- “Learning Python: Powerful Object-Oriented Programming” (O’Reilly) by Mark Lutz, iBooks, Amazon US UK DE
Swift, Objective-C, Cocoa
Swift is a very exciting, comparatively new programming language. Apple started the project but has since made it open source. Apple clearly plans Swift to be the successor for Objective-C. So far most of the macOS and iOS frameworks (collectively known as Cocoa or Cocoa touch) were native in Objective-C. They still are, but Apple is transitioning them to also have a Swift interface.
So with Swift you get native access to the macOS frameworks, but can also use it cross-platform, on servers or in command line scripts. You could also use the same language to write UI macOS or iOS applications. This makes Swift very interesting for the Apple administrator.
As a downside, since the language is new, it has changed drastically from version to version (it is now on version 3.0.1). However, the fast pace of the initial releases is expected to slow down somewhat now.
- Apple’s Swift Playgrounds for iPad: very basic, but entertaining entry level coding
- The Swift Programming Language (Apple), free iBook
- “Swift Programming: The Big Nerd Ranch Guide” by Matthew Mathias, John Gallagher, Amazon US UK DE
- “Hacking with Swift” by Paul Hudson, free online or buy on iBooks
AppleScript is certainly an awkward language to write with. But there are still some things, especially when it comes to inter-application communication, where AppleScript is the fastest, easiest and sometimes the only way to go.