Skip to content

Commit

Permalink
Basic support for chrome incognito
Browse files Browse the repository at this point in the history
  • Loading branch information
johnste committed Oct 29, 2019
1 parent 2e6c640 commit 949a587
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 33 deletions.
14 changes: 9 additions & 5 deletions Finicky/Finicky.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
54899CF31B20D5D100647101 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 54899CF21B20D5D100647101 /* Cocoa.framework */; };
548F11CC1B2A39F800FDDC3C /* finicky.icns in Resources */ = {isa = PBXBuildFile; fileRef = 548F11CB1B2A39F800FDDC3C /* finicky.icns */; };
548F11CE1B2A43F700FDDC3C /* API.swift in Sources */ = {isa = PBXBuildFile; fileRef = 548F11CD1B2A43F700FDDC3C /* API.swift */; };
54911891236843EE00346410 /* Chrome.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54911890236843EE00346410 /* Chrome.swift */; };
54B0E7031B4678CE003F8AEE /* ShortUrlResolver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54B0E7021B4678CE003F8AEE /* ShortUrlResolver.swift */; };
54B12879229C59EC00990D0D /* AppDescriptor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54B12878229C59EC00990D0D /* AppDescriptor.swift */; };
54B5771622860D0B0016BF77 /* Credits.rtf in Resources */ = {isa = PBXBuildFile; fileRef = 54B5771522860D0B0016BF77 /* Credits.rtf */; };
Expand Down Expand Up @@ -60,6 +61,7 @@
54899CF21B20D5D100647101 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
548F11CB1B2A39F800FDDC3C /* finicky.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; path = finicky.icns; sourceTree = "<group>"; };
548F11CD1B2A43F700FDDC3C /* API.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = API.swift; sourceTree = "<group>"; };
54911890236843EE00346410 /* Chrome.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Chrome.swift; sourceTree = "<group>"; };
54B0E7021B4678CE003F8AEE /* ShortUrlResolver.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShortUrlResolver.swift; sourceTree = "<group>"; };
54B12878229C59EC00990D0D /* AppDescriptor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDescriptor.swift; sourceTree = "<group>"; };
54B5771522860D0B0016BF77 /* Credits.rtf */ = {isa = PBXFileReference; lastKnownFileType = text.rtf; path = Credits.rtf; sourceTree = "<group>"; };
Expand Down Expand Up @@ -111,7 +113,7 @@
54899CD21B20D5BC00647101 /* Finicky */ = {
isa = PBXGroup;
children = (
5491188E2368194000346410 /* Chrome */,
5491188F2368438200346410 /* Browsers */,
548376AB2360C7D100834B68 /* Finicky-Bridging-Header.h */,
54CBA90D22A5C7EA0074A67F /* js */,
54899CD51B20D5BC00647101 /* AppDelegate.swift */,
Expand Down Expand Up @@ -170,11 +172,12 @@
name = Frameworks;
sourceTree = "<group>";
};
5491188E2368194000346410 /* Chrome */ = {
5491188F2368438200346410 /* Browsers */ = {
isa = PBXGroup;
children = (
54911890236843EE00346410 /* Chrome.swift */,
);
name = Chrome;
path = Browsers;
sourceTree = "<group>";
};
54CBA90D22A5C7EA0074A67F /* js */ = {
Expand Down Expand Up @@ -323,6 +326,7 @@
54E33FD71B24E27000998E13 /* Config.swift in Sources */,
54899CD61B20D5BC00647101 /* AppDelegate.swift in Sources */,
54B0E7031B4678CE003F8AEE /* ShortUrlResolver.swift in Sources */,
54911891236843EE00346410 /* Chrome.swift in Sources */,
54E4B1EB22971F7B002FA6F4 /* UpdateCheck.swift in Sources */,
548F11CE1B2A43F700FDDC3C /* API.swift in Sources */,
);
Expand Down Expand Up @@ -473,7 +477,7 @@
INFOPLIST_FILE = Finicky/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.12;
MARKETING_VERSION = v2.2.2;
MARKETING_VERSION = "v2.3-alpha";
PRODUCT_BUNDLE_IDENTIFIER = net.kassett.finicky;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Finicky/Finicky-Bridging-Header.h";
Expand All @@ -490,7 +494,7 @@
INFOPLIST_FILE = Finicky/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.12;
MARKETING_VERSION = v2.2.2;
MARKETING_VERSION = "v2.3-alpha";
PRODUCT_BUNDLE_IDENTIFIER = net.kassett.finicky;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Finicky/Finicky-Bridging-Header.h";
Expand Down
31 changes: 5 additions & 26 deletions Finicky/Finicky/Browsers.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import Foundation


enum Browser: String {
case Chrome = "com.google.Chrome"
case ChromeCanary = "com.google.Chrome.canary"
Expand All @@ -13,44 +14,22 @@ enum Browser: String {
}

public func startBrowser(app: BrowserOpts, url: URL, defaultOpenInBackground: Bool) {
var command = ["open", url.absoluteString, "-b", app.bundleId]

if (app.bundleId == Browser.Chrome.rawValue) {
let chromeScript = """
tell application "Google Chrome"
set incognitoWindows to (every window whose mode is "incognito")
set myLink to "http://example.com/?shark=yeswindow"
if (count of incognitoWindows) > 0 then
set incognitoWindow to first window whose mode is "incognito"
tell incognitoWindow to make new tab at after (get active tab) with properties {URL:myLink}
else
set incognitoWindow to make new window with properties {mode:"incognito"}
tell incognitoWindow to set URL of active tab to myLink
end if
end tell
"""
executeScript(chromeScript)
StartChromeIncognito(app: app, url: url, defaultOpenInBackground: defaultOpenInBackground)
return
}

var command = ["open", url.absoluteString, "-b", app.bundleId]

if app.openInBackground ?? defaultOpenInBackground {
command.append("-g")
}

print(command.joined(separator: " "))
shell(command)
}

func executeScript(_ script: String) {
var error: NSDictionary?
if let scriptObject = NSAppleScript(source: script) {
if let output: NSAppleEventDescriptor = scriptObject.executeAndReturnError(
&error) {
print(output.stringValue)
} else if (error != nil) {
showNotification(title: "error: \(error)")
}
}
}



68 changes: 68 additions & 0 deletions Finicky/Finicky/Browsers/Chrome.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// Chrome.swift
// Finicky
//
// Created by John Sterling on 2019-10-29.
// Copyright © 2019 John sterling. All rights reserved.
//

import Foundation
import AppKit
import Cocoa

public func StartChromeIncognito(app: BrowserOpts, url: URL, defaultOpenInBackground: Bool) {

let apps = NSRunningApplication.runningApplications(withBundleIdentifier: app.bundleId)

// If Chrome isn't running we open it from the shell to avoid opening a normal window as well as the incognito window we need
if apps.isEmpty {
var command = ["open", "-b", app.bundleId]
if app.openInBackground ?? defaultOpenInBackground {
command.append("-g")
}

command.append(contentsOf: ["--args", url.absoluteString, "--incognito"])

print(command.joined(separator: " "))
shell(command)

} else {

// If Chrome is running we do one of two things.
// a. If there is a incognito window opened already, we append a new tab to it with the wanted url
// b. If there is no incognito window we open one and set the url of the first tab to the wanted url

// FIXME: Currently using string interpolation to add the url to the script, since I didn't get a prompt when trying to send as parameters. Should be able to fix this in time.
let chromeScript = """
set myLink to "\(url.absoluteString.replacingOccurrences(of: "\"", with: "\\\""))"
tell application "Google Chrome"
set incognitoWindows to (every window whose mode is "incognito")
if (count of incognitoWindows) > 0 then
set incognitoWindow to first window whose mode is "incognito"
tell incognitoWindow to make new tab at after (get active tab) with properties {URL:myLink}
else
set incognitoWindow to make new window with properties {mode:"incognito"}
tell incognitoWindow to set URL of active tab to myLink
end if
end tell
"""
executeScript(chromeScript)
return
}



}

func executeScript(_ script: String) {
var error: NSDictionary?
if let scriptObject = NSAppleScript(source: script) {
if let output: NSAppleEventDescriptor = scriptObject.executeAndReturnError(
&error) {
print(output.stringValue)
} else if (error != nil) {
showNotification(title: "error: \(error)")
}
}
}
4 changes: 2 additions & 2 deletions Finicky/Finicky/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSAppleEventsUsageDescription</key>
<string>Please give Finicky access to control this browser</string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDocumentTypes</key>
Expand Down Expand Up @@ -74,6 +72,8 @@
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSAppleEventsUsageDescription</key>
<string>Please give Finicky access to control this browser</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2019 John Sterling. All rights reserved. Hand icon by WPZOOM.com (CC BY-SA 3.0)</string>
<key>NSMainNibFile</key>
Expand Down

0 comments on commit 949a587

Please sign in to comment.