Posting to WordPress With Workflow

I am trying to move my blogging activity, which takes place entirely on MacBook to my iPad Pro. To be honest, image capturing and loading are a real pain for me right now. The first problem I have is that, the links of Dropbox images are not directly usable in HTML documents. This may be an important design choice for Dropbox but it creates an unnecessary problem for bloggers. To make it available inside an HTML document, Canton Becker pointed a solution. This is a classic case for a Workflow.

Parameter Replacement

The solution he proposed and validated by the Dropbox document, is that, we need to replace the “dl=0” parameter with “raw=1”. My approach involves using regular expressions. In fact, this can be accomplished without using them but there is a catch. If there exists more than one place inside the URL where “dl=0” occurs, then they are also replaced with “raw=1”, which is not our purpose.

The regular expression (.*)\?dl=0$ represents the parameter dl that is at the very end of the URL. The other characters in the URL are grouped within the parenthesis. We can access that part by $1. The replacement string, $1\?raw=1 puts those characters and after them inserts the raw parameter set to 1.

Matryoshka Workflows

Now we have the image link, what will we do with it? Generally, I shorten URLs whenever I place them in my post. So, we can apply my previous workflow When I first did that, the thing that came to my mind was: Can’t I call another workflow within a workflow?

The simple answer is yes, but not directly. The long answer is below:

Run Workflow Run

As far as I know, we cannot directly call another workflow. However, we can open URLs. A workflow can be accessed via workflow:// URL scheme, right? Moreover, that URL scheme enables us to run any workflow. All we need to provide is the name of the workflow and the input to it. So why not try it? In my case, the name of the workflow to be run is “bit.ly Shorten”. I encode it as an URL and persist it inside the variable bitUrl. The modified Dropbox link is my input. I can also store it as a variable and feed as a text to workflow run scheme but preferred to use the clipboard. The Open URLs step completes my action set. You can get the workflow from here

Editor Automation

1Writer and Drafts4 really improved my blogging efficiency. That triggered my intention to write and share more. With that in mind, I was thinking if there exists a method to publish to WordPress directly from 1Writer and Drafts4. Within both apps, there is none. But, with Workflow, this becomes a reality. What to do with Workflow other than loving it?

A quick googling took me to one my favorite bloggers, without a surprise. In his article, Federico Viticci talks about the very same subject. The main point is that, there can be actions defined in 1Writer and Drafts4 which get the necessary information from the document and use them to call a Workflow script. Since the Workflow call with parameters inside the URL are independent of the action side, we can use the same Workflow either from 1Writer or Drafts4.

I use atx-style headers, so my text page the editor starts with the header, preceded by a # character. This line is succeeded
by two new line characters and the rest is the body of the post. Below is the very start of this post.

# Posting to WordPress With Workflow

I am trying to move my blogging activity, which takes place entirely on MacBook to my iPad Pro. To be honest, image capturing and loading are a real pain for me right now. The first problem I have is that, the links of Dropbox images are not directly usable in HTML documents. This may be an important [design choice](http://bit.ly/1UtRR2h) for Dropbox but it creates an unnecessary problem for bloggers. To make it available inside an HTML document, Canton Becker [pointed a solution](http://bit.ly/1UtRzZv). This is a classic case for a Workflow.

1Writer Way

1Writer Action Creation

Let’s talk about the actions. Both 1Writer and Drafts4 permit us to create action scripts using JavaScript programming language. So, the JavaScript action of 1Writer version is:

var parameters = prepareBlogPostParameters();
var workflowName = 'Publish';
app.openURL('workflow://run-workflow?name=' + encodeURIComponent(workflowName) + '&input=' + encodeURIComponent(parameters));

// you can add as many parameters as you like
function prepareBlogPostParameters() {
var text = editor.getText();
var endOfTitle = text.indexOf("\n");
var title = text.substring(1, endOfTitle).trim();
text = text.substring(endOfTitle+1).trim();
var parametersAsJson = {
"title" : title,
// "newParameter" : value,
"text" : text
};
return JSON.stringify(parametersAsJson);
}

I definitely advise you to go through the 1Writer JavaScript Documentation because there are lots of important information about the capabilities of 1Writer’s automation. For our example, I will emphasis two basic objects. We see that the properties of the document can be accessed via the “editor” object. The method we used is the getText(), which supplies the whole text in the editor. We extract the title out of it, as the first sentence ending with a new line character.

We have the title and the text. We must send it to our publish workflow. All workflows accept one input parameter. I decided to pass these two variables by encapsulating them as a single JSON object. By the virtue of its key-pair values, our workflow can get the necessary information by using the “Get Value for Key” actions.

After all these preparations, we call the workflow to do its job. In 1Writer, there is no explicit Workflow integration. Rather, we use the generic openURL() method of the “app” object. I preferred the simple run-workflow approach. X-Callback-URL can also be used. I shared this if you would like to give it a try.

There are lots of actions that can be integrated into 1Writer. You can check them out and modify according to your needs.

Drafts4 Way

Drafts4 Action Creation

Initially, my thinking was that I could use the very same JavaScript in Drafts4 with little modifications. Since 1Writer and Drafts4 objects would be different, those modifications were necessary. But through the conversion process, the situation turned out to be more complex than that, as it is almost always the case. In 1Writer, there is only one action to be performed. Drafts4 divides the whole automation into action steps. For the time being, there are 26 different action steps. What we will use is one Script and one Run Workflow step.

Drafts4 Action

Action Steps

// Script steps run short Javascripts
// For documentation and examples, visit:
// http://help.agiletortoise.com
var parameters = prepareBlogPostParameters();
var workflowName = 'Publish';
draft.defineTag('parameters', parameters);
draft.defineTag('workflowName', workflowName);

// you can add as many parameters as you like
function prepareBlogPostParameters() {
var text = draft.content;
var endOfTitle = text.indexOf("\n");
var title = text.substring(1, endOfTitle).trim();
text = text.substring(endOfTitle+1).trim();
var parametersAsJson = {
"title": title,
// "newParameter": value,
"text" : text
};
return JSON.stringify(parametersAsJson);
}

Drafts4 has also a detailed documentation about its features. Moreover, you can find many actions in its own directory.

Our first step will again generate the JSON object. This is more or less the same as in 1Writer. To get the editor text, we use the “draft” object and its “content” property. When the parameters are set, we again generate the JSON object. Different from 1Writer, we need to preserve this JSON object and the name of the workflow. That is mandatory if we want to use these in following action steps. This is accomplished by defining tags on the draft object. I just defined tags for each of them. We remove the URL call to run the workflow, since it will be done in the latter step.

Workflow from Drafts4

The next action step includes getting the parameters and workflow name and then running that workflow. The tags that we he previous action step can be accessed by writing its name inside the [[]] operator. With this, we complete the whole action in two steps that follow each other. I also put this in the Action Directory. You can try it.

The Publish Workflow

Publish Part 1
Publish Part 2

The Publish workflow gets the title and the body text. As I said before, my aim was to create a contract or interface between the editors and the workflow. With that, the editors only concentrate on generating the input JSON parameter, which contains the title and the text body. Nothing else. They do not know anything about the later stages. The Publish workflow knows nothing about the editors either. The only thing that matters for it is the JSON parameter that contains the title and text body.

As the first step, the JSON parameter is converted into a dictionary by “Get Dictionary from Input” action of the workflow. This enables us to get the title and body text with their respective keys. The “Post to WordPress” action gets the text as its input to post and the title information to set the title. To me, Post as Type and Draft as Status are enough. You can change them according to your own needs.

You can get this workflow from here. Enjoy!

Advertisements

%d bloggers like this: