API
Intro
Welcome to Script Kit! 👋
Script Kit provides an opinionated set of global APIs meant to streamline the process of writing scripts. Many of them (such as prompts) are meant to interact with the app, but there are also many common APIs for working with files, etc, that are simply built-in node or third-party libraries exposed as globals.
You do not need to use any of these APIs. You are free to write your scripts and add whatever npm packages you like.
Also, you can import kit
and access the APIs like so:
import kit from "@johnlindquist/kit"
await kit.arg("Enter your name")
If you have questions, please reach out on our Script Kit GitHub Discussions
Happy Scripting! ❤️ - John Lindquist
Playground
Press cmd+p
while browsing an API to generate a script where you can experiment with examples contained in that section. Go ahead and try it now to experiment with the example below:
await arg("Welcome to the playground!")
Prompts
arg
- Accept text input from the user.
- Optionally provide a list of choices filtered by the text input.
- Optionally provide a list of actions to trigger when the user presses a shortcut.
Details
- The first argument is a string or a prompt configuration object.
- The second argument is a list of choices, a string to render, or a function that returns choices or a string to render.
arg Hello World
let value = await arg()
A Basic String Input
let name = await arg("Enter your name")
arg with Choices Array
let name = await arg("Select a name", [
"John",
"Mindy",
"Joy",
])
arg with Async Choices
let name = await arg("Select a name", async () => {
let response = await get("https://swapi.dev/api/people/");
return response?.data?.results.map((p) => p.name);
})
arg with Async Choices Object
let person = await arg("Select a person", async () => {
let response = await get("https://swapi.dev/api/people/");
// return an array of objects with "name", "value", and "description" properties
return response?.data?.results.map((person) => {
return {
name: person.name,
description: person.url,
value: person
}
});
})
arg with Generated Choices
let char = await arg("Type then pick a char", (input) => {
// return an array of strings
return input.split("")
})
arg with Shortcuts
let url = "https://swapi.dev/api/people"
let name = await arg({
placeholder: "Select a name",
shortcuts: [
{
name: "Explore API",
key: "cmd+e",
onPress: async () => {
open(url)
},
bar: "right"
}
]
}, async () => {
let response = await get(url);
return response?.data?.results.map((p) => p.name);
})
micro
Same API as arg
, but with a tiny, adorable UI.
env
Load an env var if it exists, prompt to set the env var if not:
// Write write "MY_ENV_VAR" to ~/.kenv/.env
let MY_ENV_VAR = await env("MY_ENV_VAR")
You can also prompt the user to set the env var using a prompt by nesting it in an async function:
// Prompt the user to select from a path
let OUTPUT_DIR = await env("OUTPUT_DIR", async () => {
return await path({
hint: `Select the output directory`,
})
})
editor
The editor
function opens a text editor with the given text. The editor is a full-featured "Monaco" editor with syntax highlighting, find/replace, and more. The editor is a great way to edit or update text to write a file. The default language is markdown.
editor Hello World
let content = await editor()
editor with Initial Content
let content = await editor("Hello world!")
Load Remote Text Content into Editor
let response = await get(`https://raw.githubusercontent.com/johnlindquist/kit/main/API.md`)
let content = await editor(response.data)
div
div
displays HTML. Pass a string of HTML to div
to render it. div
is commonly used in conjunction with md
to render markdown.
Details
- Just like arg, the first argument is a string or a prompt configuration object.
- Optional:The second argument is a string of tailwind class to apply to the container, e.g.,
bg-white p-4
.
div Hello World
await div(`Hello world!`)
div with Markdown
await div(md(`
# Hello world!
### Thanks for coming to my demo
* This is a list
* This is another item
* This is the last item
`))
div with Tailwind Classes
await div(`Hello world!`, `bg-white text-black text-4xl p-4`)
div with Submit Links
let name = await div(md(`# Pick a Name
* [John](submit:John)
* [Mindy](submit:Mindy)
* [Joy](submit:Joy)
`))
await div(md(`# You selected ${name}`))
term
The term
function opens a terminal window. The terminal is a full-featured terminal, but only intended for running commands and CLI tools that require user input. term
is not suitable for long-running processes (try exec
instead).
Details
- Optional: the first argument is a command to run with the terminal
term Hello World
await term()
term with Command
await term(`cd ~/.kenv/scripts && ls`)
template
The template
prompt will present the editor populated by your template. You can then tab through each variable in your template and edit it.
Details
- The first argument is a string template. Add variables using $1, $2, etc. You can also use
Template Hello World
let text = await template(`Hello $1!`)
Standard Usage
let text = await template(`
Dear \${1:name},
Please meet me at \${2:address}
Sincerely, John`)
hotkey
The hotkey
prompt allows you to press modifier keys, then submits once you've pressed a non-monodifier key. For example, press command
then e
to submit key info about the command
and e
keys:
{
"key": "e",
"command": true,
"shift": false,
"option": false,
"control": false,
"fn": false,
"hyper": false,
"os": false,
"super": false,
"win": false,
"shortcut": "command e",
"keyCode": "KeyE"
}
This can be useful when you want to use a palette of commands and trigger each of them by switching on a hotkey.
Details
- Optional: The first argument is a string to display in the prompt.
hotkey Hello World
let keyInfo = await hotkey()
await editor(JSON.stringify(keyInfo, null, 2))