Command Line STDIN for Drafts

I don’t think I’ve asked this before - and a search doesn’t reveal the answer:

Is it feasible to pipe into a new Drafts draft from the command line?

Something like

ls - l | drafts

I suppose, likewise, one might might want to emit from Drafts.

More sophisticated might be to append to the latest draft.

I know I can create some kind of automation to do this via the clipboard. (Which might make some variant of the technique available on iOS or iPad OS, too.)

Sort of…not super elegant, but you can capture STDIN to a temp file and route it contents through an AppleScript. I have the follow shell script saved as “drafts” to do it:


STANDIN=$(mktemp /tmp/seereport.XXXXXXXXXXXX) || exit 1
cat > $STANDIN

/usr/bin/osascript > /dev/null <<ASCPT
    set stdinText to do shell script "cat $STANDIN"
    tell application "Drafts"
        make new draft with properties {content:stdinText}
    end tell

trap 'rm -f $STANDIN' EXIT

Then I can call the below to create a draft with the STDIN output:

ls -l | ./drafts

In case it is of use, here’s some scripts I shared a while back.

1 Like

Thank you @agiletortoise and @sylumer!

I thought the only way was to use a temporary file. (With my performance hat on I’m a little leery of temporary files but my use case is unlikely to involve a temporary file of more than a very few megabytes.)

It occurs to me that I could write eg Python with command line parameters to control this (or add value).

For example, adding tags or running an action.

(Rewriting - given your sample code - is only if I feel my coding is best done in another language.)

My assumption was that for Drafts to be built cross-platform it might be tough to support command line on Mac and other interface paradigms on iOS - but of course I don’t know the code.