Action

Append to Ideas

Posted by ghawk.dev, Last update about 7 hours ago

Append ideas from individual drafts into a master Ideas draft, automatically organized by category and timestamped.

Setup

Create a master draft with # Ideas at the top, followed by any categories you’d like to use as ## headings. For example:

# Ideas

## Coding Projects
## Writing Ideas
## Music

You can freely add, remove, or rename categories at any time. The action automatically detects the ## headings in your Ideas draft, so no changes to the script are required.

When you run the action, you’ll be prompted to select a category. Your idea will then be appended beneath that category with a timestamp, and the original draft will automatically be archived.

Important: Configure the Ideas Draft

You must enter the UUID of your master Ideas draft in line 2 of the script.

To find the UUID:

  1. Right-click your master Ideas draft and select Get Info.
  2. Copy the draft’s UUID.
  3. Right-click this action and select Edit.
  4. In the Script step, paste the UUID into the indicated location on line 2.

Once configured, the action will automatically keep your ideas organized as you add, remove, or rename categories.

Steps

  • script

    // UUID of your Ideas draft
    let ideasUUID = "039CEFFE-7E9B-4505-93CA-8DE3AE2F6DF5";
    
    // Find the Ideas draft
    let destination = Draft.find(ideasUUID);
    
    if (!destination) {
        app.displayErrorMessage("Couldn't find the Ideas draft.");
        context.fail();
    }
    
    // Find all level-2 Markdown headings (## Heading)
    let headingRegex = /^##\s+(.+)$/gm;
    let categories = [];
    let match;
    
    while ((match = headingRegex.exec(destination.content)) !== null) {
        categories.push(match[1].trim());
    }
    
    // Make sure we actually found some sections
    if (categories.length == 0) {
        app.displayErrorMessage(
            "No ## sections were found in your Ideas draft."
        );
        context.fail();
    }
    
    // Create the category picker
    let p = new Prompt();
    
    p.title = "Add Idea";
    p.message = "Where should this idea go?";
    
    p.addSelect(
        "category",
        "Section",
        categories,
        [categories[0]],
        false
    );
    
    p.addButton("Add Idea", null, true);
    
    // Show the prompt
    if (!p.show()) {
        context.cancel();
    }
    
    // Get selected category
    let category = p.fieldValues["category"][0];
    
    // Format timestamp
    let timestamp = strftime(
        draft.createdAt,
        "%B %-d, %Y — %-I:%M %p"
    );
    
    // Build the idea entry
    let entry = `### ${timestamp}
    
    ${draft.content}
    
    ---
    
    `;
    
    // Find the selected section
    let heading = `## ${category}`;
    let headingIndex = destination.content.indexOf(heading);
    
    if (headingIndex == -1) {
        app.displayErrorMessage(
            `Couldn't find the "${category}" section.`
        );
        context.fail();
    }
    
    // Find the next ## heading
    let nextSectionIndex = destination.content.indexOf(
        "\n## ",
        headingIndex + heading.length
    );
    
    let insertPosition;
    
    if (nextSectionIndex == -1) {
        // Selected section is the last section
        insertPosition = destination.content.length;
    } else {
        // Insert before the next section
        insertPosition = nextSectionIndex;
    }
    
    // Insert the idea
    destination.content =
        destination.content.slice(0, insertPosition) +
        "\n\n" +
        entry +
        destination.content.slice(insertPosition);
    
    // Save the Ideas draft
    destination.update();
    
    // Archive the original capture
    draft.isArchived = true;
    draft.update();
    
    app.displaySuccessMessage(`Added to ${category}`);

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.