Start Day Script - plays music, arranges windows, starts timer and creates a note

Start Day Script

I wrote a script that:

  1. Plays "good morning" audio clip from Ironman
  2. Arranges windows
  3. Starts time entry in Toggl Track
  4. Creates a new note in Simplenote

start-personal.js

//Menu: Start Day
//Description: My script for starting my day before work
//Author: Joe Previte
//Twitter: @jsjoeio
/* Note about this script
- it assumes the applications are already open (I open them at login)
*/
// -------------------------
// Music - Start
// -------------------------
let goodMorningFileName = `jarvis-good-morning.mp3 `
await run("play-audio-file", goodMorningFileName)
// -------------------------
// Music - End
// -------------------------
// -------------------------
// Window Management - Start
// -------------------------
await run("press-keyboard-shortcut", "Spotify", ";", "command,control")
// In order to make it take 2/4ths I have to run the command three times
await run("press-keyboard-shortcut", "Simplenote", "l", "command,control")
await run("press-keyboard-shortcut", "Simplenote", "l", "command,control")
await run("press-keyboard-shortcut", "Simplenote", "l", "command,control")
await run("press-keyboard-shortcut", "Toggl Track", "j", "command,control")
// -------------------------
// Window Management - End
// -------------------------
// -------------------------
// Time Tracking - Start
// -------------------------
await run("start-toggl", "checking in with myself", "personal catchall")
// -------------------------
// Time Tracking - End
// -------------------------
// -------------------------
// Note Taking - Start
// -------------------------
await run("new-daily-note")
// Toggle focus mode
await run("press-keyboard-shortcut", "Simplenote", "f", "command,option")
// -------------------------
// Note Taking - End
// -------------------------

play-audio-file.js

//Menu: Play audio file
//Description: Plays an audio file in the background
//Author: Joe Previte
//Twitter: @jsjoeio
const AUDIO_FILE_DIR = `~/Documents/audiofiles`
let audioFileName = await arg(`What is the name of the audio file?`)
exec(`afplay ${AUDIO_FILE_DIR}/${audioFileName} &>/dev/null &`)

press-keyboard-shortcut.js

// Menu: Press Keyboard Shortcut
// Description: Presses a keyboard shortcut
/**
* @description Presses a keyboard shortcut
* @param {string} application - the application form which this keyboard shortcut should be run
* @param {string} key - a single key such as "j" or "q"
* @typedef {('command'|'control'|'option')} Command - 'command' | 'control | 'option'
* @param {Command[]} commands - an array of commands.
* @example pressKeyboardShortcut("j", ["command", "control"]) would press `j + ⌘ + ^`
*/
async function pressKeyboardShortcut (application = "", key, commands = []) {
const formattedCommands = formatCommands(commands)
// Note: we have to activate an application first in order to use this script with it
// Otherwise, it will run the keyboard shortcut on Script Kit
return await applescript(
String.raw`
activate application "${application}"
tell application "System Events"
keystroke "${key}" using {${formattedCommands}}
end tell
`
)
}
function formatCommands(commands = []) {
// This will turn ["control", "command"]
// into this "control down, command down,"
// and then slice the last commma
return commands.map(command => `${command} down,`).join(" ").slice(0, -1)
}
// Example: "Toggl Track"
let application = await arg(`What application should this be run with?`)
// Example: j
let key = await arg(`What is the key?`)
// Example: command,control
let unformattedCommands = await arg(`What are the commands? Separate by comma.`)
let commands = unformattedCommands.split(",")
console.log('run with ', application, key, commands)
await pressKeyboardShortcut(application, key, commands)

start-toggl.js

let btoa = await npm("btoa")
let TOGGL_TOKEN = await env("TOGGL_TOKEN") //Go here -> https://track.toggl.com/profile
let wid = await env("WORKSPACE_ID") //The number in your URL, e.g. 5198420 -> https://track.toggl.com/projects/5198420/list
let description = await arg(`What are you working on?`)
let project = await arg('Select project:', [
'Coder',
'personal catchall',
'vim for vscode',
])
// This is the project id for "Coder"
// e.g. 165657166 -> https://track.toggl.com/2151494/projects/165657166/team
const projectIds = {
'Coder': "165657166",
"personal catchall": "165660677",
"vim for vscode": "165340149"
}
// Defaults to personal catchall
const pid = projectIds[project] || "165340149"
const response = await post(
"https://api.track.toggl.com/api/v8/time_entries/start",
{
time_entry: {
pid,
wid,
description,
created_with: "Script Kit",
},
},
{
headers: {
"Content-Type": "application/json",
Authorization:
"Basic " + btoa(`${TOGGL_TOKEN}:api_token`),
},
}
)

new-daily-note.js

//Menu: New daily note
//Description: Create a new daily note in Simplenote
//Author: Joe Previte
//Twitter: @jsjoeio
let {format} = await npm('date-fns')
let date = format(new Date(), 'yyyy-MM-dd')
let dayOfWeek = format(new Date(), "eeee")
await run("press-keyboard-shortcut", "Simplenote", "n", "command")
await applescript(
String.raw`
activate application "Simplenote"
tell application "System Events"
keystroke "${date}" & return & "Happy ${dayOfWeek} :)" & return & return & "Today I'm feeling grateful for:" & return & "1." & return & "2." & return & "3."
end tell
`
)