Thursday, March 12, 2009

Die GPT die!

From time to time you move hard drive between systems that uses GPT by default (Solaris with ZFS, Apple MacOS etc) and system that don't (Windows, Linux, BSDs) you might get annoyed by your favourite partitioning tools in the later systems for not having GPT support. The solution? Kill the GPT signatures and continue to use your favourite tool!

Problem is that GPT records exists in both the beginning AND the end of a disk - so the usual dd if=/dev/zero of=/dev/yournewdisk count=100 trick won't work. Of course you can wipe the whole drive with dd - but that would be slow and unnecessary. So what to do? Python come to rescue! The following script will completely nuke your partition table on a given disk so that you can start from scratch - make sure you know what you are doing!



#!/usr/bin/env python
import sys

'''
Nuke GPT on any given block device.

lestercheung <<<<<<<<<>>>>>>>>> gmail.com
'''

def ftell_test(d):
f = open(d, "r")
f.seek(0, 2) # SEEK_END is 2
return f.tell()

def clear_gpt(target):
'''
According to http://en.wikipedia.org/wiki/GUID_Partition_Table - GPT
stores partition data in the first and last 34 LBA blocks. A LBA sector
is normally 512 bytes.
'''
fd = open(target, "w+")
fd.seek(0)
fd.write('\0' * 34 * 512)
print "done nuking data at the beginning of disk", target
fd.seek(0, 2) # SEEK_END is 2
disk_size = fd.tell()
fd.seek(disk_size - 34*512)
fd.write('\0' * 34 * 512)
print "done nuking data at the end of disk", target

if __name__ == '__main__':
for x in sys.argv[1:]:
#print ftell_test(x)
clear_gpt(x)

Thursday, March 05, 2009

A handy applescript that I use to reclaim 15mins my time, everyday

This script turns a selection of text in Safari and turn it to a audiobook in iTunes, optionally sync it to your iphone/ipod.

Why only with Safari? Mainly so that I can extract page's URL and put them in the audiobook's metadata.

-- -*- mode: applescript -*- --
-- My first applescript:
-- This script makes a text selection to speech and import to iTunes
-- much of it is a total rip off from other scripts you see online,
-- especially those from http://dougscripts.com/. Thanks Doug!
--
-- 13z73rch3un9 [at] gmail.com

-- configurables -------------
set tmpDir to (path to desktop folder as text)
set plName to "reading materials"
set myIpodName to "NAME OF YOUR IPHONE/IPOD"
---------------------------------

tell application "Safari"
set selecTxt to (do JavaScript "(getSelection())" in document 1)
-- set pgtitle to quoted form of (do JavaScript "document.title" in document 1)
set pgtitle to (do JavaScript "document.title" in document 1)
set pgurl to (do JavaScript "document.URL" in document 1)
end tell

set pgtitle to replace(pgtitle, ":", " -")
set aiffFile to tmpDir & pgtitle & ".aiff"

say selecTxt saving to aiffFile

-- import to iTunes and add ID3 tags
tell application "iTunes"
set rawTrack to (add aiffFile)
tell application "Finder" to delete aiffFile

-- metadata
set name of rawTrack to pgtitle
set ds to ((current date) as string)
set comment of rawTrack to "Imported from Safari on " & ds & return & "source: " & pgurl
set genre of rawTrack to "reading material"
set lyrics of rawTrack to selecTxt
-- I don't know why this doesn't work....
-- set y to ((year of (current date)) as string)
-- set year of myTrack to y
set year of rawTrack to (do shell script "/bin/date +%Y")

-- transcode
set defaultEncoder to current encoder
set aacEncoder to (item 1 of (every encoder whose format is "AAC"))
set current encoder to aacEncoder
set m4aTrack to item 1 of (convert rawTrack)
set current encoder to defaultEncoder
delete rawTrack
set m4aLocation to location of m4aTrack
tell application "Finder"
set m4aAlias to m4aLocation as alias
set file type of m4aAlias to "M4B "
if name extension of m4aAlias is not "m4b" then
set name of m4aAlias to ((text 1 thru -((length of (get name extension of m4aAlias)) + 1) of (name of m4aAlias as text)) & "m4b") as string
end if
end tell
delete m4aTrack
set m4bTrack to add m4aAlias
reveal m4bTrack
-- uncomment this to sync ipod/iphone connected, change your device name!
try
update (some source whose name is myIpodName)
on error
display alert "Your iPod is not connected, not syncing... (Have you defined your ipod name in the script?)"
end try
end tell


tell application "Safari" to activate



on replace(str, sstr, newstr)
set rtn to ""
repeat with i from 1 to (length of text of str) by 1
set c to text i of str
if c is equal to sstr then
set rtn to rtn & newstr
else
set rtn to rtn & c
end if
end repeat
return rtn
end replace