The Import Actions Action Sequence

If any of you have tried to run my old Batch Import action since Drafts 3.5 was released (or any other recursive actions which previously looped inside of Drafts until an error occurred when they ran into a blank draft), you will probably have run into the annoying problem of facing an infinite loop of Drafts attempting to import a blank draft, failing to do so, and trying again anyways. Stopping the loop required exiting the app via the home button (or a five finger pinch on the iPad), then opening the multitasking menu and force quitting Drafts. You could try to do it by going straight to the multitasking menu from Drafts, but unless you move really, really quickly, Drafts will force close the multitasking menu instead of it force closing Drafts. This issue was caused because of a new feature in Drafts 3.5 which allowed you to run actions on blank drafts. Overall, this feature is a great addition, as it makes a number of other new options possible, but as a result, we'll need to find a new work around for our now problematic looping actions. I spent a while debating how to get around this issue so that Batch Import, and other recursive actions, could work again, and I have finally reached what I think is an acceptable solution. It requires a tiny bit of extra work, but it's worth it to allow recursive actions to function again.

My solution will require a whole new set of actions for the Batch Import sequence, so feel free to throw your old ones away if you still have them. Basically, it works by adding an extra line of text onto the end of each set of two code lines which define an action and its URL. This third line will specify the title of the next action to run on your list. For importing URL actions, we still use the first action I ever added to The Action Page: The importURL action. This action uses Drafts' built in "/import_action?" URL scheme to take the first two lines of a draft and import them as a new URL action. Here's the code for importURL:

drafts://x-callback-url/import_action?type=URL&name=[[title]]&url=[[body]]

Pretty straight forward, importURL uses the first line of your draft as the title of the new action to import, and the second line as the URL. The Import Actions action, just like the old Batch Import action, will chop the first two lines out of your list of actions, call importURL to import them as a new action, and then pull the rest of your list into the next action sequence via its x-success parameter. However, there's one final caveat in the new action sequence. Whereas the old sequence would chain back to itself automatically, importing the next set of actions without any further direction, the new sequence's x-success action is not hard coded. Instead, the x-success action is defined as [[line|4]], the forth line in your list of actions. Here's the code to check out if that's not clear:

drafts://x-callback-url/create?text=[[line|1..2]]&action=importURL&x-success={{drafts://x-callback-url/create?text=[[line|5..]]&action=[[line|4]]}}

So this action takes the first two lines of your draft and runs importURL on them. Then it makes a new draft using the fifth and further lines from your first draft, and runs an action on the new draft defined by line 4 in the old draft. This method solves the issue with an infinite loop because the last item in your list of actions will not have a forth line, so Drafts will not even attempt to run another action after importing the final action in your list. Not only does this fix the infinite looping issue, but it also gives the entire sequence a more graceful end by not causing an error to occur, which you then have to dismiss, after it finishes importing everything. If you're not quite sure how this would look, here's an example of the code you might have in a draft that you run this on:

Cross Post
drafts://x-callback-url/create?text=[[draft]]&action={{Post to App.net}}&x-success={{drafts://x-callback-url/create?text=[[draft]]&action=Tweet%3A%20the_axx}}

Import Actions
Triple Cross Post
drafts://x-callback-url/create?text=[[draft]]&action={{Post to Facebook}}&x-success={{drafts://x-callback-url/create?text=[[draft]]&action=Cross%20Post}}

Import Actions
Quadruple Cross Post
drafts://x-callback-url/create?text=[[draft]]&action={{Cross Post}}&x-success={{drafts://x-callback-url/create?text=[[draft]]&action=Post%20to%20FB%20and%20G%2B}}

If you run Import Actions on this sequence, it will first import the Cross Post action using the first two lines (a line is defined as everything before a line break, so even if your code wraps to the next line, as long as you don't have a newline in the middle of it, it will be treated as one line.), then create a new draft using line 5 and every line after (so the new draft created will start with the line reading "Triple Cross Post") and finally it will run the action defined by line 4. Since line 4 says "Import Actions", it will call back to itself and run again. This time, it will import Triple Cross Post, create a new draft starting with "Quadruple Cross Post", and chain back to itself again. Finally, Quadruple Cross Post will be imported. This time, however, since there are no further lines, Drafts will create a new blank draft and, since there is no fourth line after the Quadruple Cross Post action, it will not even attempt to run an action on it. The sequence will end and your actions will be imported.

One final note, if you currently use the importURL action for importing new actions, keep in mind that this sequence will work perfectly fine on a singular action title and URL that you want to import, so you can keep the clutter down by hiding the importURL action from your action lists and just using "Import Actions" for all URL importing purposes.

Below are direct import links for the two necessary actions. If you already have the old importURL action installed, it hasn't changed at all, so as long as you haven't changed the name or the code, it will still work fine.

Direct Import Link for importURL action.

Direct Import Link for Import Actions action.