Skip to content

Add support for specifying a command in browser configurations #342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Finicky/Finicky/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
url = urlComponents.url!
}
}
logToConsole("Calling url handlers for url: \(url.absoluteString)")
shortUrlResolver.resolveUrl(url, callback: { (URL) -> Void in
self.callUrlHandlers(opener: opener, url: URL)
})
Expand All @@ -324,11 +325,16 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
if let appToStart = getActiveApp(browsers: appDescriptor.browsers) {
var success = false
if let bundleId = appToStart.bundleId {
logToConsole("Bundle id: \(bundleId)")
if NSWorkspace.shared.absolutePathForApplication(withBundleIdentifier: bundleId) != nil {
openUrlWithBrowser(appDescriptor.url, browserOpts: appToStart)
success = true
}
} else if appToStart.command != nil {
openUrlWithBrowser(appDescriptor.url, browserOpts: appToStart)
success = true
} else if let appPath = appToStart.appPath {
logToConsole("App path: \(appPath)")
if BrowserOpts.isAppDirectory(appPath) {
openUrlWithBrowser(appDescriptor.url, browserOpts: appToStart)
success = true
Expand Down
6 changes: 6 additions & 0 deletions Finicky/Finicky/AppDescriptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum AppDescriptorType: String {
case bundleId
case appName
case appPath
case command
case none
}

Expand All @@ -22,6 +23,7 @@ public struct BrowserOpts: CustomStringConvertible {
public var bundleId: String?
public var appPath: String?
public var profile: String?
public var command: String?
public var args: [String]

public var description: String {
Expand All @@ -39,6 +41,7 @@ public struct BrowserOpts: CustomStringConvertible {
appType: AppDescriptorType,
openInBackground: Bool?,
profile: String?,
command: String?,
args: [String]
) throws {
self.name = name
Expand All @@ -54,12 +57,15 @@ public struct BrowserOpts: CustomStringConvertible {
}

self.profile = profile
self.command = command
self.args = args

if appType == AppDescriptorType.bundleId {
bundleId = name
} else if appType == AppDescriptorType.appPath {
appPath = name
} else if appType == AppDescriptorType.command {
appPath = command
} else if let path = NSWorkspace.shared.fullPath(forApplication: name) {
if let bundle = Bundle(path: path) {
bundleId = bundle.bundleIdentifier!
Expand Down
13 changes: 11 additions & 2 deletions Finicky/Finicky/Browsers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public func getActiveApp(browsers: [BrowserOpts]) -> BrowserOpts? {
}

public func openUrlWithBrowser(_ url: URL, browserOpts: BrowserOpts) {
print("Opening \(browserOpts) at: " + url.absoluteString)
let command = getBrowserCommand(browserOpts, url: url)
shell(command)
}
Expand All @@ -54,10 +53,20 @@ enum Browser: String {
}

public func getBrowserCommand(_ browserOpts: BrowserOpts, url: URL) -> [String] {
var command = ["open"]
var command: [String] = []
var commandArgs: [String] = []
var appendUrl = true

// command takes priority over appPath, bundleId and openInBackground
if browserOpts.command != nil {
command.append(browserOpts.command!)
command.append(contentsOf: commandArgs)
command.append(url.absoluteString)
return command
} else {
command.append("open")
}

// appPath takes priority over bundleId as it is always unique.
if let appPath = browserOpts.appPath {
command.append(contentsOf: ["-a", appPath])
Expand Down
2 changes: 2 additions & 0 deletions Finicky/Finicky/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ open class FinickyConfig {
let openInBackground: Bool? = dict["openInBackground"] as? Bool
let browserName = dict["name"] as! String
let browserProfile: String? = dict["profile"] as? String
let command: String? = dict["command"] as? String
let args: [String] = dict["args"] as? [String] ?? []

if browserName == "" {
Expand All @@ -326,6 +327,7 @@ open class FinickyConfig {
appType: appType!,
openInBackground: openInBackground,
profile: browserProfile,
command: command,
args: args
)
return browser
Expand Down
2 changes: 1 addition & 1 deletion Finicky/Finicky/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>319</string>
<string>336</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>
Expand Down
10 changes: 9 additions & 1 deletion config-api/src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ const browserSchema = validate.oneOf([
validate.string,
validate.shape({
name: validate.string.isRequired,
appType: validate.oneOf(["appName", "appPath", "bundleId"]),
appType: validate.oneOf([
validate.value("bundleId"),
validate.value("appName"),
validate.value("appPath"),
validate.value("command"),
]),
openInBackground: validate.boolean,
profile: validate.string,
command: validate.string,
args: validate.arrayOf(validate.string),
}),
validate.function("options"),
Expand Down Expand Up @@ -96,9 +102,11 @@ export const appDescriptorSchema = {
validate.value("bundleId"),
validate.value("appName"),
validate.value("appPath"),
validate.value("command"),
validate.value("none"),
]).isRequired,
openInBackground: validate.boolean,
profile: validate.string,
command: validate.string,
args: validate.arrayOf(validate.string),
};