Stochastic Nonsense

Put something smart here.

Make Private Mode Work in Safari

As I mentioned, private mode is broken in safari. There is, however, a workaround:

  1. start safari
  2. start private mode
  3. browse secret websites!
  4. exit private mode
  5. close all your tabs and the window
  6. open a new window (this window will not be in private mode)
  7. close the new window

And it works! amazon searches no longer persist! You can now exit safari. This works on osx 10.7.4 build 11E53 with safari 5.1.7.

Alternately… just get chrome. It works out of box.

Quoting for Hacker News

I hate hand quoting text for hacker news posts. Mostly as a memo to myself, this command will wrap stdin to 77 columns preferentially breaking on spaces, insert the first 3 spaces so that HN recognizes the text as a quote, then leave the output in my copybuffer.

1
echo 'the stuff I want to quote here' | fold -w 77 -s | sed "s/^/   /" | pbcopy

Feel free to use pbpaste instead of echo.

Howto Make Find Xargs Grep Robust to Spaces in Filenames on a Mac

The unix pattern for filtering files with a predicate then searching within them is find | xargs | grep. For example, to search every file whose filename contains notes for a line containing mysql

1
$ find . -iname "*notes*" | xargs grep -i mysql

Unfortunately, on OSX this is not robust to spaces or quotes in filenames. Thus if you have a filename like

1
./Dropquest 2012/Captain's Logs/Chapter 1.txt

in your search path the typical find | xargs | grep invocation will terminate with the error

1
xargs: unterminated quote

The first thing to know is you can use the -t parameter in xargs to at least tell you which filename it’s dying on, but that’s of limited use in making the command work. Even using -I{} with xargs and grep to surround the filename with quotes doesn’t fix this.

1
$ find . -iname "*notes*" | xargs -I{} grep -i mysql "{}"

Many people must have run into this problem because there is a simple solution that all the tools understand: use nulls instead of newlines to delimit files.

1
$ find . -iname "*notes*" -print0 | xargs -I{} -0 grep -i mysql "{}"

and it works!

Transpose or Pivot From Bash

I recently had a set of data in rows that I wanted to put in columns, just like transpose does in excel. Here’s a little ruby script that will do it. Ideally, I’d extend this to take a -F argument to control what the script splits on just like awk.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ cat bin/transpose
#!/usr/bin/ruby

# otherwise reading blocks
exit if STDIN.tty?

lines = []
STDIN.each do |line|
lines << line.strip.split(',').each{ |x| x.strip! }
end

columns = lines.shift
columns = columns.zip(*lines)

columns.each do |column|
puts "#{ column.join(', ') }"
end

usage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ echo "col1,0, 0.001, 0.005, 0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 0.99, 0.999, 1.0
col2,0.0,1.0,1.0,1.0,2.0,2.0,2.0,4.0,5.0,7.0,9.0,14.0,22.0,47.0,85.0,258.0,1127.0,1834676.0" | transpose
col1, col2
0, 0.0
0.001, 1.0
0.005, 1.0
0.01, 1.0
0.05, 2.0
0.1, 2.0
0.2, 2.0
0.3, 4.0
0.4, 5.0
0.5, 7.0
0.6, 9.0
0.7, 14.0
0.8, 22.0
0.9, 47.0
0.95, 85.0
0.99, 258.0
0.999, 1127.0
1.0, 1834676.0

Two Quotes About the Internet

I’m not going to apologize for the cost,” Zimmermann told CNET, adding that the final price has not been set. “This is not Facebook. Our customers are customers. They’re not products. They’re not part of the inventory.

If you are not paying for it, you’re not the customer; you’re the product being sold.

Calculate SHA1 on OSX

1
2
$ echo "my string here" | openssl sha1
(stdin)= 0669c6958b14ae3454f8679ec12adbefadef5440

Alternatively,

1
2
$ echo "my string here" | shasum
0669c6958b14ae3454f8679ec12adbefadef5440  -

Engineering Recruiting

From recruiter email:

Something new is brewing and it’s pretty damn epic. I’m working with 5 firms. I’m not going to go into detail on all of them, but you should know that things are heating up in the bay area in a big way. I have opportunities for everyone to escalate their careers to the next level. So even if you feel that this opportunity may not be for you, I DO have something for you.

The 1st is an outrageously cool startup that is absolutely going to change their market radically.

