Skip to content

Instantly share code, notes, and snippets.

@MattiSG
MattiSG / gist:3075116
Created July 9, 2012 08:34
Drop-in cross-platform OSX “open” functionality
#!/bin/bash
# Cross-platform Darwin open(1)
# Simply add this function definition above any OSX script that uses the “open” command
# For additional information on the “open” command, see https://developer.apple.com/library/mac/#documentation/darwin/reference/manpages/man1/open.1.html
open() {
if [[ $(uname) = "Darwin" ]]
then /usr/bin/open "$@" #OS X
else xdg-open "$@" &> /dev/null & # credit: http://stackoverflow.com/questions/264395
fi
@MattiSG
MattiSG / brewv
Created July 9, 2012 14:05
Install a previous version of a formula with Homebrew
#!/bin/bash
#
# Installs the previous version of a Homebrew formula
#
# Usage: brewv formula_name desired_version
#
# Based on http://stackoverflow.com/questions/3987683/homebrew-install-specific-version-of-formula#9832084
#
# Author: Matti Schneider <[email protected]> (http://mattischneider.fr)
@MattiSG
MattiSG / pagesPDFextract.sh
Created August 25, 2012 21:44
Extract PDF preview from Pages document
extract() {
if echo $1 | egrep -q '.pages\/?$'
then arg="$1"
trimmed="$(echo $arg | sed 's:/$::' | sed 's:\.pages::')"
else arg="$1.pages"
trimmed="$1"
echo "source : $arg/QuickLook/Preview.pdf"
echo "dest : $trimmed.pdf"
fi
if cp "$arg/QuickLook/Preview.pdf" "$trimmed.pdf"
@MattiSG
MattiSG / gist:3471250
Created August 25, 2012 21:45
Automatically install Symfony 1 and dependencies on OS X
if ! /usr/local/mysql/support-files/mysql.server start > /dev/null
then echo "MySQL package installed ? Check http://dev.mysql.com/downloads/mysql/5.1.html#macosx-dmg"
exit 1
fi
echo '[client]
socket = /var/mysql/mysql.sock
[mysqld]
@MattiSG
MattiSG / gist:3471254
Created August 25, 2012 21:45
Make links from URL list
# Outputs an HTML <a> list from a list of URLs, fetching destination page titles on the go
# param = input file, in which URLs are stored, one per line
for url in $(cat $1)
do
echo '<a href="'$url'">'
curl "$url" 2> /dev/null | grep '<title>' -A 1 | tail -1 | tr -s ' '
echo '</a>'
done
@MattiSG
MattiSG / deprecateTags.sh
Created October 5, 2012 15:54
Deprecate old git tags by prefixing them, but keeping their message if one is given.
OLD_NAME="intellicore"
for tag in $(git tag) # cleanup in case of half-done transition
do
if echo $tag | grep "$OLD_NAME"
then git tag -d $tag
fi
done
for tag in $(git tag)
@MattiSG
MattiSG / compare-gems.sh
Created October 10, 2012 07:14
Dirty shell to compare installed gems versions with the ones expected from a Gemfile
# Compares installed gems versions with the expected ones from a Gemfile.
# Ignores non-strict-equal dependencies (i.e. ~> 1.5 will output the same as = 1.5).
DEST_DIR="$HOME/Desktop/gems-comparison" # no trailing slash
GEMSET="mesh"
INSTALLED_GEMS_FILE="$DEST_DIR/installedGems.txt"
WANTED_GEMS_WITH_VERSIONS_FILE="$DEST_DIR/wantedGemsWithVersion.txt"
WANTED_GEMS_FILE="$DEST_DIR/wantedGems.txt"
@MattiSG
MattiSG / UpdateGrowlMail.sh
Created October 13, 2012 17:38
Automatically add GrowlMail plugin compatibility to the latest Mail.app version
# Updates the Growl Mail plugin when unknown compatibility preemptive deactivation happens.
# This consists in finding the “compatibility UUID” for the new Mail version, and adding it to the supported UUIDs of GrowlMail.
#
# See http://langui.sh/2009/11/09/fixing-growlmail-letterbox-for-mail-4-2/ for more details on the procedure.
#
# Author: Matti Schneider <[email protected]>
# Either ~/Library or /Library depending on the type of install (local or global).
INSTALL_ROOT="/Users/`whoami`/Library/Mail/" # the default is to assume a local install
BUNDLE_NAME="GrowlMail.mailbundle"
@MattiSG
MattiSG / set-arity.js
Created November 7, 2012 15:06
Set the arity of a function in JavaScript
/** Returns a string that looks like a function arguments list definition.
*
*@param {Number} count How many arguments should be generated.
*@returns {String} An arguments declaration list usable in a Function constructor.
*@private
*/
function declareArguments(count) {
return new Array(count).join('arg,') + 'arg';
}
@MattiSG
MattiSG / wrap.js
Created January 15, 2013 16:03
Wraps a substring of a string with the given prefix and suffix, ignoring case for finding while still respecting it in the resulting string.
/** Wraps a substring with the given prefix and suffix in a string.
*
*@param {String} hay The string in which the replacement is to occur.
*@param {String} needle The string to look for. Will be wrapped without case sensitivity, but will respect the case of the original string.
@param {String} prefix The string to insert before `needle`.
@param {String} suffix The string to insert after `needle`.
*/
function wrap(hay, needle, prefix, suffix) {
var lowHay = hay.toLowerCase(),
lowNeedle = needle.toLowerCase(),