Skip to content

Commit

Permalink
Rewritten in Swift.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shogun committed Sep 4, 2016
1 parent 400e4d9 commit 8f83b8f
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 94 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Carthage
Frameworks
alfred-chrome
Binary file added Alfred Chrome.alfredworkflow
Binary file not shown.
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
### 1.0.0 / 2013-10-13
### 2.0.0 / 2016-09-03

* Update for Alfred 3
* Rewritten in Swift to simplify (un)installation.

### 1.0.0 / 2014-11-05

* Added install and uninstall commands.
* Initial public version.

### 0.0.2 / 2014-11-03

* Finetuned execution and added the workflow file.

### 0.0.1 / 2014-11-02

* Initial version.
1 change: 1 addition & 0 deletions Cartfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github "crossroadlabs/Regex"
3 changes: 3 additions & 0 deletions Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
github "crossroadlabs/Result" "2.0.1"
github "crossroadlabs/Boilerplate" "0.2.4"
github "crossroadlabs/Regex" "0.8.0"
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
SRCDIR=${CURDIR}

alfred-chrome : alfred-chrome.swift frameworks
xcrun -sdk macosx swiftc -F Carthage/Build/Mac/ alfred-chrome.swift && install_name_tool -add_rpath "@executable_path/Frameworks" alfred-chrome

frameworks:
rm -rf Frameworks && ln -s Carthage/Build/Mac Frameworks

install: alfred-chrome
cd ~/Library/Application\ Support/Alfred\ 3/Alfred.alfredpreferences/workflows/user.workflow.${WORKFLOW} && \
rm -rf alfred-chrome Frameworks/* && \
cp ${SRCDIR}/alfred-chrome . && mkdir -p Frameworks && cp -a ${SRCDIR}/Carthage/Build/Mac/*.framework Frameworks

clean:
rm -rf Frameworks alfred-chrome

default: alfred-chrome
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
# Alfred Chrome Workflow

This workflow allows you to open a URL in [Google Chrome](https://www.google.com/intl/en/chrome/browser/) via [Alfred](http://www.alfredapp.com/).
Website: [Link](https://sw.cowtech.it/alfred-chrome)
Packal file: [Link](http://www.packal.org/workflow/alfred-chrome)
Alfred forums topic: [Link](http://www.alfredforum.com/topic/9473-alfred-chrome-open-url-in-google-chrome-also-with-specific-profile)

The recognized syntax is:
## Description

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

The text under square bracket is optional, the text in uppercase must be replaced with appropriate values.
The recognized commands are:

* `chrome [as $USER] [[in] incognito] $URL`
* `chrome-canary [as $USER] [[in] incognito] $URL`
* `canary [as $USER] [[in] incognito] $URL`

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.

## Installation

Apart from normal workflow installation, the only requirement is the [oj](https://github.com/ohler55/oj) gem, which can be installed by opening the terminal and running:
Double click (or open in Alfred settings) the Alfred Chrome.alfredworkflow file.

## Uninstallation

`gem install oj`
Remove the workflow via Alfred settings.

## Copyright

Expand Down
Binary file removed alfred-chrome.alfredworkflow
Binary file not shown.
60 changes: 60 additions & 0 deletions alfred-chrome.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import Foundation
import AppKit
import Regex

// First of all, gather the query and check if Chrome Canary is requested
let query = Process.arguments.dropFirst().joinWithSeparator(" ")
let useCanary = NSProcessInfo.processInfo().environment["alfred_chrome_canary"] == "YES"
let name = "Chrome\(useCanary ? " Canary" : "")"

// Check if the browser is installed, otherwise nothing to do
let application = NSWorkspace.sharedWorkspace().absolutePathForAppBundleWithIdentifier("com.google.Chrome\(useCanary ? ".canary" : "")")

if application == nil {
print("Google \(name) not installed. Doing nothing!")
exit(0)
}

// Now scan the list of valid profiles for the browser
let fsManager = NSFileManager.defaultManager()
let base = "\(fsManager.URLsForDirectory(.ApplicationSupportDirectory, inDomains: .UserDomainMask)[0].path!)/Google/\(name)"

let profiles = try fsManager.contentsOfDirectoryAtPath(base)
.filter({ $0 != "System Profile" && $0 != "Guest Profile" && fsManager.fileExistsAtPath(base + "/\($0)/Preferences") })
.reduce([String: String]()) { accu, entry in
guard
let preferences = NSData(contentsOfFile: base + "/\(entry)/Preferences"),
let profileInfo = try? NSJSONSerialization.JSONObjectWithData(preferences, options: NSJSONReadingOptions()) as! NSDictionary,
let profile = profileInfo["profile"]!["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
}

// Parse the query
guard
let matcher = try? Regex(pattern: "^(as\\s+(.+?)\\s+)?((in\\s+)?incognito\\s+)?(\\S+)$"),
let match = matcher.findFirst(in: query)
else {
exit(0)
}

// Prepare executable path and its arguments
let executable = NSBundle(path: application!)!.executablePath!
var arguments = [match.group(at: 5)!]

if let user = match.group(at: 2), let path = profiles[user] {
arguments.append("--profile-directory=\(path)")
}

if match.group(at: 3) != nil {
arguments.append("--incognito")
}

// Finally Launch the browser
print("Launching: \(executable) \(arguments.joinWithSeparator(" "))")
NSTask.launchedTaskWithLaunchPath(executable, arguments: arguments)
Binary file added icons/canary.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/standard.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
87 changes: 0 additions & 87 deletions open.rb

This file was deleted.

0 comments on commit 8f83b8f

Please sign in to comment.