10 scripting commands for Apple admins that get you off to a strong start
During our Scripting 101 for Apple Admins webinar in late June, I asked attendees to share their comfort level with scripting. On a scale from one to five (where one was “scripting terrifies me” and five was “I write my own scripts”), about half rated their comfort as either three or four.
For everyone who’s used scripts, edited others’ scripts for your own needs or written your own; you’re doing great — keep learning and keep improving your scripting skills! For those of you interested in scripting who said “scripting terrifies me,” this blog post is for you.
Watch the webinar
The hardest part of scripting is getting started. That’s what I want to convince you to do.
If you haven’t yet, go watch the Scripting 101 for Apple Admins webinar. It’s about an hour long, and you can eat your lunch at your desk while you soak it in. It’s not written to teach you how to script, but rather to show you how scripting works.
You don’t need to know how an engine works to begin learning how to drive a car, but it gives you confidence and a better understanding of what’s happening if you do. You could learn that one pedal speeds you up and the other slows you down, but knowing one pedal sends more gas to the engine to speed you up and the other pedal squeezes your wheels to slow you down gives you a better mental picture of what you’re actually doing.
So, I encourage you to watch the on-demand webinar. See how the engine works.
During the webinar, I said that with just 10 commands, you can take your administrator effectiveness from a two to an 11. That’s because much of what you want to do with scripts is what you’re already doing in the Finder on your Mac. Scripting isn’t about doing something you can only do in the command line. It’s about doing what you’re already doing in the Finder … but doing it in the command line instead.
You want to read, create or delete files. You want to change system settings. You want to open applications or URLs. Or you want to display a dialog to your end users. (OK, that last one is kind of special, but it’s not hard to do.)
So, I’ll give you your first 10 commands to try out. Open the Terminal application found in the Applications > Utilities folder and copy and paste each command below. They’re all just one line — and for some, I give you a few examples to try. Press the return key after each line.
Experiment. Change them up. Put in your own names or phrases.
Scripting for Apple Admins
Repeat or “echo” what I type.
echo 'Hello, World!'
Literally, say what I type. Talk to me! (Turn up the volume for this one.)
say 'Hello, World!' say -v Victoria 'Are you going to eat that sandwich?'
Open a file or open a folder as if you’d double-clicked it. Or open a URL as if you’d entered its address into a web browser:
open ~/Library/Preferences open /Applications/TextEdit.app open https://www.jamf.com/ open /System/Library/PreferencePanes/PrintAndScan.prefPane
a calendar in Terminal.
cal 2019 cal july 2019
Erase the Terminal window and start over at the top.
Display how many minutes, hours or days your computer has been running without a restart.
Create a new folder or “make directory” at the path specified.
mkdir ~/Desktop/NewFolder mkdir -p ~/Desktop/First/Second/Third/Fourth
Short for “superuser do” — run a command as the “superuser” or “root.” This requires that you’re currently logged in as an administrator.
Get or set a “system configuration” setting. In this case, the current computer’s name. Running the second command requires using “sudo.” Check your computer name in System Preferences > Sharing after running this one.
scutil --get ComputerName sudo scutil --set ComputerName "Deep Thought"
Get the current version of macOS
sw_vers sw_vers -productVersion
Run an AppleScript command from Terminal or the command line. AppleScript is another scripting language on your Mac. Bash cannot display a dialog, but AppleScript can. We can use one scripting language to call another scripting language and take advantage of its unique capabilities like displaying a message to an end user.
osascript -e 'display dialog "Hello, World!"'
These few commands won’t do everything you’ll want to do in your scripts, but I bet you’ll find a few of them intriguing. For example, the following script uses both the osascript command to prompt for a new computer name and then changes the name with the scutil command. The echo command toward the end reports on what just happened. Copy these four lines to a new plain text file (avoid using TextEdit, if possible) named “renameComputer.bash” on your desktop. Drag the file into Terminal and press the return key. If prompted to enter your administrator password, go ahead. All you’re doing is renaming your computer. You can always change it again.
#!/bin/bash newName=$( osascript -e 'text returned of (display dialog "Enter a new name for your Mac..." default answer "" with title "Name Your Computer" with icon file posix file "/System/Library/CoreServices/Finder.app/Contents/Resources/Finder.icns")' ) scutil --set ComputerName "$newName" echo "Setting ComputerName to $newName" exit 0
Now what? Where do I go?
Glad you asked.
First, challenge yourself. The best way to learn scripting is to first learn commands you can use in Terminal. Practically everything you can do in the Finder you can do in the command line.
As you work on your Mac day-to-day, try to do one or two things that you’d normally do in the Finder in the Terminal instead. Open a System Preference pane. Create a new folder. Change a setting such as displaying file extensions for all files. If you’re hand-configuring new computers, try to find the commands to do the same thing in Terminal. Google is the novice’s friend. It’s also the expert’s friend. This is the best way to learn.
Regardless of whether you’re new to scripting, a novice scripter or have spent years scripting, one of the best ways to learn new things is to see what others are doing and how they’re doing them.
Jamf Training Catalog
Jamf Subscription customers (including Jamf Cloud) have free access to our online Training Catalog with nearly 15 hours of video tutorial content about all things Jamf. Check out our Scripting Series with 15 modules for learning how to script. All K-12 and higher education customers have access to the Training Catalog through the end of September 2019, regardless of whether you’re a subscription customer.
Jamf Nation Discussions
Edit your Jamf Nation profile and subscribe to the daily or hourly email digests. You’ll receive an email with all the new posts from Jamf Nation discussions. Many of those posts are about scripting. Browse those messages and see if there’s something you can use, too. There’s absolutely nothing wrong with taking an entire script or part of a script and using it for your needs. This is the very reason everyone is sharing.
Jamf Nation Scripts
How does Jamf write scripts? Visit our scripts area on Jamf Nation for more than 100 Jamf and community-contributed scripts you can download and use in Jamf Pro. You’ll find examples for setting time zones, disabling guest access, updating Microsoft Office for Mac, renaming the “Macintosh HD” and configuring DNS servers.
Updated Extension Attributes
The scripts in Jamf Pro’s Extension Attributes Templates need occasional tweaking and updating to ensure they work with newer versions of macOS. You’ll find the latest of these on Jamf’s GitHub site. The full extension attributes are stored as XML files. Just download the files and then upload them to the Settings > Computer Management > Extension Attributes area of your Jamf Pro server.
Computer Information Script
I’ll plug a blog post from last year about building a computer information script for Self Service. This is a modular script you can modify to display useful information to your end user for reporting back to a Help Desk or Desktop Support technician. Modifying this script (or using it as-is) exposes you to many useful commands.
Lynda.com through your public library
If you haven’t heard of Lynda.com, you’ll find it’s a gold mine of video training and tutorials for IT administrators. It has a few bash scripting courses available and a lot of general scripting courses too. Don’t let the dates of the courses turn you away. Bash is 30 years old and hasn’t really changed in much of that time. Unlike macOS, what was relevant 10 years ago is still just as relevant today. You’ll find hours of training and learning resources at your disposal.
If you’re unable to afford the subscription price, your organization may already subscribe as part of ongoing training for you. Or if you’re a public library card holder, your local library may offer access to Lynda.com for free as part of your library membership. This is a great way to take advantage of the tax dollars you’ve already spent. Here are just a few links I’ve found already:
- Saint Paul Public Libraries
- Minneapolis Public Libraries
- Grand Prairie, TX, Public Libraries
- Las Vegas – Clark County
- Los Angeles Public Library
- New York Public Library
- Oklahoma Metro Public Library
- Orlando Public Library
Another online resource is O’Reilly’s Safari Books Online. Similar to Lynda, your organization may already subscribe to this service and make it available to you. Or your higher-ed institution may make it available. They have both video tutorials and books for bash.
Half Price Books and other discount book stores
I personally love a good physical book when trying to learn something new. It helps me to have it open to the side of my computer as I go through the exercises. Across the United States we have Half Price Books, a chain of 120 stores that buys and sells used books at a great discount from their retail prices.
You may find bash, scripting and similar “coding” books that are a few years old, but again, don’t let that deter you from picking up one that looks interesting. Little has changed in the past couple of decades with regards to the basics of scripting.
Other areas of the U.S. and the world likely have similar stores.
If you like books but prefer eBooks instead, keep your eyes open for eBook bundle deals from online distributors like Humble Bundle. This site specializes in bundling electronic content such as games and DRM-free eBooks into downloadable bundles where customers choose the prices they’re willing to pay. The proceeds often go to charity. Occasionally, the bundles include deals on computer books.
Join the discussion
One webinar video or a blog post isn’t nearly enough to cover all things scripting. I’ve started a discussion to answer questions you may have or that we couldn’t get to during the webinar. Please join me on Jamf Nation and I’ll try to respond to you. I encourage anyone who has additional training resources to post there too, especially for areas of the world outside the United States where the resources above may not be available.
And finally, if you can’t find the answer, then ask. The Mac admin and Jamf communities are sharing communities. They’re big, but they’re made small because of our interconnectedness and willingness to help each other.