Skip to content

Instantly share code, notes, and snippets.

@samrayner
samrayner / singleton.php
Created September 5, 2011 11:53
Singleton method
public static $instance;
public static function get_instance() {
$class = get_class(this);
if(!self::$instance)
self::$instance = new $class();
return self::$instance;
}
@samrayner
samrayner / splash.js
Created March 17, 2012 12:55
Serve splash images for iPhone/iPad models
(function () {
var width = 320; //default to non-retina iPhone
var height = 460;
if(navigator.platform === 'iPad') {
if(window.orientation === 90 || window.orientation === -90) {
width = 1024; //landscape
height = 748;
}
else {
@samrayner
samrayner / top-url.sh
Created August 28, 2012 10:43
Get URL of top Google result for a query
api=http://ajax.googleapis.com/ajax/services/search/web?v=1.0
url=$(curl -s --get --data-urlencode "q=$1" $api |
sed 's|"unescapedUrl":"\([^"]*\).*|\1|;s|.*GwebSearch",||')
echo -n $url | pbcopy
echo $url
@samrayner
samrayner / toggle-invisible.sh
Created August 28, 2012 10:45
Toggle invisible files in OS X
if [ `defaults read com.apple.finder AppleShowAllFiles` == 1 ]
then
defaults write com.apple.finder AppleShowAllFiles -bool false
else
defaults write com.apple.finder AppleShowAllFiles -bool true
fi
killall Finder
@samrayner
samrayner / shownotes.scpt
Created August 28, 2012 10:47
Open link list for the 5by5 show currently playing in iTunes
tell application "System Events"
set itunesRunning to count of (every process whose name is "iTunes")
if itunesRunning is 0 then return
end tell
tell application "iTunes"
if player state is stopped then return
set shows to {{title:"Hypercritical", slug:"hypercritical"}, {title:"The Talk Show", slug:"talkshow"}, {title:"Build and Analyze", slug:"buildanalyze"}, {title:"After Dark", slug:"afterdark"}, {title:"The Pipeline", slug:"pipeline"}, {title:"5by5 Specials", slug:"specials"}, {title:"Back To Work", slug:"b2w"}, {title:"Mac Power Users", slug:"mpu"}, {title:"The Incomparable", slug:"incomparable"}, {title:"The Cocktail Napkin", slug:"tcn"}, {title:"Latest in Paleo", slug:"paleo"}, {title:"Critical Path", slug:"criticalpath"}, {title:"The Big Web Show", slug:"bigwebshow"}, {title:"Content Talks", slug:"contenttalks"}, {title:"Founders Talk", slug:"founderstalk"}, {title:"The Ihnatko Almanac", slug:"ia"}, {title:"Geek Friday", slug:"geekfriday"}, {title:"Internet Superhero", slug:"superhero"}, {title
@samrayner
samrayner / install-dls.scpt
Created August 28, 2012 10:49
Install .app files from ~/Downloads
tell application "Finder"
set dlApps to every file in (path to downloads folder) whose name extension is "app"
repeat with newApp in dlApps
move newApp to (path to applications folder) with replacing
end repeat
end tell
@samrayner
samrayner / open-in-chrome.scpt
Created August 28, 2012 10:50
Open Safari tab in Google Chrome
tell application "Safari" to set currentURL to URL of current tab of window 1
tell application "Google Chrome"
activate
delay 0.2
if (exists window 1) and (URL of active tab of window 1 is "chrome://newtab/") then
tell window 1 to set URL of active tab to currentURL
else
open location currentURL
end if
end tell
@samrayner
samrayner / focus_cell.m
Last active October 13, 2015 18:58
Select first UITextField or UITextView of a UITableViewCell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSArray *subviews = [cell.contentView subviews];
for(UIView *subview in subviews) {
if([subview isKindOfClass:[UITextField class]] || [subview isKindOfClass:[UITextView class]]) {
[subview becomeFirstResponder];
break;
}
@samrayner
samrayner / tabindex.m
Last active October 13, 2015 18:58
Use UITextField tags as tabindex for a UIView
-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
NSInteger nextTag = textField.tag + 1;
UIResponder *nextResponder = [self.view viewWithTag:nextTag];
if(nextResponder) {
[nextResponder becomeFirstResponder];
}
else {
[self.view endEditing:YES];
}
@samrayner
samrayner / parent_cell.m
Last active October 13, 2015 19:07
Select nearest parent UITableViewCell of a view
- (void)selectParentTableViewCell:(UIView *)view
{
//maximum 5 attempts
for(int i = 0; i < 5; i++) {
view = view.superview;
if([view isKindOfClass:[UITableViewCell class]]) {
UITableViewCell *cell = (UITableViewCell *)view;
[self.tableView selectRowAtIndexPath:[self.tableView indexPathForCell:cell] animated:NO scrollPosition:UITableViewScrollPositionNone];
break;
}