Editorial Workflows

Working Copy Workflow

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: Workflow for interacting with the Working Copy app. It allows to checkout file from within Working Copy as well as pull / push changes from within Editorial

Shared by: Working Copy Workflow

Comments: Comment Feed (RSS)

vehka — 10 Apr 2015
I can't make this workflow work. I have it communicating successfully with Working Copy after adding the url key, but when I try pushing or pulling, it fails with "file not in repository", switches momentarily to Working copy and back, then displays "multiple files at path". I've created a Repos folder in Editorial, and set the variables in the workflow to point to a certain repo and a certain directory inside that repo. So what I am doing wrong? Thanks in advance...
anonymous — 08 Jul 2015
A more detailed setup guide would be awesome!
anonymous — 15 Jul 2015
I second that, I'm having trouble figuring out how this functions at all, but would very much like to make it work.
Whenever I run the workflow, after putting in the key, whatever I press either push or pull, it just says file not in repo
anonymous — 21 Sep 2015
The "File not in any repository!" error happens when the open file is not inside any folder, that should represent a repository. There should be a "Repos" folder inside the Local Editorial file storage, and inside the "Repos" folder should be other folders, representing individual repositories. Only within such folders (such as Local/Repos/Project) can there be individual files and further subfolders, that can be synced via Working Copy.
Anders Borum — 15 Nov 2015
If you have trouble with this workflow, you could try the one bundled with Working Copy since October 2015.

When looking at a file inside Working Copy there will be a "Edit in Editorial" action available from the share-sheet. The first time this action is used, you will be asked to install a workflow that handles file transfer back and forth between Working Copy and Editorial.

This workflow sends files into Editorial with a special filename such that Working Copy knows where it belongs when you sent it back. When the workflow is called from Editorial it will send the file back to Working Copy in the correct location if it recognises the filename.

I am the developer of Working Copy.

+ Add Comment

Workflow Preview
Set Variable ?
Variable Name
input
Value
Input
Set Variable ?
Variable Name
key
Value
If… ?
Run the block if
key
  • is Equal to
  • is Not Equal to
  • Contains
  • Doesn't Contain
  • Matches Regular Expression
Stop ?
Stop
  • This Workflow
  • Repeat Block
Show HUD Alert
ON
Message
You have to set your Working Copy key first!
…End If
Set Variable ?
Variable Name
root
Value
Repos
Set Variable ?
Variable Name
repo
Value
Set Variable ?
Variable Name
path
Value
Run Python Script ?
Source Code
#coding: utf-8
import workflow, console, editor
import urllib, webbrowser
import os, json

def getOpenFile():
	
	path = editor.get_path()
	
	# If no open file
	if not path:
		console.hud_alert("No open file!", "error", 3)
		return None, None
	
	# If not in documents library
	path = os.path.realpath(path)
	if path.find(documents) == -1:
		console.hud_alert("File not local!", "error", 3)
		return None, None
	
	# If not in the Repo folder
	path = path[len(documents):]
	parts = path.split('/', 3)
	if parts[1] != root:
		console.hud_alert("File not in root " + root + " folder!", "error", 3)
		return None, None
		
	# If not in a unique repo
	if len(parts) == 3:
		console.hud_alert("File not in any repository!", "error", 3)
		return None, None
	
	return parts[2], parts[3]

def checkout():
	
	data = json.loads(workflow.get_variable('input'))
	repo, path = os.path.split(data['identifier'])
	
	# If the folders don't exist, create them
	ospath = os.path.join(documents, root, repo)
	if not os.path.exists(ospath):
		os.makedirs(ospath)
	
	# Write to the file	
	write = True
	if os.path.exists(os.path.join(documents, root, repo, path)):
		write = False
		action = console.alert("Overwrite?", "Do you really want to overwrite current file?", "OK")
		if action == 1:
			write = True
		
	if write:
		editor.set_file_contents(os.path.join(root, repo, path), data['text'], "local")

def pull():
	repo, path = getOpenFile()
	if repo and path:
		
		write = True
		if editor.get_text() != "":
			write = False
			action = console.alert("Overwrite?", "Do you really want to overwrite current file?", "OK")
			if action == 1:
				write = True
				
		if write:
			workflow.set_variable("repo", repo)
			workflow.set_variable("path", path)

def push():
	repo, path = getOpenFile()
	if repo and path:
		
		text = urllib.quote(editor.get_text().encode('utf-8'), safe='')
		success = "editorial://workflow-callback"
		
		callback = working_copy + 'write/?key=' + key +'&repo=' + repo + '&path=' + path + '&text=' + text + '&x-success=' + success
		
		webbrowser.open(callback)
		console.hud_alert("Repo updated!", "success", 3)

# General properties
key = workflow.get_variable("key")
root = workflow.get_variable("root")
documents = os.path.expanduser('~/Documents')
working_copy = 'working-copy://x-callback-url/'

# Decide on the basic action (checkout, push or pull)
data = workflow.get_variable("input")
if data:
	checkout()
else:
	action = console.alert("Pull or push?", "Do you want to push or pull from repo?", "Pull", "Push")
	if action == 1:
		pull()
	elif action == 2:
		push()
If… ?
Run the block if
repo
  • is Equal to
  • is Not Equal to
  • Contains
  • Doesn't Contain
  • Matches Regular Expression
Open x-callback URL ?
URL
working-copy://x-callback-url/read/?key=key&repo=repo&path=path
x-success
editorial://workflow-callback/?text=
Output
  • First Parameter of x-success URL
  • Raw x-success URL
Run Python Script ?
Source Code
#coding: utf-8
import workflow, editor, console
import os

contents = workflow.get_input()
root = workflow.get_variable("root")
repo = workflow.get_variable("repo")
path = workflow.get_variable("path")
editor.set_file_contents(os.path.join(root, repo, path), contents, "local")
…End If