Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dlh committed Dec 3, 2014
0 parents commit a2549cf
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
65 changes: 65 additions & 0 deletions Applications/Safari/Open Page With Google Chrome.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) 2014 DLH

var Safari = Application("Safari");
var Chrome = Application("Google Chrome");
Chrome.includeStandardAdditions = true;

var SafariTools = {
currentURL: function() {
if (Safari.documents.length == 0)
return null;
var frontmostWindow = Safari.windows[0];
if (!frontmostWindow.visible())
return null;
return frontmostWindow.currentTab().url();
}
};

var ChromeTools = {
// Finds the tab with the provided URL and reloads + activates it
//
// Returns true if a tab exists with the provided URL.
reloadAndActivateTabWithURL: function(url) {
for (var window of Chrome.windows()) {
var index = window.tabs().findIndex(function(tab) {
return tab.url() == url;
});
if (index >= 0) {
window.activeTabIndex = index + 1;
window.activeTab.reload();
return true;
}
}
return false;
},

// Returns true if the frontmost tab is empty
hasEmptyTab: function() {
if (Chrome.windows.length == 0)
return false;
return Chrome.windows[0].activeTab.url() == "chrome://newtab/";
}
};

function main() {
var url = SafariTools.currentURL();
if (!url)
return;

Chrome.activate();

// Don't needlessly create new tabs if there's already
// one with the url
var didReload = ChromeTools.reloadAndActivateTabWithURL(url);
if (didReload)
return;

if (ChromeTools.hasEmptyTab()) {
Chrome.windows[0].activeTab.url = url;
return;
}

Chrome.openLocation(url);
}

main();
19 changes: 19 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2014, DLH

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
OS X AppleScripts
=================

My collection of AppleScripts.

Installation
------------

* Run `rake compile` to compile the plain-text scripts into `.scpt` files.
* Copy the compiled script(s) to your `~/Library/Scripts` folder, or add the
build directory to the [FastScripts](http://www.red-sweater.com/fastscripts/)
search path:

defaults write com.red-sweater.FastScripts ScriptTreePathsKey -array-add "~/path/to/AppleScripts/build"

About the Scripts
-----------------

* Safari/Open Page With Google Chrome.js
* A better version of Safari's `Develop → Open Page With → Google Chrome.app (…)` menu-item.
* You can assign a shortcut key in FastScripts. This isn't possible with
Safari's built-in menu-item, as the text includes the version number
(which often changes).
* Doesn't needlessly create new tabs if the frontmost one is empty.
* If the URL is already open in Chrome, then it activates that tab and
reloads the page.

License
-------

This project is released under the MIT license. See LICENSE.txt for more
information.
22 changes: 22 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "rake/clean"

BUILD_DIRECTORY = "build"
SOURCES = FileList["**/*/*.js", "**/*/*.applescript"].exclude(/^#{BUILD_DIRECTORY}/)
EXTENSION_TO_LANGUAGE = {
".applescript" => "AppleScript",
".js" => "JavaScript"
}
CLOBBER.include(BUILD_DIRECTORY)

SOURCES.each do |source|
compiled = File.join(BUILD_DIRECTORY, source).ext(".scpt")
file(compiled => [directory(compiled.pathmap("%d")), source]) do |t|
sh("osacompile", "-l", EXTENSION_TO_LANGUAGE[source.pathmap("%x")], "-o", compiled, source)
end
task(:compile => compiled)
end

desc("Compile all scripts")
task(:compile)

task(:default => :compile)

0 comments on commit a2549cf

Please sign in to comment.