Feb posted 10 Feb 2010

sudo mdutil -a -i off
sudo chmod 600 /System/Library/CoreServices/Search.bundle/Contents/MacOS/Search
killall SystemUIServer

Source: http://www.macosxhints.com/article.php?story=20091030173117381

Sep posted 13 Sep 2009 and tagged

Amazon recently changed its Product API requirements and it was a little tricky to get the new signature working, so here’s an example of a product search in PHP.

function _encode_query($s) { $s = str_replace(',', urlencode(','), $s); $s = str_replace(':', urlencode(':'), $s); $s = str_replace(';', urlencode(';'), $s); return $s; } function _encode_signature($s) { $s = str_replace('+', urlencode('+'), $s); $s = str_replace('=', urlencode('='), $s); return $s; } function fetch($power_query, $access_key_id, $associate_tag) { $url_base = 'http://webservices.amazon.com/onca/xml?'; $url_params = array( 'Operation' => 'ItemSearch', 'Service' => 'AWSECommerceService', 'AWSAccessKeyId' => $access_key_id, 'Version' => '2009-01-06', 'Sort' => 'salesrank', 'ResponseGroup' => 'ItemAttributes,Images', 'SearchIndex' => 'Books', 'Power' => $power_query, 'Availability' => 'Available', 'Condition' => 'All', 'Timestamp' => gmdate('Y-m-d\TH:i:s\Z') ); $pairs = array(); foreach($url_params as $k => $v) { $pairs[] = $k.'='.$this->_encode_query($v); } sort($pairs); $canonical = implode('&', $pairs); $base = 'GET'.PHP_EOL.'webservices.amazon.com'.PHP_EOL.'/onca/xml'.PHP_EOL; $to_sign = $base.$canonical; $signature = base64_encode(hash_hmac('sha256', $to_sign, $associate_tag, true)); $signature = $this->_encode_signature($signature); $request = $url_base.$canonical.'&Signature='.$signature; $response = file_get_contents($request); $xml = simplexml_load_string($response); if (!is_object($xml)) { return null; } else { $products = array(); if (sizeof($xml->Items->Item) > 0) { foreach($xml->Items->Item as $item) { $products[] = $item; } } return $products; } }

Pop out

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
function _encode_query($s) {
  $s = str_replace(',', urlencode(','), $s);
  $s = str_replace(':', urlencode(':'), $s);
  $s = str_replace(';', urlencode(';'), $s);
  return $s;
}
 
function _encode_signature($s) {
  $s = str_replace('+', urlencode('+'), $s);
  $s = str_replace('=', urlencode('='), $s);
  return $s;
}
 
function fetch($power_query, $access_key_id, $associate_tag) {
  $url_base = 'http://webservices.amazon.com/onca/xml?';
  $url_params = array(
    'Operation' => 'ItemSearch',
    'Service' => 'AWSECommerceService',
    'AWSAccessKeyId' => $access_key_id,
    'Version' => '2009-01-06',
    'Sort' => 'salesrank',
    'ResponseGroup' => 'ItemAttributes,Images',
    'SearchIndex' => 'Books',
    'Power' => $power_query,
    'Availability' => 'Available',
    'Condition' => 'All',
    'Timestamp' => gmdate('Y-m-d\TH:i:s\Z')
  );
  $pairs = array();
  foreach($url_params as $k => $v) {
    $pairs[] = $k.'='.$this->_encode_query($v);
  }
  sort($pairs);
  $canonical = implode('&', $pairs);
  $base = 'GET'.PHP_EOL.'webservices.amazon.com'.PHP_EOL.'/onca/xml'.PHP_EOL;
  $to_sign = $base.$canonical;
  $signature = base64_encode(hash_hmac('sha256', $to_sign, $associate_tag, true));
  $signature = $this->_encode_signature($signature);
  $request = $url_base.$canonical.'&Signature='.$signature;
  $response = file_get_contents($request);
  $xml = simplexml_load_string($response);
  if (!is_object($xml)) {
    return null;
  } else {
    $products = array();
    if (sizeof($xml->Items->Item) > 0) {
      foreach($xml->Items->Item as $item) {
        $products[] = $item;
      }
    }
    return $products;
  }
}

Sep posted 13 Sep 2009 and tagged

