Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 11-20 of 46 total

Repair Spy

Takes a screenshot and a webcam snapshot and emails them to flickr. Requires isightcapture. Hacked up to spy on the Apple repair team as they try to repair my Mac Book Pro's backlight for the second time.

#!/bin/bash
# requires isightcapture http://www.intergalactic.de/hacks.html

# path to save the images
PATH=/Users/default/

# flickr email address
EMAIL="yourflickremail+private@photos.flickr.com"

# tags to attach to each picture uploaded
TAGS="repair_spy"

TIMESTAMP=`/bin/date "+%y%m%d%H%M%S"`
FRIENDLY_TIMESTAMP=`/bin/date "+%Y/%m/%d %H:%M"`

#take pics
/usr/sbin/screencapture -mxC $PATHscreen_$TIMESTAMP.png
/usr/local/bin/isightcapture -t png $PATHisight_$TIMESTAMP.png

#post to flickr
/usr/bin/uuencode $PATHscreen_$TIMESTAMP.png $PATHscreen_$TIMESTAMP.png | /usr/bin/mail -s "Repair Spy Screenshot - $FRIENDLY_TIMESTAMP tags: $TAGS" $EMAIL
/usr/bin/uuencode $PATHisight_$TIMESTAMP.png $PATHisight_$TIMESTAMP.png | /usr/bin/mail -s "Repair Spy iSight Cature - $FRIENDLY_TIMESTAMP tags: $TAGS" $EMAIL


Toss this in your crontab like so, making sure to run it as the user that you provide to the Apple Techs

*/1    *       *       *       *       default /Users/default/repair_spy >& /dev/null

Get Frontmost Window

tell application "System Events"
	set frontmostApplication to name of the first process whose frontmost is true
end tell

Objective-C and Cocoa: Human-readable file size from number of bytes

// Returns a human-readable string showing the file size from the number of bytes

- (NSString *)stringFromFileSize:(int)theSize
{
	float floatSize = theSize;
	if (theSize<1023)
		return([NSString stringWithFormat:@"%i bytes",theSize]);
	floatSize = floatSize / 1024;
	if (floatSize<1023)
		return([NSString stringWithFormat:@"%1.1f KB",floatSize]);
	floatSize = floatSize / 1024;
	if (floatSize<1023)
		return([NSString stringWithFormat:@"%1.1f MB",floatSize]);
	floatSize = floatSize / 1024;

	// Add as many as you like

	return([NSString stringWithFormat:@"%1.1f GB",floatSize]);
}

Syntax highlighting with vim & Ruby

From: http://www.ruby-forum.com/topic/64262

This snippet will convert source code files to syntax-colored html files.
usage: vim-syntax-highlighting.rb src.rb src.html



require 'tempfile'

$VERBOSE=nil
STDERR.reopen(Tempfile::new($$)) unless STDIN.tty?

fin, fout, _ = ARGV
fin = ((fin.nil? or fin == '-') ? STDIN : open(fin))
fout = ((fout.nil? or fout == '-') ? STDOUT : open(fout,'w+'))

ts = Tempfile::new($$), Tempfile::new($$)
ts[0].write fin.read
ts.each{|t| t.close}
command = %Q( vim -f +'syn on' +'set filetype=ruby' +'set background=light' +'run! syntax/2html.vim' +'w! #{ ts[1].path }' +'qa!' - < #{ ts[0].path } > /dev/null 2>&1 )
#command = %Q( vim -f +'syn on' +'set filetype=c' +'set background=dark' +'run! syntax/2html.vim' +'w! #{ ts[1].path }' +'qa!' - < #{ ts[0].path } > /dev/null 2>&1 )
system command
ts.each{|t| t.open; t.rewind}
fout.write(ts[1].read)
ts.each{|t| t.close!}

Rebuild the locate library on OSX

// Rebuilds OSX's locate library. Behaves the same as updatedb on other systems.

sudo /usr/libexec/locate.updatedb

rsync backup of a Mac

sudo /usr/local/bin/rsync -aREx --delete --exclude='.Spotlight-*' --exclude '/private/var/vm/*' [IP-address of Mac mini]::PowerBookBackup

Run R from the command line

// This runs R from the command line in batch mode.
// The log file is output to track R's progress

R CMD BATCH --vanilla path/to/file.R file.log &

OSX Restart Lookupd

sudo kill -HUP `ps -ax | grep lookupd | grep sbin | awk '/(\d+).*/ { print $1 }'`

Fix Terminal spacing bug

defaults write com.apple.Terminal FontWidthSpacing 1.003

Building tcpflow on OS X

curl -O http://www.circlemud.org/pub/jelson/tcpflow/tcpflow-0.21.tar.gz
tar -zxvf tcpflow-0.21.tar.gz
cd tcpflow-0.21
cp /usr/share/libtool/config.* . 
./configure --prefix=/usr/local
sudo make install
« Newer Snippets
Older Snippets »
Showing 11-20 of 46 total