The 2nd is a leader in the free-to-play core social games arena. They’re on FIRE! Seeking Lead & Mid-Sr SW Engineers w/ strong Ruby/Rails, and a Dir of Eng.

The first firm is on the verge of a extremely profitable explosion. In a recent Wall Street Journal article they are touted as an extremely viable game changer that’ll change the face of their market.

Safari Private Mode Is Broken

Safari private mode is reproducibly broken as of 2012 0524 on my completely updated macbook running OSX 10.7.4 and Safari 5.1.7 (7534.57.2). Software update lists no available updates.

My expectation, as set by the dialog box displayed when entering private browsing mode, was that no cookies created during the private browsing session would persist. The dialog box doesn’t specifically mention cookies, but the help accessed via clicking the question mark on the dialog box has this to say:

Any changes made to cookies are discarded when you turn off Private Browsing.

safari help

Therefore I thought that if I started safari, entered private mode, logged in to amazon, exited private mode, then restarted private mode and returned to amazon that I would be logged out because amazon would have no cookies to identify me. Instead, I’m still logged in to amazon and amazon remembers my session. This is true even if I quit safari after exiting private mode and before restarting it, verifying the process has exited via

1
2
$ ps aux | grep -i safari
earl           46104   0.0  0.0  2434892    572 s006  S+    2:23PM   0:00.00 grep -i safari

Reproduction steps:

  1. start safari
  2. clear all stored data (Preferences –> Privacy tab –> Remove All Website Data)
  3. start private mode
  4. navigate to amazon.com
  5. verify no amazon cookies exist (Develop –> Web Inspector –> delete amazon.com cookies, if any)
  6. login to amazon
  7. exit private mode
  8. quit safari and verify process ending via ps, as above
  9. start safari
  10. . start private mode
  11. . navigate to amazon.com
  12. . your amazon.com user is still logged in

Note that if you exit private mode then immediately reenter private mode without quitting safari, the amazon.com cookies created in private mode are deleted as safari claims. That is, if you replace step 7 above with:

  1. start private mode again
  2. navigate to amazon.com

Then your user will not be logged in and cookies will not have persisted.

Edit: as of 2012 0706, I found a workaround.

Useful OSX Utilities for Working in Bash

OS X has a bunch of os-specific utilities that make working in bash just a bit nicer.

To turn a date in seconds into a human readable date, use:

convert a date in seconds to human readable
1
2
$ date -r 1334500523
Sun Apr 15 07:35:23 PDT 2012

You can use pbcopy to copy something into the clipboard. So for example, cat a file and place it in the system copy buffer:

pbcopy copies to clipboard
1
$ cat ./myfile | pbcopy

Similarly, pbpaste will paste from the clipboard, so to write the contents of your clipboard to a file:

pbpaste copies from the clipboard
1
$ pbpaste > myfile

To notify yourself after a long job has finished, for example uploading a new jar to a remote server, you can use say to have a voice read words to you over the speakers or use growlnotify to send yourself a growl message:

notifications
1
2
3
4
5
$ say "done"
$
$ # or
$
$ growlnotify -m "done"

(growl unfortunately started charging money which is somewhat annoying, though 1.2.2 seems to still work)

so for example:

1
$ rsync --progress output/job.jar $HOST/job.jar && say "done" && growlnotify "done"

To open the current directory in finder:

open the current directory
1
$ open .

If you add an external drive such as a usb stick, the mount point is in /Volumes:

1
2
3
4
5
laptop:~ earl$ ll /Volumes/
total 8
drwxrwxrwt@  3 root  admin   102B May 21 23:04 .
drwxr-xr-x  31 root  wheel   1.1K May 21 23:28 ..
lrwxr-xr-x   1 root  admin     1B May 14 13:09 Macintosh HD -> /

You can use spotlight from the command line as well with mdfind:

use spotlight from bash
1
2
3
4
5
$ mdfind macintosh | head -4
/Users/earl/Library/Application Support/TextWrangler/TextWrangler User Manual (4.0).pdf
/opt/local/include/lzo/lzodefs.h
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/scons-2.0.1/SCons/compat/_scons_subprocess.py
/opt/local/share/doc/boost/libs/ratio/example/config.hpp

And finally, you can create screenshots with screencapture.

trigger screenshot from bash
1
$ screencapture capture.png