import '@johnlindquist/kit'
const name = await arg<string>("What's your name?")
await div(md(`# Hello, ${name}! Let's explore Script Kit.`))
const [favColor, favAnimal] = await fields<[string, string]>([
"Favorite Color",
"Favorite Animal",
])
await toast(`You chose ${favColor} and ${favAnimal}!`)
const options: Choice[] = [
{ name: "Edit a file", value: "edit" },
{ name: "Show system info", value: "system" },
{ name: "Take a selfie", value: "selfie" },
]
const action = await select<string>("What do you want to do?", options)
if (action === "edit") {
const filePath = await path()
const initialContent = await isFile(filePath)
? await readFile(filePath, 'utf8')
: ''
const newContent = await editor({
value: initialContent,
language: 'javascript',
onInput: async (text) => {
setPanel(md(`Characters: ${text.length}`))
},
})
await writeFile(filePath, newContent) await div(md(`File updated: [${filePath}](${filePath})`))
} else if (action === "system") {
await term({
command: `echo "Logged in as $(whoami)" && echo "Current Directory: $(pwd)"`,
cwd: home(),
})
} else if (action === "selfie") {
const buffer = await webcam()
const imagePath = tmpPath("selfie.jpg")
await writeFile(imagePath, buffer)
await div(md(``))
}
const confirm = await arg<string>("Do you want to see your clipboard history?", [
"Yes",
"No",
])
if (confirm === "Yes") {
const history = await getClipboardHistory()
if (history.length > 0) {
const selected = await arg<string>(
"Select from clipboard:",
history.map((h) => h.value)
)
await editor(selected)
} else {
await div(md("Clipboard history is empty."))
}
}
const dbKey = "scriptkit-demo"
const demoDb = await db<{ items: string[] }>(dbKey, {
items: [],
})
const newItem = await arg<string>(
"Add an item to the list (or type nothing to skip):",
[]
)
if (newItem) {
demoDb.data.items.push(newItem)
await demoDb.write()
}
await div(
md(`## Current Items:\n\n${demoDb.data.items.map((item) => `- ${item}`).join('\n')}`)
)
notify({
title: "Script Kit Demo",
message: `Thanks for checking out the features, ${name}!`,
})