Action

Post Morning Quote

Posted by FlohGro, Last update almost 2 years ago - Unlisted

created by @FlohGro / more on my Blog

Post Morning Quote

This Action was created from this request in the Drafts forums: link

It implements the requested behavior

[Configuration]

Open the script step of the action and edit it.
You need to insert the UUIDs for the „Quotes To Use“ and „Used Quotes“ draft in the variables there

Additionally you can change the action that should be used to post the quotes (by default it uses the „post draft“ action from Draftodon Action Group.

Default behavior

If you run this Action it will (try to) post the next quote from the Quotes To Use Draft to your Mastodon Account configured in Draftodon Action Group. The Action will pull the next Quote from the Quotes to use Draft, remove it from there and add it to the Used Quotes Draft with a Date stamp. Then the Action will use the Draftodon „post draft“ Action to post the quote with the hashtag „#Morning Quote“ to your Mastodon Account.


If you find this useful and want to support me you can donate or buy me a coffe

Buy Me A Coffee

Steps

  • script

    // morning quote automation
    
    // request in Drafts forum: https://forums.getdrafts.com/t/using-draftoton-to-automatically-post-a-quote-every-day/13997?u=flohgro
    
    // replace UNDEFINED with the UUID of the Draft where you store the Quotes that shall be posted with this action.
    // to retrieve the UUID you need to go into the informations of a draft by e.g. long press / right click on the draft and select "Get Info" - copy the UUID from there
    const quotesToUseUUID = "UNDEFINED"
    
    // replace UNDEFINED with the UUID of the Draft where you store the used Quotes that were posted with this Action
    const quotesUsedUUID = "UNDEFINED"
    
    // this hashtag will be added at the bottom of the post; please include the hashtag symbol
    const hashtagToAppendToThePost = "#MorningQuote"
    
    // insert the name of the action that should post the quote to mastodon you can select the Actions from Draftodon Action group e.g. post draft or schedule draft
    const nameOfMastodonPostActionToUse = "post draft"
    
    // if the action should use the bottom quote of your Quotes to use draft, set this variable to true
    const useBottomQuote = false
    
    // ---------------------------------------------------------------
    // END OF USER CONFIGURATION; CHANGES BELOW MIGHT BREAK THE ACTION
    // ---------------------------------------------------------------
    
    function getNextQuote() {
        let d = Draft.find(quotesToUseUUID)
        if (!d) {
            return undefined
        }
        // split by two return characters
        let splits = d.content.split("\n\n")
        // remove first split, this is the title of the Draft
        splits.shift()
        splits = splits.filter((split) => {
            return split.trim().length > 0
        })
        if (useBottomQuote) {
            return splits[splits.length - 1]
        } else {
            return splits[0]
        }
    }
    
    function addTextToContentOfDraftWithUUID(text, uuid) {
        let d = Draft.find(uuid)
        if (!d) {
            alert("ERROR:\nNo Draft was found with given UUID\"" + uuid + "\" please ensure the correct UUID is configured in the script")
            return false
        }
    
        // draft was found, insert the text below the title of the draft with a date stamp
        let date = new Date()
        let dateStr = date.getDate()
        if (date < 10) {
            dateStr = "0" + date
        }
        let month = date.getMonth() + 1
        let monthStr = month
        if (month < 10) {
            monthStr = "0" + month
        }
        let year = `${date.getFullYear()}`
        year = year.slice(2, 4)
        let dateString = dateStr + "/" + monthStr + "/" + year
        let textToInsert = "\n" + dateString + ": " + text.trim()
        d.insert(textToInsert, 1)
        d.update()
        return true
    }
    
    function removeTextFromContentOfDraftWithUUID(text, uuid) {
        let d = Draft.find(uuid)
        if (!d) {
            alert("ERROR:\nNo Draft was found with given UUID\"" + uuid + "\" please ensure the correct UUID is configured in the script")
            return false
        }
        let curContent = d.content
        let newContent = curContent.replace(text, "")
        newContent = newContent.replace("\n\n\n", "\n")
        d.content = newContent;
        d.update()
        return true
    }
    
    function getConfiguredAction() {
        let action = Action.find(nameOfMastodonPostActionToUse)
        if (!action) {
            alert("ERROR:\nCouldn't find the defined action \"" + nameOfMastodonPostActionToUse + "\" - please make sure that the action exists; no quote will be posted and the Drafts with your quotes remain untouched")
        }
        return action
    }
    
    function loadQuotesToUseDraft(){
        let d = Draft.find(quotesToUseUUID)
        if (d){
            editor.load(d)
        }
    }
    
    function run() {
        // check if UUIDs are defined
        if (quotesToUseUUID == "UNDEFINED" || quotesToUseUUID == "" || quotesUsedUUID == "UNDEFINED" || quotesUsedUUID == "") {
            alert("ERROR:\nAt least on Draft for Quotes to use or for the used Quotes is not defined\nplease insert the UUIDs into the script step of the Action")
            return
        }
        // check if configured Action exists and retrieve it
        let actionToRun = getConfiguredAction()
        if (!actionToRun) {
            // undefined action we need to abort
            return
        }
    
    
    
        let nextQuote = getNextQuote()
    
        if (!nextQuote) {
            // no quote was returned, maybe the quote draft is empty
            alert("ERROR:\nCould not retrieve a valid Quote from the Quotes To Use Draft. please check if it is empty and if the Quotes are stored in the expected format")
            loadQuotesToUseDraft()
            return
        }
        // next quote is returned, prompt the user if this is a valid quote that shall be posted
        let approvePrompt = new Prompt()
        approvePrompt.title = "confirm to use this quote"
        approvePrompt.message = nextQuote
        approvePrompt.addButton("Confirm")
        if (!approvePrompt.show()) {
            // user cancelled the prompt, nothing to be done
            app.displayInfoMessage("cancelled posting quote")
            return
        }
        // user confirmed the usage of this quote, we need to remove it from the quotes to use draft and add it to the quotes used draft
        if (!addTextToContentOfDraftWithUUID(nextQuote, quotesUsedUUID)) {
            // failed adding text to the draft
            return
        }
        // quote was added to the Used Quotes Draft, remove it from the quotes to use draft
        if (!removeTextFromContentOfDraftWithUUID(nextQuote, quotesToUseUUID)) {
            // now we have a mess!
            alert("ERROR:\nThe quote was added to the Used Quotes Draft, but could not be removed from the Quotes to Use Draft.\nThis should not happen normally. Please clean up the mess by removing the quote from the Quotes to use Draft by hand.\n\nThe Action will continue to post the new quote to Mastodon nevertheless")
        }
        // quote was (hopefully) removed from the Quotes to Use Draft; now create a new Draft with the new quote and the hashtag that shall be used
        let postDraft = new Draft()
        postDraft.content = nextQuote.trim() + "\n\n" + hashtagToAppendToThePost
        postDraft.update()
        editor.load(postDraft)
    
        // the draft was created, run the defined action on that draft
    
        app.queueAction(actionToRun, postDraft)
    }
    
    run()

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.