Automate your life with Typescript and ScriptKit
Integrate ScriptKit with other automation services and create an unforgetable experience
If you are developer, you would have that natural instinct to automate everything that comes along the way. Create a folder? automate. Open browser and Spotify when opening vs code? automate. But. Its not that easy and you know it. We have a couple of options in Mac like Shortcuts and Automator. However, these are UI driven. I know your programmer mind is screaming, “who wants an UI automation tool?”.
But I don’t like going all in on a Python script and running it every time either. Fair enough.
Enter ScriptKit… The very first time I saw ScriptKit and its capabilities, I was completely blown away. Its in this sweet spot between a complete scripting language and a full blown UI automator. It has endless possibilities on what you could automate with it. The cherry on top? Its in Typescript. Its amazing.
ScriptKit itself is based on Google’s zx which is a cli executable for javascript files.
In this article, I will be showing 2 examples which are both cool and helpful to get started in ScriptKit.
Before that,
Here is a very simple hello world script:
// Name: Display HTML
import "@johnlindquist/kit"
await div(`<h1>Hello World</h1>`)
Example 1
Open favourite sites in a click
Opening your favourite sites should be a single one click process and must not involve several steps. Here is a way to achieve it:
// Name: Open Favorite Sites
import "@johnlindquist/kit"
try {
let path = await arg("Type a site to open", [
{
name: "Youtube Music",
description: "Opens YT music in a browser window",
value: "https://music.youtube.com",
},
{
name: "Pocket",
description: "Opens Pocket in a browser window",
value: "https://getpocket.com/saves",
},
{
name: "Archivorous",
description: "Opens Archivorous in a browser window",
value: "https://archivorous.com",
},
])
$`open ${path}`
} catch (e) {
log(e)
}
The script is simple, I define a bunch of sites and open the one I selected using the zx $
command.
If you don’t believe me about ScriptKit, install and give this script a try with your own favourite sites. You will fall in love ❤️
Example 2
Are you a fan of smart home devices? You will love this one. Let’s use ScriptKit to automatically turn on/off the smart lights.
One of the best things about ScriptKit is its ability to make HTTP request. Although controlling smart devices is not natively possible in ScriptKit, we can utilise the ever amazing IFTTT to achieve this.
But first, there are a couple of prerequisites:
- You must have IFTTT setup without your smart device provider ( for example: Philips). The webhook provides complete control over the device. Careful with it.
- You must have a webhook IFTTT action created like this:
Now you can use the script below:
I am using IFTTT free version and so I cannot parse the incoming json in the webhook for dynamically controlling the lights. (But if you do have a pro version, you can use it to write javascript in IFTTT console and dynamically control which light is triggered in a single webook).
But for my purposes, I have created 6 webhooks and integrated them with ScriptKit:
// Name: turn on or off smart lights
import "@johnlindquist/kit"
try {
let selectedLight = await arg("Type a site to open", [
{
name: "Playroom on",
description: "Turn on the playroom light",
value: "playroom_lights_on",
},
{
name: "Playroom off",
description: "Turn off the playroom light",
value: "playroom_lights_off",
},
{
name: "Bedroom on",
description: "Turn on the bedroom light",
value: "bedroom_lights_on",
},
{
name: "Bedroom off",
description: "Turn off the bedroom light",
value: "bedroom_lights_off",
},
{
name: "Living room on",
description: "Turn on the living light",
value: "livingroom_lights_on",
},
{
name: "Living room off",
description: "Turn off the living light",
value: "livingroom_lights_off",
},
])
await post(`https://maker.ifttt.com/trigger/${selectedLight}/json/with/key/<your key>`)
} catch (e) {
log(e)
}
Now if you invoke the script, voila!
The light turns on like magic. No voice commands, no mobile app, just from your computer without even taking your hands off the keyboard.
Hopefully, these examples are inspiring enough to explore ScriptKit. There is also a silent hero: IFTTT which performed all the heavy lifting for us.