Skip to content

Commit

Permalink
Add sourceProcessPath, rename matchDomains (#81)
Browse files Browse the repository at this point in the history
* Update documentation

* Add matchHostnames alias for matchDomains #73

* Remove debug log

* Get path

* Add documentation, bump version
  • Loading branch information
johnste committed Oct 23, 2019
1 parent f1e641c commit d196333
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 9 deletions.
6 changes: 6 additions & 0 deletions Finicky/Finicky.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
544B57881B28B87900812908 /* statusitem@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "statusitem@2x.png"; sourceTree = "<group>"; };
546F1677228B487F006C5375 /* Utilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utilities.swift; sourceTree = "<group>"; };
5479FDD321655A3400D15A3C /* Notifications.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Notifications.swift; sourceTree = "<group>"; };
548376AB2360C7D100834B68 /* Finicky-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Finicky-Bridging-Header.h"; sourceTree = "<group>"; };
54899CD01B20D5BC00647101 /* Finicky.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Finicky.app; sourceTree = BUILT_PRODUCTS_DIR; };
54899CD41B20D5BC00647101 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
54899CD51B20D5BC00647101 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -110,6 +111,7 @@
54899CD21B20D5BC00647101 /* Finicky */ = {
isa = PBXGroup;
children = (
548376AB2360C7D100834B68 /* Finicky-Bridging-Header.h */,
54CBA90D22A5C7EA0074A67F /* js */,
54899CD51B20D5BC00647101 /* AppDelegate.swift */,
54899CD91B20D5BC00647101 /* MainMenu.xib */,
Expand Down Expand Up @@ -463,8 +465,10 @@
INFOPLIST_FILE = Finicky/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.12;
MARKETING_VERSION = v2.2.2;
PRODUCT_BUNDLE_IDENTIFIER = net.kassett.finicky;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Finicky/Finicky-Bridging-Header.h";
SWIFT_VERSION = 5.0;
};
name = Debug;
Expand All @@ -478,8 +482,10 @@
INFOPLIST_FILE = Finicky/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.12;
MARKETING_VERSION = v2.2.2;
PRODUCT_BUNDLE_IDENTIFIER = net.kassett.finicky;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Finicky/Finicky-Bridging-Header.h";
SWIFT_VERSION = 5.0;
};
name = Release;
Expand Down
25 changes: 21 additions & 4 deletions Finicky/Finicky/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,29 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
return result
}

func getPidPath(pid: Int32) -> String? {
let pathBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: Int(MAXPATHLEN))
defer {
pathBuffer.deallocate()
}
let pathLength = proc_pidpath(pid, pathBuffer, UInt32(MAXPATHLEN))

if pathLength > 0 {
let path = String(cString: pathBuffer)
return path
}
return nil
}

@objc func handleGetURLEvent(_ event: NSAppleEventDescriptor?, withReplyEvent _: NSAppleEventDescriptor?) {
toggleDockIcon(showIcon: false)
let url: URL = URL(string: event!.paramDescriptor(forKeyword: AEKeyword(keyDirectObject))!.stringValue!)!
let pid = event!.attributeDescriptor(forKeyword: AEKeyword(keySenderPIDAttr))!.int32Value
let sourceBundleIdentifier = NSRunningApplication(processIdentifier: pid)?.bundleIdentifier
let path = getPidPath(pid: pid)

shortUrlResolver.resolveUrl(url, callback: { (URL) -> Void in
self.callUrlHandlers(sourceBundleIdentifier, url: URL)
self.callUrlHandlers(sourceBundleIdentifier, url: URL, sourceProcessPath: path)
})
}

Expand Down Expand Up @@ -190,8 +206,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
return browsers.first
}

