Write your first AppleScript!

Philip Young

AppleScript is a scripting language made by Apple. It gives you superpower and helps you automate part of your workflow.

If you can't code, this quick tutorial will help you understand how easy it is to get started.

Otherwise, you probably only need to turn on and off Do Not Disturb when you start a session & stop working. If that's the case, you can learn it from Integrate automatic Do Not Disturb with Session on Mac.

1. Your first AppleScript code

  1. First, open Script Editor app. You can do it by:
  2. Open Finder > Applications > Utilities > Script Editor, or
  3. you can press CMD + Space on your Keyboard and type "Script Editor" Script editor search
  4. Create a new document
  5. On Script Editor, type:
1display dialog "I'm shown!"

applescript

  1. Press the play icon Script editor run button location

  2. A dialog will be shown Dialog shown

    Congratulations. You've just wrote your first code!

2. Open and close App

Slack gif

Opening and closing app is very easy. Let's use Slack as an example.

  1. To open Slack, write:
1tell application "Slack" to activate

applescript

  1. or you can close the app by calling:
1tell application "Slack" to quit

applescript

Slack is a good example of app to be closed with Session automation. You can force close it when you start your focus with start_session, and reopen it again on stop_working.

3. Spotify example

TBD

4. Combining multiple AppleScript

The best thing with AppleScript is you can combine all of them into single file.

Say on session_start you want to:

  • quit Slack
  • quit Mail
  • turn on Do Not Disturb
  • Display a dialog that your Session has started

This is the AppleScript code

1tell application "Slack" to quit
2
3tell application "Mail" to quit
4
5set output to (do shell script "defaults read com.apple.controlcenter 'NSStatusItem Visible DoNotDisturb'")
6if output is "0" then
7	tell application "System Events" to keystroke "D" using {command down, shift down, option down, control down}
8	do shell script "defaults write com.apple.controlcenter 'NSStatusItem Visible DoNotDisturb' 1"
9end if
10
11display dialog "Session Started!"

applescript

Do Not Disturb section is extremely complex. Don't be discouraged. You can learn to set it up from here.

5. Running your AppleScript on Session

You have wrote your code. Now what?

Integrate it with Session automation! You can put your code inside Session's AppleScript folder. Look into Setup your AppleScript with Session to learn what's possible.

AppleScript on iOS?

Sadly AppleScript is not available on iOS. but it's sibling—Apple shortcut—exists. And Session also support Apple shortcut hook 😎! In the future, it's possible to block iOS apps if, say, your 3 hour focus daily is not reached yet.

Tutorial coming soon 😊.

Conclusion

The possibilities are endless. If you can't code, now you can.

If you want to start Spotify, try to Google "How to play Spotify album with AppleScript". Internet will tell you how. Otherwise, keep looking into Session's website from time to time. We'll write a new tutorial on productivity, time management, automation for you.

That's it for now. Be more mindful, and keep stay focused!

© Copyright 2020-2024 Translucent LLC