Skip to content

Instantly share code, notes, and snippets.

@omz
Created December 16, 2012 04:25
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save omz/4303311 to your computer and use it in GitHub Desktop.
Save omz/4303311 to your computer and use it in GitHub Desktop.
Imports a workflow or script from a Gist URL in the clipboard into the commands menu.
This gist contains a workflow for Editorial, an app for
writing plain text and markdown on iOS.
To import the workflow, copy the *entire* text of the gist,
then open Editorial and create a new workflow.
------------ BEGIN WORKFLOW ------------
{
"actions" : [
{
"class" : "WorkflowActionGetClipboard",
"parameters" : {
}
},
{
"class" : "WorkflowActionMatchRegularExpression",
"parameters" : {
"regex" : {
"text" : "http(s?):\/\/gist.github.com\/([0-9a-f]*)",
"type" : "advancedText",
"tokenRanges" : {
}
},
"matchGroup" : ""
}
},
{
"class" : "WorkflowActionCondition",
"parameters" : {
"expression1" : {
"text" : "?",
"type" : "advancedText",
"tokenRanges" : {
"{0, 1}" : "Input"
}
}
}
},
{
"class" : "WorkflowActionStopWorkflow",
"parameters" : {
"HUDTitle" : "No Gist URL in Clipboard",
"showHUD" : true
}
},
{
"class" : "WorkflowActionConditionEnd",
"parameters" : {
}
},
{
"class" : "WorkflowActionShowHUD",
"parameters" : {
"duration" : 1,
"HUDText" : {
"text" : "Downloading Gist...",
"type" : "advancedText",
"tokenRanges" : {
}
}
}
},
{
"class" : "WorkflowActionRunScript",
"parameters" : {
"script" : "import clipboard\nimport editor\nimport console\nimport re\nimport os\n\nclass InvalidGistURLError (Exception): pass\nclass MultipleFilesInGistError (Exception): pass\nclass NoFilesInGistError (Exception): pass\nclass GistDownloadError (Exception): pass\n\ndef download_gist(gist_url):\n\t# Returns a 2-tuple of filename and content\n\tconsole.show_activity()\n\tgist_id_match = re.match('http(s?):\/\/gist.github.com\/([0-9a-f]*)', gist_url)\n\tif gist_id_match:\n\t\timport requests\n\t\tgist_id = gist_id_match.group(2)\n\t\tjson_url = 'https:\/\/api.github.com\/gists\/' + gist_id\n\t\ttry:\n\t\t\timport json\n\t\t\tgist_json = requests.get(json_url).text\n\t\t\tgist_info = json.loads(gist_json)\n\t\t\tfiles = gist_info['files']\n\t\texcept:\n\t\t\traise GistDownloadError()\n\t\twkflw_files = []\n\t\tfor file_info in files.values():\n\t\t\tfilename = file_info['filename']\n\t\t\text = os.path.splitext(filename)[1]\n\t\t\tif ext != '.wkflw' and ext != '.py':\n\t\t\t\tcontinue\n\t\t\twkflw_files.append(file_info)\n\t\tif len(wkflw_files) > 1:\n\t\t\traise MultipleFilesInGistError()\n\t\telif len(wkflw_files) == 0:\n\t\t\traise NoFilesInGistError()\n\t\telse:\n\t\t\tfile_info = wkflw_files[0]\n\t\t\tfilename = file_info['filename']\n\t\t\tcontent = file_info['content']\n\t\t\treturn filename, content\n\telse:\n\t\traise InvalidGistURLError()\n\ndef main():\n\tgist_url = clipboard.get().strip()\n\ttry:\n\t\tfilename, content = download_gist(gist_url)\n\t\tname = os.path.splitext(filename)[0]\n\t\tconsole.alert('Add Command', 'Do you want to add the command \"' + name + '\" to your menu?', 'Add')\n\t\teditor.add_command(name, content)\n\texcept InvalidGistURLError:\n\t\tconsole.alert('No Gist URL',\n\t\t 'The clipboard doesn\\'t seem to contain a valid Gist URL.',\n\t\t 'OK')\n\texcept MultipleFilesInGistError:\n\t\tconsole.alert('Multiple Files', 'This Gist contains multiple files.')\n\texcept NoFilesInGistError:\n\t\tconsole.alert('No Workflow Files', 'This Gist contains no Workflow files.')\n\texcept GistDownloadError:\n\t\tconsole.alert('Error', 'The Gist could not be downloaded.')\n\nif __name__ == '__main__':\n\tmain()\n\n\n"
}
}
],
"name" : "Add from Gist"
}
------------- END WORKFLOW -------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment