Skip to content

Instantly share code, notes, and snippets.

@tofias
Forked from gruber/Open URLs in Safari Tabs.pl
Last active December 19, 2015 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tofias/6025922 to your computer and use it in GitHub Desktop.
Save tofias/6025922 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
# Description: http://daringfireball.net/2010/08/open_urls_in_safari_tabs
# License: See below.
# http://gist.github.com/507356
# UPDATE for "Create and run the AppleScript" portion of Gruber's original script
# http://gist.github.com/1990793
#
# UPDATE 2 for force opening in Google Chrome
# https://gist.github.com/tofias/6025922
use strict;
use warnings;
use URI::Escape;
my $text = do { local $/; <> };
my @urls;
my $url_regex = qr{(?xi)
\b
( # Capture 1: entire matched URL
(?:
[a-z][\w-]+: # URL protocol and colon
(?:
/{1,3} # 1-3 slashes
| # or
[a-z0-9%] # Single letter or digit or '%'
# (Trying not to match e.g. "URI::Escape")
)
| # or
www\d{0,3}[.] # "www.", "www1.", "www2." … "www999."
| # or
[a-z0-9.\-]+[.][a-z]{2,4}/ # looks like domain name followed by a slash
)
(?: # One or more:
[^\s()<>]+ # Run of non-space, non-()<>
| # or
\(([^\s()<>]+|(\([^\s()<>]+\)))*\) # balanced parens, up to 2 levels
)+
(?: # End with:
\(([^\s()<>]+|(\([^\s()<>]+\)))*\) # balanced parens, up to 2 levels
| # or
[^\s`!()\[\]{};:'".,<>?«»“”‘’] # not a space or one of these punct chars
)
)
};
# Build an AppleScript-syntax list of the URLs in the input text.
while ($text =~ m{$url_regex}g) {
my $u = $1;
$u =~ s{([\x{80}-\x{ffff}])}{uri_escape($1)}eg; # Encode non-ASCII characters
push @urls, qq{"$u"};
}
my $urls_as_applescript_list = join ", ", @urls;
# Create and run the AppleScript.
my $applescript = <<"END_SCRIPT";
set _url_list to {$urls_as_applescript_list}
set _tab to 1
repeat with _url in _url_list
tell application "Google Chrome"
activate
if _tab is 1 then
make new window
set URL of active tab of window 1 to _url
set _tab to 0
else
tell window 1 to set URL of (make new tab) to _url
end if
end tell
end repeat
END_SCRIPT
print $applescript;
system("/usr/bin/osascript", "-e", $applescript);
__END__
LICENSE
Copyright (c) 2013 Michael Tofias
Permission is hereby granted in the same manner of John Gruber's below.
http://www.opensource.org/licenses/mit-license.php
Copyright (c) 2011 John Gruber
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment