From Scripting to Musing

Samantha Wong
Government Digital Services, Singapore
4 min readJun 29, 2023

--

Dusting off a couple of cobwebs from the keyboard — you’ll excuse me, it’s been so long since I’ve last been here, I’ve forgotten how to start.

There’s no meaningful preamble or over-arching story here today — just wanted to share/jot down a cute little utility I found handy - AppleScript.

As the name suggests, AppleScript is a scripting language developed by Apple that dictates interactions on macOS. It can interface with the Finder application, Safari and other System applications.

This is handy if you want to automate certain tasks that a user might do on their Mac*; tasks such as opening a link on a certain browser, closing some windows, or clicking a certain button on a pop-up. Other user tasks might be making certain keystrokes, interacting with the clipboard, and getting system information.

For example, you could tell Safari to open a certain web page link:

osascript -e 'tell application "Safari" to open location "https://www.tech.gov.sg"'

Or create a new folder in your Desktop:

osascript -e ‘tell application “Finder” to make new folder at desktop with properties {name:”Life Is Interesting”}’

These commands aren’t terribly useful, especially considering you could easily replace them with other command line staples such as:

open -a Safari https://www.tech.gov.sg

And:

mkdir ~/Desktop/Life\ Is\ Interesting

that are more succinct, universally known and arguably more usable.

However, it so happens I had need of a scripting option that would help me assign a default browser to my System Preferences. You might think that this could be achieved by any library — but from my superficial research (I did this at 5am in the morning) — while there were libraries that could make some modifications to System Preferences, macOS ultimately still requires you to go through the paces of confirming and accepting the change via clicking on a pop-up. (I actually see this as a security feature.) That’s a user interaction that’s entirely within macOS. AppleScript provided a suitable repose to this requirement.

So if you download a command line utility like defaultbrowser or defbro, you could then use this accompanying AppleScript to deal with the pop-up:

Full text in default-browser.scpt as opened in macOS’s Script Editor.

From Terminal, you could then do:

osascript path/to/default-browser.scpt [browser-name]

After which your desired browser would be assigned as default.

Afterthoughts

In this way, AppleScript is to macOS as PowerShell is to Windows, and similar scripting can be extended to browsers (JavaScript) and mobile devices (adb for Android, XCTest on iOS?). What this means is I, or somebody else, could write something that can control how a machine behaves. (Basically, every computer program, Sam.) Or in this case, mimic a user action. A program pretending to be you. This is powerful stuff!

Whilst it may be used for good, e.g. automating some mundane processes and doing the heavy lifting for non-human-appropriate jobs, it can also be used by unsafe actors to log keystrokes, steal passwords or gain unauthorised access to applications and data on your machine.

In order for my osascripts to work; I had to allow certain Privacy and Security accesses on my System Preferences. Unscrupulous applications you might have downloaded from the internet or gotten from dubious sources will also request for similar elevated permissions. When you see system pop-ups asking you to confirm certain actions/decisions, it might be wise to read through them. These can serve as red flags and caution you against proceeding.

I’ll be the first to admit that they can also be overly active and annoying when you are rushing through a task. Another lesson: don’t rush through tasks.

If you notice your computer making an unusual amount of network calls, or starting to get bloated with files and/or running applications you did not authorise or download — you may have been hacked.

The internet and technology have opened entire vistas of possibility. Information has been democratised, data is more easily accessible. At the same time, every convenience brings about more attack vectors on privacy and data safety.

Knowing I can program my computer to adjust its default browser also tells me someone else could adjust its default browser, or worse.

Is there a silver bullet for any of this? Unfortunately, not that I know of. Applications can try to keep your data safe server-side and in-transit (meaning as data is passed from your computer to an application server via the internet). But you have to keep yourself safe on the client-side (meaning what you do on your own local computer).

Compartmentalise your life — do your important work in a cave. What they told us in primary school, be careful of the websites you visit and the links you click. Do we aways have to be this alert when on the internet? Yes.

Acknowledgements

I depended entirely on some excellent references which I list below, in particular Felix Paradis and his own reference to an Apple StackExchange exchange was my primary source.

The views expressed in this article are my own and do not necessarily reflect that of my employer’s.

References

--

--