This function generates a linear gradient between two hex values. Specify the number of hex values in the returned array with $color_steps. (Haven’t used this in a long time, but if I’m not more diligent about throwing snippets up here, I’ll lose track of them forever.)

function get_gradient($hex_from, $hex_to, $color_steps) { $from_rgb = array( 'r' => hexdec(substr($hex_from, 0, 2)), 'g' => hexdec(substr($hex_from, 2, 2)), 'b' => hexdec(substr($hex_from, 4, 2)) ); $to_rgb = array( 'r' => hexdec(substr($hex_to, 0, 2)), 'g' => hexdec(substr($hex_to, 2, 2)), 'b' => hexdec(substr($hex_to, 4, 2)) ); $step_rgb = array( 'r' => ($from_rgb['r'] - $to_rgb['r']) / ($color_steps - 1), 'g' => ($from_rgb['g'] - $to_rgb['g']) / ($color_steps - 1), 'b' => ($from_rgb['b'] - $to_rgb['b']) / ($color_steps - 1) ); $gradient_colors = array(); for($i = 0; $i < $color_steps; $i++) { $rgb = array( 'r' => floor($from_rgb['r'] - ($step_rgb['r'] * $i)), 'g' => floor($from_rgb['g'] - ($step_rgb['g'] * $i)), 'b' => floor($from_rgb['b'] - ($step_rgb['b'] * $i)) ); $hex_rgb = array( 'r' => sprintf('%02x', ($rgb['r'])), 'g' => sprintf('%02x', ($rgb['g'])), 'b' => sprintf('%02x', ($rgb['b'])) ); $gradient_colors[] = implode(null, $hex_rgb); } return $gradient_colors; }

Pop out

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function get_gradient($hex_from, $hex_to, $color_steps) {
  $from_rgb = array(
    'r' => hexdec(substr($hex_from, 0, 2)),
    'g' => hexdec(substr($hex_from, 2, 2)),
    'b' => hexdec(substr($hex_from, 4, 2))
  );
  $to_rgb = array(
    'r' => hexdec(substr($hex_to, 0, 2)),
    'g' => hexdec(substr($hex_to, 2, 2)),
    'b' => hexdec(substr($hex_to, 4, 2))
  );
  $step_rgb = array(
    'r' => ($from_rgb['r'] - $to_rgb['r']) / ($color_steps - 1),
    'g' => ($from_rgb['g'] - $to_rgb['g']) / ($color_steps - 1),
    'b' => ($from_rgb['b'] - $to_rgb['b']) / ($color_steps - 1)
  );
  $gradient_colors = array();
  for($i = 0; $i < $color_steps; $i++) {
    $rgb = array(
      'r' => floor($from_rgb['r'] - ($step_rgb['r'] * $i)),
      'g' => floor($from_rgb['g'] - ($step_rgb['g'] * $i)),
      'b' => floor($from_rgb['b'] - ($step_rgb['b'] * $i))
    );
    $hex_rgb = array(
      'r' => sprintf('%02x', ($rgb['r'])),
      'g' => sprintf('%02x', ($rgb['g'])),
      'b' => sprintf('%02x', ($rgb['b']))
    );
    $gradient_colors[] = implode(null, $hex_rgb);
  }
  return $gradient_colors;
}

Example:

get_gradient('#0084b4', '#a9e0f4', 10); ( [0] => 0084b4 [1] => 128ebb [2] => 2598c2 [3] => 38a2c9 [4] => 4bacd0 [5] => 5db7d7 [6] => 70c1de [7] => 83cbe5 [8] => 96d5ec [9] => a9e0f4 )

Pop out

1
2
3
4
5
6
7
8
9
10
11
12
13
14
get_gradient('#0084b4', '#a9e0f4', 10);
 
(
    [0] => 0084b4
    [1] => 128ebb
    [2] => 2598c2
    [3] => 38a2c9
    [4] => 4bacd0
    [5] => 5db7d7
    [6] => 70c1de
    [7] => 83cbe5
    [8] => 96d5ec
    [9] => a9e0f4
)

Sep posted 13 Sep 2009

For all your pursuits on the, er, up-and-up …

