Thanks alastair for this great action.
I’ve switched over to it from using Siri Shortcuts that I had setup to do something similar.
Shortcuts is great, however I sometimes get errors, and this is noticeably quicker!
I had to tweak the match part of the script when finding the UUID from the output of the dayone2 cli.
It wasn’t opening day one and on further inspection I noticed that it was only returning “C” being the first letter of the output. My updated part is:
// Extract the UUID of the new Day One entry from the standard output
let uuid = runner.standardOutput.match(/[0-9a-fA-F]{32}/)
Your script made me open up to learning a bit more about the drafts scripting, so thanks for the inspiration.
I had added in a journal option in a slightly different method to yours. Specifying the journal as default an adjusting the name. However I like your idea of if not uncommented then apply the default.
It led me to see if there was a way to specify my journals in a list and choose from one.
I put this together (with the help of ChatGPT, which has been great for learning scripting)
For anyone who might find this helpful. Insert this at the top of the script:
// Define the journal names
let journalNames = ["Journal", "Notes", "Ideas", "Work notes", "How To"];
// Create a prompt for journal selection
let p = Prompt.create();
p.title = "Select Journal";
p.addSelect("journal", "Journal", journalNames, [], false);
p.addButton("OK");
// Show the prompt and get the selected journal
let didSelect = p.show();
let journal;
if (didSelect) {
journal = p.fieldValues["journal"];
} else {
journal = "Journal"; // Default journal name
}
And amend the dayone2 cli call part with:
--journal "${journal}" -- \
I also thought to try and resolve your TODO regarding different output text name.
I wondered if I could use the uuid of the draft as the file name. After some trial and error I think I got there.
For anyone interested, here’s how I used perplexity.ai to try to get to the bottom of this:
use uuid of drafts note for temp text file output
This is the entirety of the updated script that works for me:
// Define the journal name
let journal = "Log";
// Define draft uuid
let draftUuid = draft.uuid;
// Create a local file manager
let fmLocal = FileManager.createLocal();
// Write the content of the current draft to a local file
let filePath = "/" + draftUuid + ".txt";
fmLocal.writeString(filePath, draft.content);
// Format the creation date of the draft to ISO 8601 format, removing milliseconds
let dt = draft.createdAt.toISOString().replace(/\.\d+Z$/, "Z");
// Combine the draft's tags with the tag "drafts"
let tags = ["drafts"].concat(draft.tags);
// Construct the shell script to create a new entry in Day One
// The script uses the dayone2 CLI and the content of the local file
// The entry is tagged, geotagged, and timestamped according to the draft
let script = `#!/bin/bash -eu
/usr/local/bin/dayone2 \
--isoDate=${dt} \
--coordinate ${draft.createdLatitude} ${draft.createdLongitude} \
--tags ${tags.join(" ")} \
--journal "${journal}" -- \
new <${fmLocal.basePath}${filePath}`
// Create a shell script runner and execute the script
let runner = ShellScript.create(script);
if (!runner.execute()) {
throw new Error(`stdout:${runner.standardOutput}\nstderr:${runner.standardError}`)
}
// Extract & Open the UUID of the new Day One entry from the standard output
let dayOneUuid = runner.standardOutput.match(/[0-9a-fA-F]{32}/)
if (dayOneUuid.length) {
draft.setTemplateTag('d1uuid', dayOneUuid[0]);
app.openURL(`dayone://view?entryId=${dayOneUuid[0]}`);
}
Thanks again alastair for the inspiration!