@objc func callUrlHandlers(_ sourceBundleIdentifier: String?, url: URL) {
if let appDescriptor = configLoader.determineOpeningApp(url: url, sourceBundleIdentifier: sourceBundleIdentifier) {
@objc func callUrlHandlers(_ sourceBundleIdentifier: String?, url: URL, sourceProcessPath: String?) {
if let appDescriptor = configLoader.determineOpeningApp(url: url, sourceBundleIdentifier: sourceBundleIdentifier, sourceProcessPath: sourceProcessPath) {
if let appToStart = getActiveApp(browsers: appDescriptor.browsers) {
if NSWorkspace.shared.absolutePathForApplication(withBundleIdentifier: appToStart.bundleId) != nil {
openUrlWithBrowser(appDescriptor.url, bundleIdentifier: appToStart.bundleId, openInBackground: appToStart.openInBackground)
Expand Down Expand Up @@ -222,8 +238,9 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
}

func application(_: NSApplication, openFiles filenames: [String]) {
toggleDockIcon(showIcon: false)
for filename in filenames {
callUrlHandlers(nil, url: URL(fileURLWithPath: filename))
callUrlHandlers(nil, url: URL(fileURLWithPath: filename), sourceProcessPath: nil)
}
}

Expand Down
7 changes: 4 additions & 3 deletions Finicky/Finicky/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ open class FinickyConfig {
return list
}

open func determineOpeningApp(url: URL, sourceBundleIdentifier: String? = nil) -> AppDescriptor? {
if let appValue = getConfiguredAppValue(url: url, sourceBundleIdentifier: sourceBundleIdentifier) {
open func determineOpeningApp(url: URL, sourceBundleIdentifier: String? = nil, sourceProcessPath: String? = nil) -> AppDescriptor? {
if let appValue = getConfiguredAppValue(url: url, sourceBundleIdentifier: sourceBundleIdentifier, sourceProcessPath: sourceProcessPath) {
if !appValue.isObject {
return nil
}
Expand Down Expand Up @@ -278,9 +278,10 @@ open class FinickyConfig {
return nil
}

func getConfiguredAppValue(url: URL, sourceBundleIdentifier: String?) -> JSValue? {
func getConfiguredAppValue(url: URL, sourceBundleIdentifier: String?, sourceProcessPath: String?) -> JSValue? {
let optionsDict = [
"sourceBundleIdentifier": sourceBundleIdentifier as Any,
"sourceProcessPath": sourceProcessPath as Any,
"keys": getModifierKeyFlags(),
] as [AnyHashable: Any]
let result: JSValue? = ctx.evaluateScript("finickyConfigApi.processUrl")?.call(withArguments: [configObject!, url.absoluteString, optionsDict])
Expand Down
13 changes: 13 additions & 0 deletions Finicky/Finicky/Finicky-Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Finicky-Bridging-Headder.h
//
//
// Created by John Sterling on 2019-10-23.
//

#ifndef Finicky_Bridging_Headder_h
#define Finicky_Bridging_Headder_h

#include <libproc.h>

#endif /* Finicky_Bridging_Headder_h */
2 changes: 1 addition & 1 deletion Finicky/Finicky/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>v2.2.1</string>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleURLTypes</key>
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Finicky is an macOS application that allows you to set up rules that decide whic
1. Installation alternatives:

- Download [the latest release](https://github.com/johnste/finicky/releases), unzip and put `Finicky.app` in your application folder.
- Install with [homebrew-cask](https://github.com/caskroom/homebrew-cask): `brew cask install finicky`.
- Install with [homebrew-cask](https://github.com/caskroom/homebrew-cask): `brew cask install finicky`.

2. Create a file called `.finicky.js` with configuration
([examples](#example-configuration)) in your home directory.
Expand Down Expand Up @@ -159,6 +159,12 @@ module.exports = {
sourceBundleIdentifier === "com.tinyspeck.slackmacgap",
browser: "Safari"
},
{
// You can get the path of the process that triggered Finicky (EXPERIMENTAL)
match: ({ sourceProcessPath }) =>
sourceProcessPath.startsWith("/Applications/Slack.app/"),
browser: "Firefox"
},
{
match: ["http://zombo.com"],
browser: {
Expand Down

0 comments on commit d196333

Please sign in to comment.