Search engine bots:

  • Google – Googlebot/2.1 ( http://www.googlebot.com/bot.html)
  • Google Image – Googlebot-Image/1.0 ( http://www.googlebot.com/bot.html)
  • MSN Live – msnbot-Products/1.0 (+http://search.msn.com/msnbot.htm)
  • Yahoo – Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)
  • ask
  • Browsers:

    • Firefox (WindowsXP) – Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6
    • IE 7 – Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)
    • IE 6 – Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)
      Safari – Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en) AppleWebKit/522.11 (KHTML, like Gecko) Safari/3.0.2
    • Opera – Opera/9.00 (Windows NT 5.1; U; en)

    Source: http://www.developertutorials.com/tutorials/php/scraping-links-with-php-8-01-05/page3.html

Aug posted 12 Aug 2009 and tagged ,

Add this to ~/.bash_profile, ~/.bashrc, or wherever you prefer. You can then recursively remove annoying .DS_Store files from a directory by executing the command rm_dss.

alias rm_dss='find . -name *.DS_Store -type f -exec rm {} \;'

Pop out

1
alias rm_dss='find . -name *.DS_Store -type f -exec rm {} \;'

Source: http://snippets.dzone.com/posts/show/4982

Jun posted 19 Jun 2009 and tagged

How handy! Lifehacker says:

Google Docs Download, a Greasemonkey script previously featured here for creating a Google Docs bulk download feature that should already be there, is now available as a (beta-level) Python script.

May posted 31 May 2009 and tagged , ,

This function pipes a javascript file to a Python implementation of Douglas Crockford’s handy JSMin, a javascript minifier.

minify() { if [ -f $1 ] then MIN=${1%.[^.]*}.min.js cat $1 | python ~/path/to/jsmin.py > ${1%.[^.]*}.min.js BEFORE=`wc -c <$1` AFTER=`wc -c <$MIN` echo "$BEFORE $1" echo "$AFTER $MIN ($(echo "scale=2; 100*$AFTER/$BEFORE" | bc)%)" else echo "$1 not found" fi }

Pop out

1
2
3
4
5
6
7
8
9
10
11
12
13
minify() {
  if [ -f $1 ]
  then
    MIN=${1%.[^.]*}.min.js
    cat $1 | python ~/path/to/jsmin.py > ${1%.[^.]*}.min.js
    BEFORE=`wc -c <$1`
    AFTER=`wc -c <$MIN`
    echo "$BEFORE $1"
    echo "$AFTER $MIN ($(echo "scale=2; 100*$AFTER/$BEFORE" | bc)%)"
  else
    echo "$1 not found"
  fi
}

It also prints the minified version’s character count as a percentage of the original’s:

$ minify prototype.js 

  126127 prototype.js
   93689 prototype.min.js (74.28%)

May posted 14 May 2009 and tagged

import os

Pop out

1
import os

List all files in a directory recursively, with relative paths:

def list_dir(dir_name): for r, d, f in os.walk(dir_name): files = [os.path.join(r, n) for n in f if not n.startswith('.')] return files

Pop out

1
2
3
4
def list_dir(dir_name):
    for r, d, f in os.walk(dir_name):
        files = [os.path.join(r, n) for n in f if not n.startswith('.')]
    return files

Create a directory if it doesn’t already exist:

def make_dir(dir_name): if not os.path.isdir(dir_name): os.mkdir(dir_name)

Pop out

1
2
3
def make_dir(dir_name):
    if not os.path.isdir(dir_name):
        os.mkdir(dir_name)

Empty an existing directory:

def empty_dir(dir_name): if not os.path.isdir(dir_name): os.mkdir(dir_name) else: [os.remove(f) for f in list_dir(dir_name)]

Pop out

1
2
3
4
5
def empty_dir(dir_name):
    if not os.path.isdir(dir_name):
        os.mkdir(dir_name)
    else:
        [os.remove(f) for f in list_dir(dir_name)]

May posted 10 May 2009 and tagged

^a c
Create new window (shell)
^a k
Kill the current window
^a w
List all windows
(the current window is marked with *)
^a 0-9
Go to a window numbered 0-9
^a n
Go to the next window
^a ^a
Toggle between the current and previous window
^a [
Start copy mode
^a ]
Paste copied text
^a ?
Help (display a list of commands)
^a ^\
Quit screen
^a D
Power detach and logout
^a d
Detach but keep shell window open
space or return
End a command

Source: Indiana University UITS

May posted 4 May 2009 and tagged ,

RewriteEngine On RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC] RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]

Pop out

1
2
3
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]