Last active
August 29, 2015 14:24
-
-
Save joelcarranza/272124a83024a69c046d to your computer and use it in GitHub Desktop.
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
/*------------------------------------------------------------------- | |
Opens any URLs found in the title or note of selected Omnifocus tasks | |
Source: https://gist.github.com/joelcarranza/272124a83024a69c046d | |
-------------------------------------------------------------------*/ | |
ObjC.import('stdlib') | |
ObjC.import('AppKit') | |
/* | |
Launch and return an application with the given bundle identifier | |
Inspired by: | |
https://github.com/dtinth/JXA-Cookbook/wiki/Getting-the-Application-Instance | |
*/ | |
function launchApplication(bundleIdentifier) { | |
var runningApplications = $.NSWorkspace.sharedWorkspace.runningApplications; | |
var isRunning = runningApplications.js.some(function(app) { | |
return app.bundleIdentifier.js === bundleIdentifier; | |
}); | |
if (!isRunning) { | |
$.NSWorkspace.sharedWorkspace.launchAppWithBundleIdentifierOptionsAdditionalEventParamDescriptorLaunchIdentifier( | |
bundleIdentifier, | |
$.NSWorkspaceLaunchDefault, | |
$.NSAppleEventDescriptor.nullDescriptor, | |
null | |
); | |
} | |
return Application(bundleIdentifier); | |
} | |
var urlre = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi | |
var chrome = launchApplication('com.google.Chrome'); | |
function openURLInTab(url) { | |
if(chrome.windows.length == 0) { | |
chrome.Window().make(); | |
} | |
window = chrome.windows[0]; | |
var tab = chrome.Tab({url: url}); | |
window.tabs.push(tab); | |
} | |
var OF = Application('com.omnigroup.OmniFocus2'); | |
OF.includeStandardAdditions = true; | |
OF.windows[0].content.selectedTrees().forEach(function(task) { | |
var text = task.value().name()+"\n"+task.value().note(); | |
var urls = text.match(urlre); | |
if(urls) { | |
urls.forEach(openURLInTab); | |
} | |
}); | |
chrome.activate(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment