Skip to content

Commit

Permalink
Merge branch 'master' of github.com:johnste/finicky
Browse files Browse the repository at this point in the history
  • Loading branch information
johnste committed Aug 9, 2015
2 parents 1f08f39 + 27d8fd8 commit 2361be8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
42 changes: 32 additions & 10 deletions Finicky/Finicky/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
var configLoader: FNConfigLoader!
var shortUrlResolver: FNShortUrlResolver!
var urlsToLoad = Array<String>()
var isActive: Bool = true

static var defaultBrowser: String! = "com.google.Chrome"

Expand All @@ -31,7 +32,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
var img: NSImage! = NSImage(named: "statusitem")
img.setTemplate(true)

let bar = NSStatusBar.systemStatusBar()
let bar = NSStatusBar.systemStatusBar()
// Workaround for some bug: -1 instead of NSVariableStatusItemLength
statusItem = bar.statusItemWithLength(CGFloat(-1))
statusItem.menu = statusItemMenu
Expand Down Expand Up @@ -77,29 +78,45 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let flags = getFlags()
var bundleIdentifier : String! = AppDelegate.defaultBrowser
var newUrl : NSURL = url
var openInBackground : Bool? = nil

let strategy = FinickyAPI.callUrlHandlers(newUrl, sourceBundleIdentifier: sourceBundleIdentifier, flags: flags)
if strategy["url"] != nil {
newUrl = NSURL(string: strategy["url"]!)!

let bundleId : String! = strategy["bundleIdentifier"] as String!
newUrl = NSURL(string: strategy["url"]! as! String)!

let bundleId : String! = strategy["bundleIdentifier"] as! String!
if bundleId != nil && !bundleId.isEmpty {
bundleIdentifier = strategy["bundleIdentifier"]!
bundleIdentifier = strategy["bundleIdentifier"]! as! String
}

if strategy["openInBackground"] != nil {
openInBackground = (strategy["openInBackground"]! as! Bool)
}

if bundleIdentifier != nil && !bundleIdentifier.isEmpty {
openUrlWithBrowser(newUrl, bundleIdentifier:bundleIdentifier)
openUrlWithBrowser(newUrl, bundleIdentifier:bundleIdentifier, openInBackground: openInBackground)
}
}
}

func openUrlWithBrowser(url: NSURL, bundleIdentifier: String) {
func openUrlWithBrowser(url: NSURL, bundleIdentifier: String, openInBackground: Bool?) {
var eventDescriptor: NSAppleEventDescriptor? = NSAppleEventDescriptor()
var errorInfo : NSDictionary? = nil
var appleEventManager:NSAppleEventManager = NSAppleEventManager.sharedAppleEventManager()
var urls = [url]
NSWorkspace.sharedWorkspace().openURLs(urls, withAppBundleIdentifier: bundleIdentifier, options: NSWorkspaceLaunchOptions.Default, additionalEventParamDescriptor: nil, launchIdentifiers: nil)

var launchInBackground = !isActive
if openInBackground != nil {
launchInBackground = openInBackground!
}

NSWorkspace.sharedWorkspace().openURLs(
urls,
withAppBundleIdentifier: bundleIdentifier,
options: launchInBackground ? NSWorkspaceLaunchOptions.WithoutActivation : NSWorkspaceLaunchOptions.Default,
additionalEventParamDescriptor: nil,
launchIdentifiers: nil
)
}

func getFlags() -> Dictionary<String, Bool> {
Expand All @@ -115,12 +132,17 @@ class AppDelegate: NSObject, NSApplicationDelegate {
configLoader = FNConfigLoader()
configLoader.reload()
shortUrlResolver = FNShortUrlResolver()

var appleEventManager:NSAppleEventManager = NSAppleEventManager.sharedAppleEventManager()
appleEventManager.setEventHandler(self, andSelector: "handleGetURLEvent:withReplyEvent:", forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
}

func applicationWillTerminate(aNotification: NSNotification) {
func applicationDidBecomeActive(aNotification: NSNotification) {
isActive = true
}

func applicationDidResignActive(aNotification: NSNotification) {
isActive = false
}

}

11 changes: 7 additions & 4 deletions Finicky/Finicky/FNAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ import JavaScriptCore
the new url and bundle identifier to spawn
*/

class func callUrlHandlers(originalUrl: NSURL, sourceBundleIdentifier: String?, flags : Dictionary<String, Bool>) -> Dictionary<String, String> {
var strategy : Dictionary<String, String> = [
class func callUrlHandlers(originalUrl: NSURL, sourceBundleIdentifier: String?, flags : Dictionary<String, Bool>) -> Dictionary<String, AnyObject> {
var strategy : Dictionary<String, AnyObject> = [
"url": originalUrl.absoluteString!,
"bundleIdentifier": ""
]
Expand All @@ -72,7 +72,7 @@ import JavaScriptCore
]

for handler in urlHandlers {
let url = strategy["url"]!
let url = strategy["url"]! as! String
let val = handler.callWithArguments([url, options])

if !val.isUndefined() {
Expand All @@ -85,6 +85,10 @@ import JavaScriptCore
if handlerStrategy["bundleIdentifier"] != nil {
strategy["bundleIdentifier"] = (handlerStrategy["bundleIdentifier"] as! String)
}

if handlerStrategy["openInBackground"] != nil {
strategy["openInBackground"] = (handlerStrategy["openInBackground"] as! Bool)
}

if handlerStrategy["last"] != nil {
break
Expand All @@ -94,5 +98,4 @@ import JavaScriptCore
}
return strategy
}

}

0 comments on commit 2361be8

Please sign in to comment.