Script applications

Send a small Apple event

Write and run a minimal AppleScript command against Finder before combining application scripting with shell automation.

An Apple event is a structured message one process sends to another. Give me this property. Perform this command. AppleScript is the language most people use to send them. Every tell application block compiles into Apple events under the hood.

Start with a read-only request. Ask Finder for the path of the front window:

tell application "Finder"
  POSIX path of (target of front window as alias)
end tell

Run it in Script Editor. With a Finder window open on Downloads, the result pane shows:

"/Users/flavio/Downloads/"

The permission prompt

The first run does something else too. macOS asks whether Script Editor may control Finder.

That’s a TCC permission prompt. TCC is the macOS privacy system, and it asks once per pair of controlling app and target app. Approve it and the grant appears under System Settings → Privacy & Security → Automation.

Deny it and every future event to Finder fails with error -1743, “Not authorized to send Apple events”. It stays that way until you flip the switch in System Settings. Remember that error number. It comes back in the next lesson.

Break it on purpose

Now close every Finder window and run the script again. It errors, because there is no front window to ask about.

This is normal. Your script made an assumption, and the assumption was false. The fix is an explicit fallback:

tell application "Finder"
  if (count of windows) is 0 then return POSIX path of (desktop as alias)
  POSIX path of (target of front window as alias)
end tell

With no windows open, the script now returns the Desktop path instead of an error.

AppleScript errors are part of the contract, not noise to ignore. The scripts that survive are the ones that decide in advance what happens when an assumption turns out false.

Keep events small

While you learn an app, send one property read at a time. Check one result.

A tell block that chains five operations tells you nothing useful when it fails in the middle. Which of the five broke? You don’t know. You have to take it apart anyway.

Once each event is verified on its own, combining them later is assembly, not archaeology. That’s what we do in the next lesson, where the shell calls this exact script.

Try this: pick the property you found in the previous lesson and read it in a tell block. Then make the assumption behind it false, watch the error, and add the fallback.

Lesson completed