Editorial Workflows

MultiTweet

public workflow

Install Workflow...

This workflow contains at least one Python script. Only use it if you trust the person who shared this with you, and if you know exactly what it does.

I understand, install the workflow!

This is a workflow for Editorial, a Markdown and plain text editor for iOS. To download it, you need to view this page on a device that has the app installed.

Description: Brings up action sheet to tweet selected content. If selection is longer than 140 characters, it is broken up into multiple, appropriately sized smaller tweets. Each tweet includes a tweet counter to indicate to followers that it is part of a series of tweets.

Comments: Comment Feed (RSS)

There are no comments yet.

+ Add Comment

Workflow Preview
If… ?
Run the block if
Selected Text
  • is Equal to
  • is Not Equal to
  • Contains
  • Doesn't Contain
  • Matches Regular Expression
Stop ?
Stop
  • This Workflow
  • Repeat Block
Show HUD Alert
OFF
Message
Stopped
…End If
Selected Text ?
Entire Line(s)
OFF
Empty Selection Output
  • No Output
  • All Text
  • Closest Word
Folded Text
  • Include
  • Replace with:
Run Python Script ?
Source Code
# Andrew Clark
# @acdlite

#coding: utf-8
import re
import workflow

# Functions
def split_tweets(content, placeholder='x'):
   tweets = []
   c = content
   while len(c) > 0:
       tweet_number = " ({0}/{1})".format(len(tweets)+1, placeholder)
       force_suffix = False

       if len(c) <= 140-len(tweet_number):
           suffix = tweet_number
           force_suffix = True
       else:
           suffix = u'\u2026' + tweet_number 

       if len(tweets) == 0:
           t, l = smart_truncate(c, 140, suffix)
           tweets.append(t)
       else:
           t, l = smart_truncate(c, 139, suffix, force_suffix)
           tweets.append(u'\u2026' + t.lstrip())

       c = c[l:].rstrip()
   return tweets

def smart_truncate(content, length=100, suffix='...', force_suffix=False):
   if  len(content) <= length and not force_suffix:
       return (content, len(content))
   elif len(content) <= length-len(suffix) and force_suffix:
       return (content + suffix, len(content))
   else:
       c = ' '.join(content[:length-len(suffix)].split(' ')[0:-1])
       return (c + suffix, len(c))

# Body

action_in = workflow.get_input()

tweets = []
placeholder = 'x' # Used in body of tweet; will be replaced with total number of tweets
cont = True

while cont:
   tweets = split_tweets(action_in, placeholder)
   # If the number of digits in total number of tweets exceeds length of placeholder
   # try again with incremented placeholder length
   cont = len(str(abs(len(tweets)))) > len(placeholder)
   if cont:
       placeholder = ''.join(['x' for i in range(len(placeholder)+1)])
       
# Replace placeholder with total number of tweets
reg = re.compile("{0}\)$".format(placeholder))
tweets = [reg.sub("{0})".format(len(tweets)), t) for t in tweets]

#TODO: Generate the output...
action_out = '{tweet}' + '\n{tweet}'.join(tweets)

workflow.set_output(action_out)
Repeat… ?
Repeat with
  • Every Line
  • Every Match of Regular Expression:
^\{tweet\}.*$
Match Group
0 (entire match)
Reverse
OFF
Find / Replace ?
Find
^\{tweet\}
  • Case-insensitive (A = a)
  • Case-sensitive (A ≠ a)
  • Regular Expression
Replace with
Compose Tweet ?
Initial Text
Input
Stop if Canceled
ON
…End Repeat