Last active
December 14, 2015 18:52
-
-
Save markbranly/cb9b7315bd41673888f8 to your computer and use it in GitHub Desktop.
Markdown Dropbox Link
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* # Markdown Dropbox Link (Mac OS X Automator Service) | |
* | |
* Triggered on a file from within Finder, takes a Dropbox share link from the Clipboard and | |
* updates the clipboard with a Markdown formatted link where the link text is a Dropbox | |
* root relative path. | |
* | |
* Author: Mark Branly | |
* | |
* ## Installation | |
* | |
* 1. Open Automator (/Applications/Automator) | |
* 2. In the bottom right of the file dialog window that appears, choose "New Document" | |
* 3. For Document Type, choose "Service" | |
* 4. In the main window at the top, change "Service receives selected" to "files or folders" in "Finder" | |
* 5. From the Actions panel, drag "Run Javascript" | |
* 6. Paste the entire contents of this file (MarkdownDropboxLink.js) into the code window, replacing all contents. | |
* 7. From the menubar, choose File > Save and name your service "Markdown Dropbox Link" | |
* | |
* | |
* ## Usage | |
* | |
* 1. Navigate to a file in Dropbox | |
* 2. Generate the Dropbox Share Link from the context menu (like you normally would) | |
* 3. From the services menu (bottom of context menu or _Application_ menu on menu bar), run "Markdown Dropbox Link" | |
* 4. Paste Markdown link into your favorite places like Trello, emails, etc. | |
**/ | |
function run(input, parameters) { | |
var serviceName = 'Markdown Dropbox Link', | |
app = Application.currentApplication(), | |
se = Application('System Events'), | |
showAlert = true, | |
showAlertOnSuccess = false, | |
filePath,message,linkText,linkURL,markdownLink; | |
app.includeStandardAdditions = true; | |
if (input.toString() == ',') { | |
return; | |
} else { | |
filePath = input.toString(); | |
} | |
linkText = getDBXRelativePath(filePath); | |
if (linkText) { | |
// ToDo: Trigger the share dropbox link ourselves | |
// Grab the clipboard contents | |
linkURL = app.theClipboard(); | |
if (isDropboxShareLink(linkURL)) { | |
markdownLink = '[' + linkText + '](' + linkURL + ')'; | |
message = 'The following Markdown link has been copied to your clipboard: \n\n' + markdownLink + '\n\n'; | |
showAlert = showAlertOnSuccess; | |
app.setTheClipboardTo(markdownLink); | |
app.displayNotification( | |
markdownLink, | |
{withTitle: serviceName, subtitle: 'Copied to clipboard'} | |
); | |
} else { | |
message = 'The clipboard doesn\'t appear to contain a Dropbox Share Link.'; | |
} | |
} else { | |
message = 'Looks like this file is not in your Dropbox:\n\n '+input; | |
} | |
if (showAlert) { | |
app.displayAlert( | |
serviceName, | |
{message: message} | |
); | |
} | |
return input; | |
} | |
function getDBXRelativePath(fullPath) { | |
var dbxPos = fullPath.indexOf('/Dropbox'), | |
dbxRoot = -1; | |
if (dbxPos < 0 ) { | |
return false; | |
} | |
dbxRoot = fullPath.indexOf('/', dbxPos + 1); | |
return fullPath.substring(dbxRoot); | |
} | |
function isDropboxShareLink(text) { | |
var shareLinkStart = 'https://www.dropbox.com/'; | |
return text.toString().indexOf(shareLinkStart) == 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment