EDDYMENS

Published a month ago

MacOS AppleScript Tutorial: Part 1 - Getting Started With AppleScript

Table of contents

What is AppleScript?

AppleScript is a scripting language created by Apple. It allows you to automate tasks on macOS by controlling your computer and interacting with apps like Safari, Finder, or Mail. For example, you can tell your computer to open an app, click a button, or even send an email automatically.

In this tutorial, we'll walk through the basics of AppleScript so you can start writing your first simple scripts.

Why Learn AppleScript?

AppleScript is useful because:

  1. Automating tasks: Save time by automating repetitive tasks, like renaming files or organizing folders.
  2. Controlling apps: You can tell apps to do things automatically.
  3. Simplicity: It’s written in plain English, so it’s relatively easy to understand.

How to Open Script Editor

To write AppleScript, you’ll need to use the Script Editor, which comes with macOS.

  1. Press Command + Space to open Spotlight.
  2. Type Script Editor and hit Enter.
  3. You should now see a window with a space to write scripts.

Writing Your First AppleScript

Let's start with a simple script to make your computer talk. This is a fun way to see AppleScript in action!

  1. In the Script Editor, type the following:

    01: say "Hello, world!"
  2. Click the Run button (it looks like a play button at the top of the window).

Your computer should say "Hello, world!" out loud.

Breaking Down the Script

  • say is a command that makes your Mac speak.
  • "Hello, world!" is the text you want it to say. You can change the text to whatever you want!

Controlling Finder with AppleScript

Finder is the app you use to browse your files. You can control Finder using AppleScript. For example, you can make a new folder on your desktop:

  1. In the Script Editor, type:

    01: tell application "Finder" 02: make new folder at desktop with properties {name:"My New Folder"} 03: end tell
  2. Click Run.

This script tells Finder to create a folder called "My New Folder" on your desktop.

Breaking Down the Script

  • tell application "Finder" tells the Finder app that you want to give it commands.
  • make new folder at desktop creates a folder on your desktop.
  • {name:"My New Folder"} gives the folder a name.

Running AppleScripts from Anywhere

Instead of opening Script Editor each time, you can save your script as an app or add it to your menu bar for quick access.

  1. Click File > Export in Script Editor.
  2. Select Application as the file format.
  3. Give it a name and save it somewhere on your Mac.

Now, whenever you run that file, the script will execute automatically.

Conclusion

You’ve just written your first AppleScripts! You learned how to:

  • Make your computer speak.
  • Create a new folder on your desktop using Finder.

In the next part of the tutorial, we’ll dive deeper into more useful commands and how to interact with other apps on your Mac.

Feel free to play around with the examples and change things. In AppleScript, trial and error is a great way to learn!

Here is another article you might like 😊 Periodically Run GitHub Actions: A Guide To Scheduled Workflows