Scripts by dealingwith

Genius Lyrics Lookup

Just what it says on the tin. How can I improve this one?

Install song-search

// Menu: Genius Lyrics Search
let Genius = await npm('genius-lyrics-api')
let geniusUserToken = await env("GENIUS_AUTH_TOKEN")
import { getLyrics, searchSong } from 'genius-lyrics-api';
let songTitle = await arg("Song Title")
let options = {
apiKey: geniusUserToken,
title: songTitle,
artist: '',
optimizeQuery: true
}
let returnedSongs = await(searchSong(options).then((r) => r))
let returnedSongTitles = returnedSongs.map(s => s.title)
let chosenSongTitle = await arg("Which song?", returnedSongTitles)
let songData = returnedSongs.filter(s => s.title == chosenSongTitle)
let lyrics = await(getLyrics(songData[0].url).then((r) => r))
let whatToDo = await arg("", ["Copy", "View", "Visit"])
if (whatToDo == "Copy") copy(lyrics)
else if (whatToDo == "View") await textarea(lyrics)
else if (whatToDo == "Visit") await focusTab(songData[0].url)
Discuss Post

Resize selected images

Resizes selected images to specified width. Note: overwrites existing files.

Install batch-image-resize

let Jimp = await npm('jimp')
let width = await arg('New width in pixels')
let selectedFiles = await getSelectedFile();
let filePaths = selectedFiles.split("\n");
for (let filePath of filePaths) {
Jimp.read(filePath, (err, image) => {
if (err) throw err;
image.resize(parseInt(width), Jimp.AUTO).write(filePath); // save
});
}
Discuss Post

Title Case Selected Text

Here's a really quick one I decided to spin up this morning:

Install title-case

let { titleCase } = await npm("title-case");
let selectedText = await getSelectedText();
let value = titleCase(selectedText);
await setSelectedText(value);

...also, a tiny variant on the above I like to call The Proper Namifier™

let { titleCase } = await npm("title-case");
let selectedText = await getSelectedText();
let value = titleCase(selectedText) + '™';
await setSelectedText(value);
Discuss Post

Another Notion example -- select a random item from a database

Like the last Notion example I posted, this one interfaces with my giant, singular Tasks database in Notion. This one selects one task at random (that does not have a status of Done or Archive).

Install notion-random-task

let notionToken = await env('NOTION_USER_TOKEN')
let databaseID = "3859b567fda3464ea5a69d5ccb56274b"
let {data} = await post(
`https://api.notion.com/v1/databases/${databaseID}/query`,
{
"filter": {
"and": [
{
"property": "Status",
"select": {
"does_not_equal": "Done"
}
},
{
"property": "Status",
"select": {
"does_not_equal": "Archive"
}
}
]
},
page_size: 100
},
{
headers: {
Authorization: `Bearer ${notionToken}`,
"Content-Type": "application/json",
"Notion-Version": "2021-05-13"
}
})
let tasks = data.results
let task = tasks[Math.floor(Math.random() * tasks.length)];
let pageID = task.id.replace(/-/g, "");
let pageURL = `https://notion.so/${databaseID}?p=${pageID}`
copy(pageURL)
await focusTab(pageURL, "Google Chrome Beta")

I also created one that only queries for tasks with no status (the "inbox")...

Install notion-random-from-inbox

let notionToken = await env('NOTION_USER_TOKEN')
let databaseID = "3859b567fda3464ea5a69d5ccb56274b"
let {data} = await post(
`https://api.notion.com/v1/databases/${databaseID}/query`,
{
"filter":
{
"property": "Status",
"select": {
"is_empty": true
}
},
page_size: 50
},
{
headers: {
Authorization: `Bearer ${notionToken}`,
"Content-Type": "application/json",
"Notion-Version": "2021-05-13"
}
})
let tasks = data.results
let task = tasks[Math.floor(Math.random() * tasks.length)];
let pageID = task.id.replace(/-/g, "");
let pageURL = `https://notion.so/${databaseID}?p=${pageID}`
copy(pageURL)
await focusTab(pageURL, "Google Chrome Beta")

