Scripting
Focus can be as simple or as complex as you want it to be when comes to blocking unwanted distractions.
If you’re in the latter camp, you can use basic code to do a TON of cool things!
There are two primary methods for scripting within Focus.
Focus Scripting Method #1: BASH Scripting
NOTE: Using custom BASH scripts in Focus requires a basic level of familiarity with the language.
_If you want a comprehensive tutorial on how to read and use BASH scripts, please read this tutorial_
To access the BASH scripting panel, click the Focus menubar icon, click Preferences and then click on the Scripting tab.
By default, Focus comes with examples of how to properly type custom BASH scripts.
The sky's the limit when it comes to deciding what you want to happen: You can start/stop other programs, start playlists or even enable Do Not Disturb mode.
Depending on the state of Focus, you can use environment variables within the script that are specific to Focus:
Environment Variable | Description |
FOCUS_ACTIVE=0 or 1 | Is Focus active or not? |
FOCUS_HARDCORE_MODE=0 or 1 | Is Focus in hardcore mode or not? |
FOCUS_UNBLOCK_DATE=date | Date when will Focus expire? (format like 2015-06-20 20:56:59 +0000) |
FOCUS_INTERVAL=12345 | If a timer was set, how long was it set for? |
FOCUS_ACTUAL_INTERVAL=12345 | Length of actual Focus session. Used in Unfocus |
FOCUS_TYPE=normal or timer or schedule | What kind of Focus session is this? |
NOTE: Here is a very basic example of how you would integrate environment variables within the script you have in the Scripting panel…
ENVVAR=1234
ANOTHER=56789
For more information on how to use environment variables within BASH scripting (both inside and outside of Focus), please read this tutorial.
You can also have these scripts run depending on the current behaviour of Focus.
Click on “starts” and you’ll see a drop-down menu with the following four options:
Option | Result |
Starts | The script will be executed when Focus is activated |
Stops | The script will be executed when Focus is no longer activated |
Breaks | The script will be executed when Focus is on a break |
Unbreaks | The script will be executed when the pre-programmed break in Focus is over. |
EXPORTING A BASH SCRIPT OUT OF FOCUS
If you want to save your custom BASH script in Focus for use at a later time, you can export it and keep a copy stored on your computer.
Simply access the Scripting tab and click on the “gear” icon:
Select the “Export script” option. You will then be able to choose the folder on your computer where you want your script to be exported:
Once you have selected your folder, click the “Open” button and you will see the following screen:
Your custom BASH script will be available in the form of a .txt file, and it will appear like this when you open it:
IMPORTING A BASH SCRIPT INTO FOCUS
Want to restore an old BASH script, or even import a new script you’ve been working on? No sweat - Focus has you covered!
Simply access the Scripting tab and click on the “gear” icon.
Click on the “Import script” option:
NOTE: As mentioned in the default BASH script, it is imperative to distinguish the difference between a command and a comment.
Any line beginning with a “#” is a comment. If you remove the “#” character, Focus will read the line as a command to run.
Please see the final visual in the “Focus Scripting Method #1: BASH Scripting” section on this page to see how your .txt file should look like.
You will then have the option to select the .txt file containing your BASH script:
Open the file, and you will instantly see your BASH script appear in Focus.
RESTORING THE DEFAULT BASH SCRIPT PROVIDED BY FOCUS
Perhaps you just want to start off from a blank slate and not have Focus run the script currently in the Scripting window.
Easy peasy, lemon-squeezy!
To restore the default BASH script provided by Focus, simply access the Scripting tab and click on the “gear” icon.
Click on the “Reset to defaults” option:
You will see this notification pop up immediately:
Focus will remove whatever BASH script is already there, replacing it with the default script.
NOTE: This CANNOT be undone, so make sure you’re good to go before pressing the “OK” button!
Focus Scripting Method #1: URL HANDLERS
Fortunately, you do not have to be in the Focus app to take advantage of its Scripting feature.
Focus can be scripted from Terminal, Alfred or your web browser.
For instance, you can input the following scripts into your browser and toggle Focus in different ways:
Focus URL | Description | |
focus://focus | Start Focusing | |
focus://focus?minutes=30 | Start Focusing for 30 minutes | |
focus://break?minutes=5 | Start breaking for 5 minutes | |
focus://unfocus | Stop Focusing | |
focus://unbreak | Stop breaking | |
focus://toggle | Toggle Focusing | |
focus://preferences | Open preferences | |
focus://debug | Show debug information |
If you’re eager to try this out right now in your default web browser, do the following:
Focus - activates Focus
Unfocus - terminates a working session within Focus
If you want the easiest way to use URL Handlers, you can launch a new Focus session from Terminal.
Open up a new BASH sheet by opening the Terminal app (/Applications/Utilities/Terminal.app).
Once the app is open, type:
Open focus://focus
For more information on URL Handlers and advanced ways to use this feature, please read this blog post.