Skip to content

Instantly share code, notes, and snippets.

@gruber
gruber / Liberal Regex Pattern for Web URLs
Last active April 22, 2024 19:02
Liberal, Accurate Regex Pattern for Matching Web URLs
The regex patterns in this gist are intended only to match web URLs -- http,
https, and naked domains like "example.com". For a pattern that attempts to
match all URLs, regardless of protocol, see: https://gist.github.com/gruber/249502
# Single-line version:
(?i)\b((?:https?:(?:/{1,3}|[a-z0-9%])|[a-z0-9.\-]+[.](?:com|net|org|edu|gov|mil|aero|asia|biz|cat|coop|info|int|jobs|mobi|museum|name|post|pro|tel|travel|xxx|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|dd|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|s
@gruber
gruber / Battery Test — Chrome.scpt
Last active April 22, 2024 08:33
A simple test to load a series of web pages in Chrome on MacOS repeatedly until the battery runs out.
on run
set imessage_id to "" -- leave blank to skip sending periodic iMessage progress updates, put your iMessage ID here to get updates as the script runs
set delay_seconds to 5 -- Don't set to 0, it runs too fast. 0.75 is a good setting for "go fast".
set computer_name to do shell script "scutil --get ComputerName"
set os_version to do shell script "sw_vers -productVersion"
set os_build to do shell script "sw_vers -buildVersion"
set _uptime to do shell script "uptime"
set url_list to {"http://techmeme.com", "http://www.consumerreports.org/laptops/macbook-pros-fail-to-earn-consumer-reports-recommendation/", "https://twitter.com/panzer/status/812367550734401536", "http://www.politico.com/story/2016/12/foreign-travelers-social-media-232930", "http://www.macworld.com/article/3153384/gaming/nintendo-plans-to-release-2-or-3-mobile-games-a-year-after-super-mario-runs-success.html", "http://www.wsj.com/articles/cyber-experts-cite-link-between-dnc-hacks-and-aggression-against-uk
@gruber
gruber / Split Tabs to New Window.scpt
Last active April 16, 2024 20:52
An AppleScript for Safari to move all tabs in the frontmost window, from the current tab to the rightmost (last) tab, to a new window.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
(*
Original script: John Gruber (https://daringfireball.net/linked/2023/12/05/an-applescript-for-safari-split-tabs-to-new-window)
Much more elegant version: Leon Cowle (https://github.com/leoncowle)
Even more elegant version: https://stackoverflow.com/questions/54066100/applescript-to-split-safari-tabs-into-new-window/54089865#54089865
Worth a warning: "moving" tabs with this script doesn't actually move them like
drag-and-drop does. The tabs "moved" by this script will reload in the new window,
@gruber
gruber / gist:1063574
Last active April 15, 2024 20:00
Walken in LA: Hot Dogs

By Christopher Walken

Do you enjoy eating hot dogs? I hope you won’t be put off by my frankness when I tell you that I absolutely love them. In fact, I enjoy no food item more than a freshly-boiled hot dog. Now, I’ve done a lot of movies, and it’s true that I’ve worked with quite a few celebrities who did not share this opinion. I’m sorry to say that these people have always angered me.

There are two types of people in this world: those who eat hot dogs whenever it is possible to do so, and those who opt to do other things with their free time. Who do the latter think they are kidding? What pastime could be more rewarding than the consumption of hot dogs? I haven’t yet found one, and I don’t expect to in my lifetime. Unlike other foods, hot dogs can be eaten at any time, in any place, and it is not necessary to cook them. Now, I ask you: Why not eat hot dogs? They are delicious.

I carry a bag of hot dogs with me wherever I go. I eat them from the bag whenever I get the urge, regardless of the circumstances

@gruber
gruber / Liberal Regex Pattern for All URLs
Last active March 20, 2024 20:28
Liberal, Accurate Regex Pattern for Matching All URLs
The regex patterns in this gist are intended to match any URLs,
including "mailto:foo@example.com", "x-whatever://foo", etc. For a
pattern that attempts only to match web URLs (http, https), see:
https://gist.github.com/gruber/8891611
# Single-line version of pattern:
(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))
@gruber
gruber / AppleNewsURLRedirect.pl
Last active March 5, 2024 16:33
Given an Apple News URL, prints the original URL for the story.
#!/usr/bin/perl
# Given an Apple News URL, prints the original URL for the story.
#
# Usage:
# % AppleNewsURLRedirect.pl 'https://apple.news/Ae78KIEmMOHCVXD4VA7ysKw'
# or
# % echo 'https://apple.news/Ae78KIEmMOHCVXD4VA7ysKw' | AppleNewsURLRedirect.pl
#
# Author: John Gruber <https://daringfireball.net/>
#!/usr/bin/perl
# This filter changes all words to Title Caps, and attempts to be clever
# about *un*capitalizing small words like a/an/the in the input.
#
# The list of "small words" which are not capped comes from
# the New York Times Manual of Style, plus 'vs' and 'v'.
#
# 10 May 2008
# Original version by John Gruber:
@gruber
gruber / gist:647224
Created October 26, 2010 16:26 — forked from dpk/gist:646570
Slight tweaks to dpkendal's script. Gives the BBEdit document a name (rather than "untitled <integer>", and skips the \n to \r stuff, which I don't think is necessary in recent versions of BBEdit.
tell application "Safari"
set _source to do JavaScript "window.document.documentElement.outerHTML" in document 1
set _name to name of document 1
end tell
tell application "BBEdit"
make new text window with properties ¬
{contents:_source, source language:"HTML", name:"Generated source: " & _name}
select insertion point before character 1 of text window 1
# Copy the URL or tweet ID for a tweet, and the script will send it to Aaron's Twitter Viewer
set _old_delims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set _tweet_url to get the clipboard
set _tweet_id to last text item of _tweet_url
try
get _tweet_id as integer
on error
@gruber
gruber / JavaScript Blacklist
Last active November 8, 2023 22:46
My list of blocked sites for use with Drew Thaler's JavaScript Blacklist extension for Safari (https://code.google.com/p/jsblacklist/downloads/list)
adclick.g.doubleclick.net, addthis.com, adgardener.com, adnxs.com, ads.pointroll.com, ads.shorttail.net, apture.com, bop.fm, cdn.taboolasyndication.com, chartbeat.com, chartbeat.net, d1.openx.org, doubleclick.net, doubleverify.com, exitjunction.com, fyre.co, getconnected.southwestwi-fi.com, googleadservices.com, gravity.com, grvcdn.com, imrworldwide.com, intellitxt.com, jetpackdigital.com, kontera.com, livefyre.com, luminate.com, meebo.com, moovmanage.com, outbrain.com, parsely.com, po.st, pointroll.com, pubmatic.com, quantserve.com, s.ppjol.net, scorecardresearch.com, serving-sys.com, sharethis.com, snap.com, superclick.com, taboola.com, taboolasyndication.com, tynt.com, wibiya.com, zergnet.com