Action

Save listed drafts as files

Posted by ipsum, Last update almost 2 years ago

UPDATES

almost 2 years ago

Default ISO 8601 date in filename to off

Exports the currently visible list of drafts to a folder, using the “title” (first line) as the file name. Checks that all drafts have short-ish titles first. Sets the created and modified file timestamps to match the draft’s metadata.

See the first few lines for settings.

Aborts if the drafts list isn’t on screen.

Steps

  • script (macOS only)

    // Settings
    const tag_when_done = false; // handy, but counts as a draft modification
    const max_title_length = 70;
    const prefix_iso_date = false;
    
    // Script
    function export_title(dft) {
        iso_date = prefix_iso_date ? dft.processTemplate('[[created|%Y-%m-%d]] ') : "";
        return iso_date + dft.processTemplate('[[safe_title]]');
    }
    
    if (app.isDraftListVisible) {
        let bk = Bookmark.findOrCreate("Export pit")
        let fmBk = FileManager.createForBookmark(bk);
    
        let draftsGroup = app.currentWorkspace.query("inbox");
    
        overlong_titles = [];
        draftsGroup.forEach(function(dft) {
            title = dft.processTemplate('[[safe_title]]');
    
            if (title.length > max_title_length) {
                console.log("Overlong title: " + title);
                overlong_titles.push(draft);
            }
        });
    
        overlong_count = overlong_titles.length
    
        if (overlong_count == 0) {
            // Get the ok
            let subsetLength = Math.min(draftsGroup.length, 10);
            let subset = draftsGroup.slice(0, subsetLength);
            let subset_titles = subset.map(function(dft) {
                return export_title(dft);
            });
            let preview_titles = subset_titles.join("\n")
    
            let p = Prompt.create();
    
            p.title = "Export confirmation";
            p.message = "About to export " + draftsGroup.length + " draft(s). Titles include: \n\n" + preview_titles;
            p.addButton("Export");
            p.isCancellable = true;
    
            let proceed = p.show();
    
            if (proceed) {
                export_tag = "exported-" + Date.now()
                draftsGroup.forEach(function(dft) {
                    title = export_title(dft);
                    filename = title + '.md';
                    fmBk.writeString(filename, dft.content);
                    fmBk.setCreationDate(filename, dft.createdAt);
                    fmBk.setModificationDate(filename, dft.modifiedAt);
                    if (tag_when_done) {
                        dft.addTag(export_tag);
                        dft.update();
                    }
                });
            } else {
                context.cancel("User canceled");
            }
        } else {
            app.displayErrorMessage("Overlong titles: " + overlong_count + ". See Action Log for list.");
            context.fail("Overlong titles found.");
        }
    
    } else {
        context.cancel("Draft list is not visible. Aborting for safety.");
    }
    
    script.complete();

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.