EDDYMENS

Published a month ago

MacOS AppleScript Tutorial: Part 4 – Advanced Automation And Real-World Examples

Table of contents

In Part 3 [→], we explored loops, conditional statements, and error handling in AppleScript. Now, in this final part, we will dive into more advanced automation techniques and see how you can apply AppleScript in real-world scenarios. We'll cover:

  • Working with lists and arrays
  • Interacting with multiple apps
  • Creating workflows to automate daily tasks

Working with Lists (Arrays) in AppleScript

AppleScript can handle lists, which are collections of items (like files or numbers). Let’s see how we can use lists to perform more complex automations.

Example: Listing All Files on Your Desktop

  1. Type the following in Script Editor:

    01: tell application "Finder" 02: set fileList to name of every file of desktop 03: set fileNames to "" 04: repeat with fileName in fileList 05: set fileNames to fileNames & fileName & return 06: end repeat 07: display dialog "Files on desktop: " & return & fileNames 08: end tell
  2. Click Run.

This script lists all the file names on your desktop and displays them in a dialog.

Breaking Down the Script

  • set fileList to name of every file of desktop gets a list of all file names on your desktop.
  • The repeat loop goes through each file and stores its name in a variable called fileNames.

Automating Tasks Across Multiple Apps

AppleScript can control multiple apps in the same script, which is incredibly useful for automating workflows.

Example: Copying Text from Safari to a Text Document

  1. Type this script:

    01: tell application "Safari" 02: set myURL to URL of front document 03: end tell 04: 05: tell application "TextEdit" 06: activate 07: make new document 08: set text of front document to "Saved URL: " & myURL 09: end tell
  2. Click Run.

This script grabs the URL of the current Safari tab and automatically creates a new document in TextEdit with the URL saved in it.

Breaking Down the Script

  • set myURL to URL of front document captures the URL of the current tab in Safari.
  • tell application "TextEdit" switches to TextEdit, creates a new document, and pastes the URL into it.

Creating a Full Workflow

Let’s create a simple workflow to automate your morning routine: open Safari, navigate to a news website, and open your email app.

  1. Type this script:

    01: tell application "Safari" 02: open location "https://news.google.com" 03: activate 04: end tell 05: 06: tell application "Mail" 07: activate 08: end tell
  2. Click Run.

This script will open Google News in Safari and launch your Mail app.

Scheduling AppleScripts with Automator

You can use Automator to schedule your AppleScripts to run at specific times. For example, you can run your morning routine script every day at 9 AM.

  1. Open Automator (search for it with Spotlight).
  2. Choose Calendar Alarm.
  3. Drag the Run AppleScript action from the library into the workflow.
  4. Paste your script into the Run AppleScript action.
  5. Save the workflow and set the time in the Calendar.

Conclusion

In Part 4, we explored more advanced AppleScript techniques:

  • Handling lists of items (like files).
  • Automating tasks across multiple apps.
  • Building a workflow to automate everyday tasks.
  • Scheduling AppleScripts with Automator.

In future tutorials, we will explore specific tasks you can automate using the ideas we’ve covered in the different parts. Until then, take care ✌️!

Here is another article you might like 😊 MacOS AppleScript Tutorial: Part 3 - Loops, Conditional Statements, And Error Handling