// Name: Widget Dynamic Listsimport "@johnlindquist/kit"let names = ["John", "Mindy", "Ben", "Scooter"]let items = _.shuffle(names).map(name => ({ name }))let html = `<div class="flex flex-col w-screen h-screen justify-around items-center"><buttonclass="rounded px-4 py-2 bg-black bg-opacity-50 hover:bg-opacity-25 w-1/2"v-for="(item, index) in items" :key="item.name" :data-name="item.name" :data-index="index">{{index}}. {{item.name}}</button><div>{{selected}}</div></div>`let w = await widget(html, {state: {items,selected: items[0].name}})w.onClick(event => {if (event.dataset.name) {w.setState({selected: event.dataset.name})}})setInterval(() => {items = _.shuffle(items)w.setState({items,})}, 1000)
import "@johnlindquist/kit"// Menu: Chrome History// Description: Open a url from your history// Author: John Lindquist// Twitter: @johnlindquistlet historyFilePath = home("Library/Application Support/Google/Chrome/Default/History")// ~/.kenv/tmp/chrome-history/Historylet tmpHistoryPath = tmpPath("History")// We make copy of the db each time or you run into a SQL_BUSY if Chrome is openawait copyFile(historyFilePath, tmpHistoryPath)let sqlite3 = await npm("sqlite3")let db = new sqlite3.Database(tmpHistoryPath)let limit = 1000let sql = `SELECT url, title, last_visit_time FROM urls ORDER BY last_visit_time DESC LIMIT ${limit}`let callback = async (err, rows) => {let url = await arg("Chrome History",rows.map(({ url, title }) => {return {name: title,description: url,value: url,}}))open(url)}db.all(sql, callback)db.close()
// Name: Get Data from a Widgetimport "@johnlindquist/kit"let state = {firstName: "",lastName: "",fullName: "",}let w = await widget(`<div class="flex flex-col justify-center items-center text-white"><form class="flex flex-col"><input id="first" type="text" class="text-black" placeholder="First Name" autofocus><input id="last" type="text" class="text-black" placeholder="Last Name"><button id="submit" type="submit">Submit</button></form><div class="flex flex-col"><div>{{firstName}} {{lastName}}</div><div>{{fullName}}</div></div></div>`,{height: 360,width: 360,state,})w.onInput(event => {if (event.targetId === "first") {state.firstName = event.value}if (event.targetId === "last") {state.lastName = event.value}w.setState(state)})w.onClick(event => {if (event.targetId === "submit") {state.fullName = `Hello, ${state.firstName} ${state.lastName}`}w.setState(state)})
// Name: Post URL with Metadata to Tanaimport "@johnlindquist/kit"let og = await npm("opengraph-io")// You'll need to sign-up for https://www.opengraph.io/// Create a free account and grab an API key from: https://dashboard.opengraph.io/apis/Opengraphio// Paste the token when prompted for OPENGRAPH_API_ID_TOKENlet openGraph = og({appId: await env("OPENGRAPH_API_ID_TOKEN", {hint: `Grab a token from [here](https://dashboard.opengraph.io/apis/Opengraphio)`,}),cacheOk: true, // If a cached result is available, use it for quicknessuseProxy: false, // Proxies help avoid being blocked and can bypass capchasmaxCacheAge: 432000000, // The maximum cache age to acceptacceptLang: "en-US,en;q=0.9", // Language to present to the site.fullRender: false, // This will cause JS to execute when rendering to deal with JS dependant sites})let url = await getActiveTab()// on windows, remove above and use:// let url = await arg("Paste URL here")let result = await openGraph.getSiteInfo(url)debuggerif (result.openGraph.error) {await editor(result.openGraph.error)} else {let { title, site_name, description } = result.openGraph// In the future, Tana will support more than just raw text// For now, only text is supported.// This gives you a chance to tweak the Title/Description before posting:let note = await template(`${title} - ${site_name}- ${url}- ${description}`.trim())let TANA_URL ="https://europe-west1-tagr-prod.cloudfunctions.net"// Select a node in Tana, hit cmd+K from mac, ctrl+K for windows, search for "API" to generate a token// Paste the token when promptedlet TANA_TOKEN_TODOS = await env("TANA_TOKEN_TODOS_TWO")await hide()// await copy(note.trim()) if you prefer to copy/paste you can use this insteadawait get(`${TANA_URL}/addToNode`, {headers: {authorization: `Bearer ${TANA_TOKEN_TODOS}`,},params: {note: note.trim(),},})}debugger
// Name: Grab Readwise Highlightsimport "@johnlindquist/kit"let API_TOKEN = await env("TANA_API_TOKEN")let HIGHLIGHTS_URL = "https://readwise.io/api/v2/export/"// Credit to: https://github.com/tanainc/tana-paste-examples// Copy/pasted and refactored to Script Kit: scriptkit.comlet getItemsFromReadwise = async (daysToFetch = 1) => {let dateOffset = 24 * 60 * 60 * 1000 * daysToFetchlet updatedAfterDate = new Date()updatedAfterDate.setTime(updatedAfterDate.getTime() - dateOffset)let response = await fetch(`${HIGHLIGHTS_URL}?updatedAfter=${updatedAfterDate.toISOString()}`,{headers: {Authorization: `Token ${API_TOKEN}`,},})return await response.json()}let daysToFetch = await arg("Days to fetch")let { results } = await getItemsFromReadwise(parseInt(daysToFetch, 10))let result = results.map(book => {let isValidSourceURL =book.source_url?.startsWith("https://")let hasHighlights = book.highlights?.length > 0let title = `${book.title}`let url = ``if (isValidSourceURL) {url = `URL:: ${book.source_url}`}let type = `type:: ${book.category?.replace(/s$/, "")}`let author = `author:: ${book.author}`let highlights = book.highlights.map(highlight => {let lines = highlight.text.split("\n")let cleanLines = lines.map(line => {let cleanedLine = line.replace(/•\s+/, "").trim()return `${cleanedLine}`})let note = ``if (highlight.note) {note = `${highlight.note}`}return `${cleanLines}${note}`.trim()})let maybeUrl = url? `- ${url}`: ``let maybeHighlights = highlights?.length? `- Highlights- ${highlights.join("\n")}`: ``return `- ${title} #from-the-web${maybeUrl}- ${type}- ${author}${maybeHighlights}`.trim()})// because I'm on a trial account (I assume)let [skip, ...rest] = result// copies the readwise content to the clipboard// I'm using slice to only take 2 results for demo purposesawait copy(`%%tana%%${rest.slice(0, 2).join("\n")}`)
// Name: Restart Opalimport "@johnlindquist/kit"let fkill = await npm("fkill")let open = await npm("open")let app = "Opal"console.log(`Closing Opal`)try {fkill(app, { silent: true })} catch {console.log(`Wasn't running...`)}await wait(1000)console.log(`Opening Opal`)await open.openApp(app)
// Name: Synonym Snippet// Snippet: *,,import "@johnlindquist/kit"let word = await arg("The value of *")let response = await get(`https://api.datamuse.com/words?ml=${word}`)let synonyms = response.data.map(i => i.word)let synonym = await arg("Select a replacement", synonyms)await setSelectedText(synonym)
// Name: Midjourney Prompt// Description: Generate a Random Midjourney Prompt// Author: John Lindquist// Twitter: @johnlindquistimport "@johnlindquist/kit"let count = parseInt(await arg({placeholder: "How many prompts to generate and paste?",onInput: input => {submit(input) //auto submit the number on press},}),10)let frontmost = await npm("frontmost-app")let exitIfNotDiscord = async () => {let { localizedName: name } = await frontmost()if (name !== "Discord") {exit()}}await exitIfNotDiscord()let n = (x = 0) =>`::${Number(_.random(0.2, 2.2) + x).toPrecision(1)}`let iw = (x = 0) =>`${Number(_.random(0.2, 2.2) + x).toPrecision(1)}`let parentDir = await env("MIDJOURNEY_PARENT_DIR",async () => {return await path({hint: `Select a parent dir to clone the repo: https://github.com/johnlindquist/midjourney`,})})let mjDir = path.resolve(parentDir, "midjourney")let mjExists = await isDir(mjDir)if (!mjExists) {cd(parentDir)await exec(`git clone https://github.com/johnlindquist/midjourney`)cd(mjDir)} else {cd(mjDir)await exec(`git pull`)}// await wiki() will pull a random wikipedia titlelet wiki = await npm("random-word-wikipedia")// Generate functions based on .md files in the repolet files = await readdir(mjDir)files = files.filter(file => file.endsWith(".md"))for await (let file of files) {let filePath = path.resolve(mjDir, file)let list = await readFile(filePath, "utf-8")let { name } = path.parse(filePath)global[name] = () => _.sample(list.trim().split("\n"))}hide()let go = async () => {await exitIfNotDiscord()// Customize however you want. More old "maybe working" examples here: https://github.com/johnlindquist/midjourney/blob/main/prompts.mdlet fnsPrompt = `/imagine prompt: ${image()} ${myimage()} Studio photoshoot of ${celeb()} ${animal()} ${hero()}::4 ${await wiki()}${n()} in a ${color()} ${climate()}${n()} in the style of ${game()}${n()} taken by ${photographer()}${n()} by ${artist()}${n()} stylized like ${style()} and ${rank()}${n()} --stylize ${_.random(1000,3000)} --uplight --aspect 16:9 --iw ${iw()}`let prompt = _.sample([fnsPrompt])await exitIfNotDiscord()await setSelectedText(prompt)log(prompt)await wait(1000)await exitIfNotDiscord() //I try to be extra careful when scripting the `Enter` keyawait keyboard.pressKey(Key.Enter)await keyboard.releaseKey(Key.Enter)await wait(250) // Discord needs a sec after paste+enter to recorgnize it as a commandawait exitIfNotDiscord()await keyboard.pressKey(Key.Enter)await keyboard.releaseKey(Key.Enter)}let i = 0while (i < count) {i++await go()await wait(1000)}
// Name: xstate widgetimport "@johnlindquist/kit"let { createMachine, interpret } = await npm("xstate")let initial = 'inactive'let toggleMachine = createMachine({id: 'toggle',initial,states: {inactive: { on: { TOGGLE: 'active' } },active: { on: {TOGGLE: 'inactive'}}}})// Widgets use "petite-vue" templating with state on the root// This allows to send a state objectlet w = await widget(`<div class="p-4 text-4xl"><button>Click</button><div>{{ value === "active" ? "💚" : "💔"}}<div></div>`, {state: {value: initial}})let toggleService = interpret(toggleMachine).onTransition(state => {log(state) // ~/.kenv/logs/xstate-widget.logw.setState(state)}).start()w.onClick(() => {toggleService.send('TOGGLE')})
// Name: Download Chrome Video// Description: Downloads the video of the current Chrome page// Author: John Lindquist// Twitter: @johnlindquist// REQUIRES https://formulae.brew.sh/formula/youtube-dlimport "@johnlindquist/kit"import Stream from "stream"let writeableStream = new Stream.Writable()writeableStream._write = (chunk, encoding, next) => {console.log(chunk.toString().trim())next()}cd(await path())let url = await getActiveTab()setChoices(null)setDescription(`Downloading ${url}...`)setInput(``)setPlaceholder(`Please wait...`)exec(`/opt/homebrew/bin/youtube-dl ${url}`, {all: true}).all.pipe(writeableStream)
// Name: Homebrew Search// Description: Search and Install Homebrew Formulae// Author: John Lindquist// Twitter: @johnlindquistimport "@johnlindquist/kit"let response = await get(`https://formulae.brew.sh/api/formula.json`)let homebrewChoices = response.data.map(({name, tap}) => {return {name,value: name,description: tap,preview: async ()=> {let response = await get(`https://formulae.brew.sh/api/formula/${name}.json`)let {full_name, tap, desc, homepage, versions, urls} = response.datareturn md(`## ${full_name}### ${tap}${desc}[${homepage}](${homepage})* Version - ${versions?.stable}* URLs - ${urls?.stable?.url}`)}}})let formula = await arg("Search homebrew", homebrewChoices)let bins = await readdir(`/opt/homebrew/bin`)let installed = bins.includes(formula)if(installed){setDescription(`${formula} already installed`)}let message = `${installed ? `Uninstall` : `Install`} ${formula}?`let confirm = await arg(message, [{name: `[y]es`, value: true},{name: `[n]o`, value: false}])setChoices(null)setPlaceholder(`Please wait...`)if(confirm){setDescription(`${installed ? `Uninstalling` : `Installing`} ${formula}`)await exec(`/opt/homebrew/bin/brew ${installed ? `uninstall` : `install`} ${formula}`)}
// Name: Toggle Desktop Icons// Author: John Lindquist// Twitter: @johnlindquistimport "@johnlindquist/kit"let visible = truetry{// This command fails if icons are hiddenlet {stdout} = await exec(`defaults read com.apple.finder CreateDesktop`)visible = stdout === 1}catch{}let command = `defaults ${visible ? "write" : "delete"} com.apple.finder CreateDesktop ${visible ? "-bool FALSE" : ""};killall Finder`await exec(command)
// Name: Grab Captionimport "@johnlindquist/kit"// This query selector is specific to Disney pluslet js = `Array.from(document.querySelectorAll('.dss-subtitle-renderer-line')).map(el => el.innerText)`let value = await applescript(`tell application "Google Chrome" to tell window 1set str to execute active tab javascript "${js}"return strend tell`)await div(md(`## ${value}`))// To copy to clipboard, use:// copy(value)
// Menu: Search Anime
// Description: Use the jikan.moe API to search anime
// Author: John Lindquist
// Twitter: @johnlindquist
let anime = await arg("Anime:")
let response = await get(
`https://api.jikan.moe/v3/search/anime?q=${anime}`
)
let { image_url, title } = response.data.results[0]
showImage(image_url, { title })
// Menu: App Launcher
// Description: Search for an app then launch it
// Author: John Lindquist
// Twitter: @johnlindquist
let createChoices = async () => {
let apps = await fileSearch("", {
onlyin: "/",
kind: "application",
})
let prefs = await fileSearch("", {
onlyin: "/",
kind: "preferences",
})
let group = path => apps =>
apps
.filter(app => app.match(path))
.sort((a, b) => {
let aName = a.replace(/.*\//, "")
let bName = b.replace(/.*\//, "")
return aName > bName ? 1 : aName < bName ? -1 : 0
})
return [
...group(/^\/Applications\/(?!Utilities)/)(apps),
...group(/\.prefPane$/)(prefs),
...group(/^\/Applications\/Utilities/)(apps),
...group(/System/)(apps),
...group(/Users/)(apps),
].map(value => {
return {
name: value.split("/").pop().replace(".app", ""),
value,
description: value,
}
})
}
let appsDb = await db("apps", async () => ({
choices: await createChoices(),
}))
let app = await arg("Select app:", appsDb.choices)
let command = `open -a "${app}"`
if (app.endsWith(".prefPane")) {
command = `open ${app}`
}
exec(command)
// Menu: Book Search
// Description: Use Open Library API to search for books
// Author: John Lindquist
// Twitter: @johnlindquist
let query = await arg('Search for a book title:')
//This API can be a little slow. Wait a couple seconds
let response = await get(`http://openlibrary.org/search.json?q=${query}`)
let transform = ({title, author_name}) =>
`* "${title}" - ${author_name?.length && author_name[0]}`
let markdown = response.data.docs.map(transform).join('\n')
inspect(markdown, 'md')
// Menu: Center App
// Description: Center the frontmost app
// Author: John Lindquist
// Twitter: @johnlindquist
let { workArea, bounds } = await getActiveScreen()
let { width, height } = workArea
let { x, y } = bounds
let padding = 100
let top = y + padding
let left = x + padding
let right = x + width - padding
let bottom = y + height - padding
setActiveAppBounds({
top,
left,
right,
bottom,
})
// Menu: Chrome Bookmarks
// Description: Select and open a bookmark from Chrome
// Author: John Lindquist
// Twitter: @johnlindquist
let bookmarks = await readFile(
home(
"Library/Application Support/Google/Chrome/Default/Bookmarks"
)
)
bookmarks = JSON.parse(bookmarks)
bookmarks = bookmarks.roots.bookmark_bar.children
let url = await arg(
"Select bookmark",
bookmarks.map(({ name, url }) => {
return {
name,
description: url,
value: url,
}
})
)
exec(`open "${url}"`)
// Menu: Open Chrome Tab
// Description: List all Chrome tabs. Then switch to that tab
// Author: John Lindquist
// Twitter: @johnlindquist
let currentTabs = await getTabs()
let bookmarks = await readFile(
home(
"Library/Application Support/Google/Chrome/Default/Bookmarks"
)
)
bookmarks = JSON.parse(bookmarks)
bookmarks = bookmarks.roots.bookmark_bar.children
let bookmarkChoices = bookmarks.map(({ name, url }) => {
return {
name: url,
description: name,
value: url,
}
})
let currentOpenChoices = currentTabs.map(
({ url, title }) => ({
name: url,
value: url,
description: title,
})
)
let bookmarksAndOpen = [
...bookmarkChoices,
...currentOpenChoices,
]
let choices = _.uniqBy(bookmarksAndOpen, "name")
let url = await arg("Focus Chrome tab:", choices)
focusTab(url)
// Menu: Chrome Tab Switcher
// Description: List all Chrome tabs. Then switch to that tab
// Author: John Lindquist
// Twitter: @johnlindquist
let tabs = await getTabs()
let url = await arg(
"Select Chrome tab:",
tabs.map(({ url, title }) => ({
name: url,
value: url,
description: title,
}))
)
focusTab(url)
// Description: Launch a url in Chrome. If url is already open, switch to that tab.
// Author: John Lindquist
// Twitter: @johnlindquist
let url = await arg("Enter url:")
focusTab(url)
// Menu: Convert Colors
// Description: Converts colors between rgb, hex, etc
// Author: John Lindquist
// Twitter: @johnlindquist
let convert = await npm("color-convert")
let createChoice = (type, value, input) => {
return {
name: type + ": " + value,
value,
html: `<div class="h-full w-full p-1 text-xs flex justify-center items-center font-bold" style="background-color:${input}">
<span>${value}</span>
</div>`,
}
}
//using a function with "input" allows you to generate values
let conversion = await arg("Enter color:", input => {
if (input.startsWith("#")) {
return ["rgb", "cmyk", "hsl"].map(type => {
let value = convert.hex[type](input).toString()
return createChoice(type, value, input)
})
}
//two or more lowercase
if (input.match(/^[a-z]{2,}/)) {
return ["rgb", "hex", "cmyk", "hsl"]
.map(type => {
try {
let value =
convert.keyword[type](input).toString()
return createChoice(type, value, input)
} catch (error) {
return ""
}
})
.filter(Boolean)
}
return []
})
setSelectedText(conversion)
// Menu: John's personal startup script for scriptkit.com
// Description: This probably won't run on your machine 😜
// Author: John Lindquist
// Twitter: @johnlindquist
edit(`~/projects/scriptkit.com`)
iterm(`cd ~/projects/scriptkit.com; vercel dev`)
await focusTab("http://localhost:3000")
// Menu: Search for a File
// Description: File Search
// Author: John Lindquist
// Twitter: @johnlindquist
/** Note: This is a very basic search implementation based on "mdfind".
* File search will be a _big_ focus in future versions of Script Kit
*/
let selectedFile = await arg(
"Search a file:",
async input => {
if (input?.length < 4) return []
let files = await fileSearch(input)
return files.map(path => {
return {
name: path.split("/").pop(),
description: path,
value: path,
}
})
}
)
exec(`open ${selectedFile}`)
// Description: Launch Twitter in Chrome. If Twitter is already open, switch to that tab.
// Author: John Lindquist
// Twitter: @johnlindquist
// Shortcut: opt t
//runs the "chrome-tab" script with twitter.com passed into the first `arg`
await run("chrome-tab", "twitter.com")
// Menu: Giphy
// Description: Search giphy. Paste link.
// Author: John Lindquist
// Twitter: @johnlindquist
let download = await npm("image-downloader")
let queryString = await npm("query-string")
let GIPHY_API_KEY = await env("GIPHY_API_KEY", {
hint: md(
`Get a [Giphy API Key](https://developers.giphy.com/dashboard/)`
),
ignoreBlur: true,
secret: true,
})
let search = q =>
`https://api.giphy.com/v1/gifs/search?api_key=${GIPHY_API_KEY}&q=${q}&limit=10&offset=0&rating=g&lang=en`
let { input, url } = await arg(
"Search giphy:",
async input => {
if (!input) return []
let query = search(input)
let { data } = await get(query)
return data.data.map(gif => {
return {
name: gif.title.trim() || gif.slug,
value: {
input,
url: gif.images.original.url,
},
preview: `<img src="${gif.images.downsized.url}" alt="">`,
}
})
}
)
let formattedLink = await arg("Format to paste", [
{
name: "URL Only",
value: url,
},
{
name: "Markdown Image Link",
value: `![${input}](${url})`,
},
{
name: "HTML <img>",
value: `<img src="${url}" alt="${input}">`,
},
])
setSelectedText(formattedLink)
// Menu: Gist from Finder
// Description: Select a file in Finder, then create a Gist
// Author: John Lindquist
// Twitter: @johnlindquist
let filePath = await getSelectedFile()
let file = filePath.split("/").pop()
let isPublic = await arg("Should the gist be public?", [
{ name: "No", value: false },
{ name: "Yes", value: true },
])
const body = {
files: {
[file]: {
content: await readFile(filePath, "utf8"),
},
},
}
if (isPublic) body.public = true
let config = {
headers: {
Authorization:
"Bearer " +
(await env("GITHUB_GIST_TOKEN", {
info: `Create a gist token: <a class="bg-white" href="https://github.com/settings/tokens/new">https://github.com/settings/tokens/new</a>`,
message: `Set .env GITHUB_GIST_TOKEN:`,
})),
},
}
const response = await post(
`https://api.github.com/gists`,
body,
config
)
exec(`open ` + response.data.html_url)
// Menu: Google Image Grid
// Description: Create a Grid of Images
// Author: John Lindquist
// Twitter: @johnlindquist
let gis = await npm("g-i-s")
await arg("Search for images:", async input => {
if (input.length < 3) return ``
let searchResults = await new Promise(res => {
gis(input, (_, results) => {
res(results)
})
})
return `<div class="flex flex-wrap">${searchResults
.map(({ url }) => `<img class="h-32" src="${url}" />`)
.join("")}</div>`
})
// Menu: Hello World
// Description: Enter an name, speak it back
// Author: John Lindquist
// Twitter: @johnlindquist
let name = await arg(`What's your name?`)
say(`Hello, ${name}!`)
// Menu: Detect Image Width and Height
// Description: Show the metadata of an image
// Author: John Lindquist
// Twitter: @johnlindquist
let sharp = await npm("sharp")
let image = await arg("Search an image:", async input => {
if (input.length < 3) return []
let files = await fileSearch(input, { kind: "image" })
return files.map(path => {
return {
name: path.split("/").pop(),
value: path,
description: path,
}
})
})
let { width, height } = await sharp(image).metadata()
console.log({ width, height })
await arg(`Width: ${width} Height: ${height}`)
// Menu: Resize an Image
// Description: Select an image in Finder. Type option + i to resize it.
// Author: John Lindquist
// Twitter: @johnlindquist
// Shortcut: opt i
let sharp = await npm("sharp")
let imagePath = await getSelectedFile()
let width = Number(await arg("Enter width:"))
let metadata = await sharp(imagePath).metadata()
let newHeight = Math.floor(
metadata.height * (width / metadata.width)
)
let lastDot = /.(?!.*\.)/
let resizedImageName = imagePath.replace(
lastDot,
`-${width}.`
)
await sharp(imagePath)
.resize(width, newHeight)
.toFile(resizedImageName)
// Menu: Dad Joke
// Description: Logs out a Dad Joke from icanhazdadjoke.com
// Author: John Lindquist
// Twitter: @johnlindquist
let response = await get(`https://icanhazdadjoke.com/`, {
headers: {
Accept: "text/plain",
},
})
let joke = response.data
setPanel(joke)
say(joke)
// Menu: New Journal Entry
// Description: Generate a file using the current date in a specified folder
// Author: John Lindquist
// Twitter: @johnlindquist
let { format } = await npm("date-fns")
let date = format(new Date(), "yyyy-MM-dd")
let journalPath = await env("JOURNAL_PATH")
if (!(await isDir(journalPath))) {
mkdir("-p", journalPath)
}
let journalFile = path.join(journalPath, date + ".md")
if (!(await isFile(journalFile))) {
let journalPrompt = `How are you feeling today?`
await writeFile(journalFile, journalPrompt)
}
edit(journalFile, env?.JOURNAL_PATH)
// Menu: Open Project
// Description: List dev projects
// Author: John Lindquist
// Twitter: @johnlindquist
let { projects, write } = await db("projects", {
projects: [
"~/.kit",
"~/projects/kitapp",
"~/projects/scriptkit.com",
],
})
onTab("Open", async () => {
let project = await arg("Open project:", projects)
edit(project)
})
onTab("Add", async () => {
while (true) {
let project = await arg(
"Add path to project:",
md(projects.map(project => `* ${project}`).join("\n"))
)
projects.push(project)
await write()
}
})
onTab("Remove", async () => {
while (true) {
let project = await arg("Open project:", projects)
let indexOfProject = projects.indexOf(project)
projects.splice(indexOfProject, 1)
await write()
}
})
// Menu: Paste URL
// Description: Copy the current URL from your browser. Paste it at cursor.
// Author: John Lindquist
// Twitter: @johnlindquist
let url = await getActiveTab()
await setSelectedText(url)
// Menu: Project Name
// Description: Generate an alliteraive, dashed project name, copies it to the clipboard, and shows a notification
// Author: John Lindquist
// Twitter: @johnlindquist
let { generate } = await npm("project-name-generator")
const name = generate({
word: 2,
alliterative: true,
}).dashed
await setSelectedText(name)
// Menu: Quick Thoughts
// Description: Add lines to today's journal page
// Author: John Lindquist
// Twitter: @johnlindquist
let { format } = await npm("date-fns")
let date = format(new Date(), "yyyy-MM-dd")
let thoughtsPath = await env("THOUGHTS_PATH")
let thoughtFile = path.join(thoughtsPath, date + ".md")
let firstEntry = true
let addThought = async thought => {
if (firstEntry) {
thought = `
- ${format(new Date(), "hh:mmaa")}
${thought}\n`
firstEntry = false
} else {
thought = ` ${thought}\n`
}
await appendFile(thoughtFile, thought)
}
let openThoughtFile = async () => {
let { stdout } = exec(`wc ${thoughtFile}`, {
silent: true,
})
let lineCount = stdout.trim().split(" ").shift()
edit(thoughtFile, thoughtsPath, lineCount + 1) //open with cursor at end
await wait(500)
exit()
}
if (!(await isFile(thoughtFile)))
await writeFile(thoughtFile, `# ${date}\n`)
while (true) {
let thought = await arg({
placeholder: "Thought:",
hint: `Type "open" to open journal`,
})
if (thought === "open") {
await openThoughtFile()
} else {
await addThought(thought)
}
}
// Menu: Read News
// Description: Scrape headlines from news.google.com then pick headline to read
// Author: John Lindquist
// Twitter: @johnlindquist
let headlines = await scrapeSelector(
"https://news.google.com",
"h3",
el => ({
name: el.innerText,
value: el.firstChild.href,
})
)
let url = await arg("What do you want to read?", headlines)
exec(`open "${url}"`)
// Menu: Reddit
// Description: Browse Reddit from Script Kit
// Author: John Lindquist
// Twitter: @johnlindquist
let Reddit = await npm("reddit")
let envOptions = {
ignoreBlur: true,
hint: md(
`[Create a reddit app](https://www.reddit.com/prefs/apps)`
),
secret: true,
}
let reddit = new Reddit({
username: await env("REDDIT_USERNAME"),
password: await env("REDDIT_PASSWORD"),
appId: await env("REDDIT_APP_ID", envOptions),
appSecret: await env("REDDIT_APP_SECRET", envOptions),
userAgent: `ScriptKit/1.0.0 (https://scriptkit.com)`,
})
let subreddits = [
"funny",
"aww",
"dataisbeautiful",
"mildlyinteresting",
"RocketLeague",
]
subreddits.forEach(sub => {
onTab(sub, async () => {
let url = await arg(
"Select post to open:",
async () => {
let best = await reddit.get(`/r/${sub}/hot`)
return best.data.children.map(({ data }) => {
let {
title,
thumbnail,
url,
subreddit_name_prefixed,
preview,
} = data
let resolutions =
preview?.images?.[0]?.resolutions
let previewImage =
resolutions?.[resolutions?.length - 1]?.url
return {
name: title,
description: subreddit_name_prefixed,
value: url,
img: thumbnail,
...(previewImage && {
preview: md(`
![${title}](${previewImage})
### ${title}
`),
}),
}
})
}
)
exec(`open "${url}"`)
})
})
// Menu: Share Selected File
// Description: Select a file in Finder. Creates tunnel and copies link to clipboard.
// Author: John Lindquist
// Twitter: @johnlindquistt
// Background: true
let ngrok = await npm("ngrok")
let handler = await npm("serve-handler")
let exitHook = await npm("exit-hook")
let http = await import("http")
let filePath = await getSelectedFile()
let symLinkName = _.last(
filePath.split(path.sep)
).replaceAll(" ", "-")
let symLinkPath = tmp(symLinkName)
console.log(`Creating temporary symlink: ${symLinkPath}`)
ln(filePath, symLinkPath)
let port = 3033
const server = http.createServer(handler)
cd(tmp())
server.listen(port, async () => {
let tunnel = await ngrok.connect(port)
let shareLink = tunnel + "/" + symLinkName
console.log(
chalk`{yellow ${shareLink}} copied to clipboard`
)
copy(shareLink)
})
exitHook(() => {
server.close()
if (test("-f", symLinkPath)) {
console.log(
`Removing temporary symlink: ${symLinkPath}`
)
exec(`rm ${symLinkPath}`)
}
})
// Menu: Open Sound Prefs
// Description: Open the Sound prefs panel
// Author: John Lindquist
// Twitter: @johnlindquist
exec(`open /System/Library/PreferencePanes/Sound.prefPane`)
// Menu: Speak Script
// Description: Run a Script based on Speech Input
// Author: John Lindquist
// Twitter: @johnlindquist
let { scripts } = await db("scripts")
let escapedScripts = scripts.map(script => ({
name: `"${script.name.replace(/"/g, '\\"')}"`, //escape quotes
value: script.filePath,
}))
let speakableScripts = escapedScripts
.map(({ name }) => name)
.join(",")
let speech = await applescript(String.raw`
tell application "SpeechRecognitionServer"
listen for {${speakableScripts}}
end tell
`)
let script = escapedScripts.find(
script => script.name == `"${speech}"`
)
await run(script.value)
// Menu: Speed Reader
// Description: Display clipboard content at a defined rate
// Author: John Lindquist
// Twitter: @johnlindquist
let wpm = 1000 * (60 / (await arg('Enter words per minute:')))
let text = await paste()
text = text
.trim()
.split(' ')
.filter(Boolean)
.flatMap((sentence) => sentence.trim().split(' '))
let i = 0
let id = setInterval(() => {
setPlaceholder(` ${text[i++]}`)
if (i >= text.length) clearInterval(id)
}, wpm)
// Menu: Synonym
// Description: List synonyms
// Author: John Lindquist
// Twitter: @johnlindquist
let synonym = await arg("Type a word", async input => {
if (!input || input?.length < 3) return []
let url = `https://api.datamuse.com/words?ml=${input}&md=d`
let response = await get(url)
return response.data.map(({ word, defs }) => {
return {
name: `${word}${defs?.[0] && ` - ${defs[0]}`}`,
value: word,
selected: `Paste ${word}`,
}
})
})
setSelectedText(synonym)
// Menu: Title Case
// Description: Converts the selected text to title case
// Author: John Lindquist
// Twitter: @johnlindquist
let { titleCase } = await npm("title-case")
let text = await getSelectedText()
let titleText = titleCase(text)
await setSelectedText(titleText)
// Menu: Update Twitter Name
// Description: Change your name on twitter
// Author: John Lindquist
// Twitter: @johnlindquist
let Twitter = await npm('twitter-lite')
let envOptions = {
hint: md(
`You need to [create an app](https://developer.twitter.com/en/apps) to get these keys/tokens`,
),
ignoreBlur: true,
secret: true,
}
let client = new Twitter({
consumer_key: await env('TWITTER_CONSUMER_KEY', envOptions),
consumer_secret: await env('TWITTER_CONSUMER_SECRET', envOptions),
access_token_key: await env('TWITTER_ACCESS_TOKEN_KEY', envOptions),
access_token_secret: await env('TWITTER_ACCESS_TOKEN_SECRET', envOptions),
})
let name = await arg('Enter new twitter name:')
let response = await client
.post('account/update_profile', {
name,
})
.catch((error) => console.log(error))
// Menu: Vocab Quiz
// Description: Quiz on random vocab words
// Author: John Lindquist
// Twitter: @johnlindquist
await npm("wordnet-db")
let randomWord = await npm("random-word")
let { WordNet } = await npm("natural")
let wordNet = new WordNet()
let words = []
while (true) {
setPlaceholder(`Finding random word and definitions...`)
while (words.length < 4) {
let quizWord = randomWord()
let results = await new Promise(resolve => {
wordNet.lookup(quizWord, resolve)
})
if (results.length) {
let [{ lemma, def }] = results
words.push({ name: def, value: lemma })
}
}
let word = words[0]
let result = await arg(
`What does "${word.value}" mean?`,
_.shuffle(words)
)
let correct = word.value === result
setPlaceholder(
`${correct ? "✅" : "🚫"} ${word.value}: ${word.name}`
)
words = []
await wait(2000)
}
// Menu: Word API
// Description: Queries a word api. Pastes selection.
// Author: John Lindquist
// Twitter: @johnlindquist
let typeMap = {
describe: "rel_jjb",
trigger: "rel_trg",
noun: "rel_jja",
follow: "lc",
rhyme: "rel_rhy",
spell: "sp",
synonym: "ml",
sounds: "rel_nry",
suggest: "suggest",
}
let word = await arg("Type a word and hit Enter:")
let typeArg = await arg(
"What would you like to find?",
Object.keys(typeMap)
)
let type = typeMap[typeArg]
word = word.replace(/ /g, "+")
let url = `https://api.datamuse.com/words?${type}=${word}&md=d`
if (typeArg == "suggest")
url = `https://api.datamuse.com/sug?s=${word}&md=d`
let response = await get(url)
let formattedWords = response.data.map(({ word, defs }) => {
let info = ""
if (defs) {
let [type, meaning] = defs[0].split("\t")
info = `- (${type}): ${meaning}`
}
return {
name: `${word}${info}`,
value: word,
}
})
let pickWord = await arg("Select to paste:", formattedWords)
setSelectedText(pickWord)