Action

Selection to Mastodon

Posted by David Blue, Last update about 1 year ago

Post selected text to Mastodon (if no text is selected, the entire draft contents will be passed) and return the resulting status’ URL as a markdown-formatted hyperlink.

This is a modified version of the Post to Mastodon (Script) Action shared by agiletortoise.

As such, this advisory applies:

IMPORTANT: Before using this action, edit the first to “Define Template Tag” steps to enter the Mastodon instance host and credential identifier to use.

The differences/additions are as follows:

  1. If any text is selected at run, only the selection will be passed as the status content. For my own uses, this is/has long been a must, as I use an assigned keyboard shortcut to call this (and other social posting) action(s.)
  2. By default, the URL to the resulting status (Ex:https://mastodon.social/@DavidBlue/109950043272210317) is inserted in Markdown format after a line break, following a hyphen and a space (to form an unordered list item.) The text of the hyperlink is Mastodon out of the box, but this can easily be modified by editing the final Define Template Tag action step.
  3. Said status URL is also copied to the system clipboard.
[selected text]
- [Mastodon](https://mastodon.social/@DavidBlue/109950215153070513)

Steps

  • defineTemplateTag

    name
    mastodon-host
    template
    mastodon.social
  • defineTemplateTag

    name
    credential-id
    template
    Mastodon
  • defineTemplateTag

    name
    status
    template
    [[selection]]
  • script

    // grab host-id from tags
    let host = draft.processTemplate("[[mastodon-host]]")
    let credentialID = draft.processTemplate("[[credential-id]]")
    
    // define a variable with the contents of the selection (or the entire draft contents if no text is selected)
    
    let mastostatus = draft.processTemplate("[[status]]")
    
    // validate values
    if (host.length == 0 || host == "your.mastodon") {
    	alert(`Mastodon host must be configured to run this action.
    
    Edit action and set your Mastodon host name (like "mastodon.social") as the template value in the first "Define Template Tag" action step.
    `)
    	context.cancel()
    }
    
  • script

    // create Mastodon instance
    let m = Mastodon.create(host, credentialID)
    
    // post to the /statuses endpoing
    let path = "/api/v1/statuses"
    let data = {
    	"status": mastostatus,
    	"visibility": "public"
    }
    let response = m.request({
      "path": path,
      "method": "POST",
      "data": data
    })
    
    if (!response.success) {
    	console.log(`Post Failed: ${response.statusCode}, ${response.error}`)
    	context.fail()
    }
    
    // define a template tag with the resulting status's URL
    else {
    	console.log(`Posted to Mastodon: ${response.responseData["url"]}`)
    	draft.setTemplateTag("mastolink", response.responseData["url"])
    }
  • clipboard

    template
    [[mastolink]]
  • insertText

    template
    [[status]]
    - [Mastodon]([[mastolink]])

Options

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