Skip to content

Instantly share code, notes, and snippets.


Max Masnick masnick

View GitHub Profile
View ipython_pickle.applescript
tell application "Keyboard Maestro Engine"
-- IF KM Variable does NOT exist, the AS Variable will be set to empty string --
set the_path to getvariable "Path"
end tell
tell application "iTerm"
create window with default profile
masnick / bookmarklet.js
Last active Jul 26, 2017
Universal "find at my library" bookmarklet for journal articles
View bookmarklet.js
var url = '';
var doi;
var suffixes = {
"pmid": "?sid=Entrez:PubMed&id=pmid:",
"doi": "?sid=Entrez:DOI&id=doi:"
var re = /^[0-9]{5,15}/;
var found = jQuery(jQuery(".rprtid dd")[0]).text().match(re);
masnick / balance.scpt
Last active May 12, 2017
macOS: Set sound output balance to middle
View balance.scpt
-- Based on the AppleScript posted here:
tell application "System Preferences"
reveal anchor "output" of pane id ""
delay 0.5 -- If you get an error, it's possible this delay isn't long enough.
end tell
tell application "System Events"
tell slider 1 of group 1 of tab group 1 of window 1 of process "System Preferences"
set value to 0.5
end tell
masnick /
Last active Jul 14, 2017
Loading data from a stored proc with multiple results sets into a Pandas DataFrame
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine('mysql://{}:{}@{}/{}'.format(username, password, server, database_name))
connection = engine.connect().connection
cursor = connection.cursor()
cursor.execute('call storedProcName(%s, %s, ...)', params)
# Result set 1
View loadsql.ado
* NOTE: this ADO uses shell calls to run `rm`, so it could potentally delete files
* from your computer. Use at your own risk.
cap drop program loadsql
program define loadsql
*! Load the output of an SQL file into Stata, version 1.3 (
*! Based on
version 14.1
syntax using/, DSN(string) [User(string) Password(string) CLEAR NOQuote LOWercase SQLshow ALLSTRing DATESTRing]
masnick / md2jira.js
Created May 26, 2016
Convert Markdown to JIRA markup
View md2jira.js
function toJ(converted) {
converted = converted.replace(/^(.*?)\n([=-])+$/gm, function (match,content,level) {
return 'h' + (level[0] === '=' ? 1 : 2) + '. ' + content;
converted = converted.replace(/^([#]+)(.*?)$/gm, function (match,level,content) {
return 'h' + level.length + '.' + content;
masnick /
Last active May 23, 2016
pyenv setup instructions

OS X ships with an old version of Python 2. Here's how I set up my system:

brew install pyenv pyenv-virtualenv

Then add to .zshrc or .bashrc:

eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Then restart, and build the newest version of Python:

masnick / private.xml
Created May 9, 2016
Disable Command-F1 with Karabiner
View private.xml
<?xml version="1.0"?>
<name>Disable Command-F1</name>
<autogen>--KeyToKey-- ConsumerKeyCode::BRIGHTNESS_DOWN, ModifierFlag::COMMAND_L, ConsumerKeyCode::BRIGHTNESS_DOWN</autogen>
<autogen>--KeyToKey-- ConsumerKeyCode::BRIGHTNESS_DOWN, ModifierFlag::COMMAND_R, ConsumerKeyCode::BRIGHTNESS_DOWN</autogen>
masnick /
Last active Apr 28, 2016
Compare 2 SIRs in Python
Compare 2 SIRs by exact binomial and mid-p
This is a conversion of SAS code written by Minn Soe and published by the CDC.
The original SAS code is available here:
Author: Max Masnick, PhD (
Last modified: 2016-04-28
masnick /
Created Apr 22, 2016
Move newest file in Downloads folder to the frontmost Finder window
#! /usr/bin/env bash
current_folder=`osascript -e 'tell application "Finder"' -e "if (${1-1} <= (count Finder windows)) then" -e "get POSIX path of (target of window ${1-1} as alias)" -e 'else' -e 'get POSIX path of (desktop as alias)' -e 'end if' -e 'end tell'`
newest_file=`mdls -name kMDItemFSName -name kMDItemDateAdded -raw /Users/YOUR_USERNAME_HERE/Downloads/* | xargs -0 -I {} echo {} | sed 'N;s/\n/ /' | sort --reverse | head -1 | sed -E "s/^.*\\+0000 //"`
mv "/Users/YOUR_USERNAME_HERE/Downloads/$newest_file" "$current_folder"
osascript -e "display notification \"$newest_file$current_folder\" with title \"File moved\""