~Is there a better way to open the link in a browser? I stole this method from Ian Jones.~

Discuss Post

Post to Notion

Screen Shot 2021-05-19 at 11 06 46 PM

I thought the Notion API might be a fun thing to play with via Script Kit. I have a singular "tasks" database that I use to capture all manner of things, so I always want to insert pages into it in order to process them later. These scripts will require you to update the databaseID value in the code itself. I could see an expansion of this script to allow for the selection of a specific Notion database from a list.

The second script below takes a second input in order to insert content into the new page -- I was wondering if there is a way to allow multiline text input via Script Kit, which would make that script more robust.

You can get a Notion API token here and find quick-start instructions here, as well as the API reference here.

For a quick overview of the data model, check out Chris Biscardi's video here. For a more in-depth overview, check out this Notion blog post.

Install notion-add-task

let notionToken = await env('NOTION_USER_TOKEN')
let databaseID = "3859b567fda3464ea5a69d5ccb56274b"
let content = await arg('Enter Task')
let {data} = await post(
'https://api.notion.com/v1/pages', {
"parent": { "database_id": databaseID },
"properties": {
"Name": {
"title": [
{
"text": {
"content": content
}
}
]
}
}
},
{
headers: {
Authorization: `Bearer ${notionToken}`,
}
})
let pageID = data.id.replace(/-/g, "");
let pageURL = `https://notion.so/${databaseID}?p=${pageID}`
copy(pageURL)

Install notion-add-task-with-content

let notionToken = await env('NOTION_USER_TOKEN')
let databaseID = "3859b567fda3464ea5a69d5ccb56274b"
let content = await arg('Enter Task')
let {data} = await post(
'https://api.notion.com/v1/pages', {
"parent": { "database_id": databaseID },
"properties": {
"Name": {
"title": [
{
"text": {
"content": content
}
}
]
}
}
},
{
headers: {
Authorization: `Bearer ${notionToken}`,
"Content-Type": "application/json",
"Notion-Version": "2021-05-13"
}
})
let bodyContent = await arg('Enter Content')
let {bodyData} = await patch(
`https://api.notion.com/v1/blocks/${data.id}/children`, {
"children": [
{
"object": "block",
"type": "paragraph",
"paragraph": {
"text": [
{
"type": "text",
"text": {
"content": bodyContent
}
}
]
}
}
]
},
{
headers: {
Authorization: `Bearer ${notionToken}`,
"Content-Type": "application/json",
"Notion-Version": "2021-05-13"
}
})
let pageID = data.id.replace(/-/g, "");
let pageURL = `https://notion.so/${databaseID}?p=${pageID}`
copy(pageURL)

(I just noticed that I haven't updated the first script to pass the "Notion-Version" header, but it seems unnecessary for now.)

Discuss Post

Create a new Jekyll post with Script Kit

For years I've used Rake tasks to manage my Jekyll blog writing & publishing flow. I'm starting to migrate those to Kit. They were stolen from an old version of Octopress which itself hasn't had an update in five years, so it's time.

Here's the first, which creates a new Jekyll post. I figure this might be useful to others new to Kit who want to do stuff with files or using static site generators.

let {format} = await npm('date-fns')
let title = await arg('Name of post')
let file_title = title.toLowerCase().replaceAll(' ', '-')
let posts_path = '/Users/danielmiller/code/daniel-industries/_posts/'
let date = new Date()
let date_for_path = format(date, 'yyyy-MM-dd')
let date_for_yml = format(date, 'yyyy-MM-dd hh:mm:ss z')
let file_path = posts_path + date_for_path + '-' + file_title + '.markdown'
let file_contents = `---
layout: post
title: "${ title }"
excerpt:
date: ${ date_for_yml }
categories:
---
`
writeFile(file_path, file_contents)
exec(`code ${file_path} --new-window`)

The last line is a nice bonus I gave myself, opening the new file in a dedicated VS Code window.

Discuss Post