Skip to content

Commit

Permalink
Version 3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Shogun committed Mar 20, 2017
1 parent 4737bad commit 4527410
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.history
alfred-chrome
/alfred-chrome
src/alfred-chrome/.build
Binary file modified Alfred Chrome.alfredworkflow
Binary file not shown.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 3.0.0 / 2017-03-20

* Rewritten to easily choose profiles.

### 2.2.0 / 2016-11-23

* Use static linking for the executable.
Expand Down
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,11 @@ Alfred forums topic: [Link](http://www.alfredforum.com/topic/9473-alfred-chrome-

This workflow allows you to open a URL in [Google Chrome](https://www.google.com/chrome/) via [Alfred 3](https://www.alfredapp.com/).

The recognized commands are:
To trigger the workflow, type `chrome` or `cr`. Optionally you can add a URL.

* `chrome [as $USER] [[in] incognito] [$URL]`
* `chrome-canary [as $USER] [[in] incognito] [$URL]`
* `canary [as $USER] [[in] incognito] [$URL]`
Between the results you will a list of profiles to use to open the URL (or simply to open a new window).

Where

* each part between square brackets is optional.
* `$URL` is the URL you want to open.
* `$USER` must be replaced with a valid Chrome (Canary) profile name.
If you hold the `Alt` key while selecting the result item, the window will be open using incognito mode.

## Installation

Expand All @@ -30,7 +24,16 @@ This workflow is for Alfred 3 only therefore it works on macOS 10.9+ (Mavericks

## Uninstallation

Remove the workflow via Alfred settings.
Remove the workflow via Alfred settings.

## Specifying a different application

If you use Canary version of Chrome or similar applications, like Chromium, you can instruct the workflow to use it.

To do it:

* In the Workflows settings, right click on Alfred Chrome workflow and select "Open in Terminal".
* Enter and execute the following command: `echo "$APP" > application`, where `$APP` must be replaced with the app name, like you see in the Applications folder.

## Copyright

Expand Down
8 changes: 8 additions & 0 deletions src/alfred-chrome/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* This file is part of alfred-chrome. Copyright (C) 2016 and above Shogun <[email protected]>.
* Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
*/

import PackageDescription

let package = Package(name: "alfred-chrome")
66 changes: 66 additions & 0 deletions src/alfred-chrome/Sources/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* This file is part of alfred-chrome. Copyright (C) 2016 and above Shogun <[email protected]>.
* Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
*/

import Foundation
import AppKit.NSWorkspace
let fsManager = FileManager()

// Gather informations about URL and app
let url = CommandLine.arguments.dropFirst().joined(separator: " ")
let name = fsManager.fileExists(atPath: "./application") ?
String(data: fsManager.contents(atPath: "./application")!, encoding: String.Encoding.utf8)!.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) :
"Google Chrome"

// Check if the browser is installed, otherwise nothing to do
guard
let application = NSWorkspace.shared().fullPath(forApplication: name), let executable = Bundle(path: application)!.executablePath,
let data = fsManager.contents(atPath: "\(application)/Contents/Info.plist"),
let plist = try PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? [String: Any],
let supportDirectory = plist["CrProductDirName"]
else {
print("\(name) cannot be found.\nNothing to do! :(\n")
exit(1)
}

// Now scan the list of valid profiles for the browser
let base = "\(fsManager.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0].path)/\(supportDirectory)"
let profiles = try fsManager.contentsOfDirectory(atPath: base)
.filter({ $0 != "System Profile" && $0 != "Guest Profile" && fsManager.fileExists(atPath: base + "/\($0)/Preferences") })
.reduce([String: String]()) { accu, entry in
guard
let data = fsManager.contents(atPath: "\(base)/\(entry)/Preferences"),
let parsed = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions()) as? [String: Any],
let profile = (parsed["profile"] as! [String: Any])["name"] as? String
else {
return accu
}

var newAccu = accu // Needed since accu is let and var usage is deprecated in Swift 3
newAccu[profile] = entry
return newAccu
}

let items: [[String: Any]] = profiles.keys.map { key in
let profile = profiles[key]!
let displayUrl = url.characters.count > 0 ? url : "a new window"
let title = "Open \(url.characters.count > 0 ? "URL" : "a new window") using profile \(key)"

return [
"uid": "chrome-\(name.lowercased().replacingOccurrences(of: " ", with: "-"))-profile-\(key.lowercased().replacingOccurrences(of: " ", with: "-"))",
"title": title,
"subtitle": "Open \(displayUrl) in \(name) using profile \(key) ...",
"arg": "\"\(executable)\" \(url) --profile-directory=\"\(profile)\"",
"mods": [
"alt": [
"title": "\(title) (in incognito)",
"subtitle": "Open \(displayUrl) in \(name) (in incognito) using profile \(key) ...",
"arg": "\"\(executable)\" \(url) --profile-directory=\"\(profile)\" --incognito",
]
]
]
}

let results = try JSONSerialization.data(withJSONObject: ["items": items], options: [])
print(String(data: results, encoding: String.Encoding.utf8)!)
1 change: 1 addition & 0 deletions src/alfred-chrome/application
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Google Chrome Canary

0 comments on commit 4527410

Please sign in to comment.