Tuesday, June 9, 2009

Automator workflow for interleaving odd and even pages of PDF scans

I have made an Automator workflow that combines two PDFs, by shuffling (interleaving them), but after reversing the second one (second in terms of creation time). This means that you can scan a whole lot of pages (odd sides), turn them over and scan the other sides (even sides), then right click, choose an Automator workflow to combine them. They become Combined.pdf in the same folder - the originals are moved to the Trash.

Uses the python script automator action

This was something also wanted here.

The automator action starts with a Get Selected Finder Items action, then has a Run Python Script action with the following code.

import os
from CoreGraphics import *

def createPDFDocumentWithPath(path):
print path
return CGPDFDocumentCreateWithProvider(CGDataProviderCreateWithFilename(path))
def duplexPages(writeContext, oddfile, evenfile):
# open PDFDocuments for each of the files.
odd = createPDFDocumentWithPath( oddfile )
even = createPDFDocumentWithPath( evenfile )

# confirm that the number of pages is the same
maxPages = odd.getNumberOfPages()
if ( maxPages <> even.getNumberOfPages() ):
print "Documents do not have the same number of pages"
else:
# Shuffle the pages
for pageNum in xrange(1, maxPages + 1):
# start at first odd page
page = odd.getPage(pageNum)
print 'Page', pageNum, 'from', oddfile
if page != None:
mediaBox = odd.getMediaBox(pageNum)
writeContext.beginPage(mediaBox)
writeContext.drawPDFDocument(mediaBox, odd, pageNum)
writeContext.endPage()
# start at last even page
page = even.getPage(maxPages + 1 - pageNum)
print 'Page', maxPages + 1 - pageNum, 'from', evenfile
if page != None:
mediaBox = even.getMediaBox(maxPages + 1 - pageNum)
writeContext.beginPage(mediaBox)
writeContext.drawPDFDocument(mediaBox, even, maxPages + 1 - pageNum)
writeContext.endPage()
def realFilesOnly( item ):
return item.find(".") <> 0

# from http://stackoverflow.com/questions/249785/os-x-determine-trash-location-for-a-given-path
def get_trash_path(input_file):
    path, file = os.path.split(input_file)
    if path.startswith("/Volumes/"):
        # /Volumes/driveName/.Trashes/
        s = path.split(os.path.sep)
        # s[2] is drive name ([0] is empty, [1] is Volumes)
        trash_path = os.path.join("/Volumes", s[2], ".Trashes", str(os.getuid()))
        if not os.path.isdir(trash_path):
            raise IOError("Volume appears to be a network drive (%s could not be found)" % (trash_path))
    else:
        trash_path = os.path.join(os.getenv("HOME"), ".Trash")
    return trash_path
def main(input, *args, **kwargs):
dirname = os.path.dirname(input[0])    # extract folder to work in
# print dirname
files = input
# print files
# files = os.listdir( dirname )
# files = filter(realFilesOnly, files) # filter out special files
# print files # enable to see which files are selected
if ( len(files) == 2 ):
# files[0] = os.path.join(dirname,files[0]) # add path to files 
# files[1] = os.path.join(dirname,files[1])

# odd file is assumed to be the earlier one
if ( os.path.getctime(files[1]) > os.path.getctime(files[0]) ):
odd = files[0]
even = files[1]
else:
odd = files[1]
even = files[0]

print "Identified Odd: ", odd
print "Identified Even: ", even

outputFilename = os.path.join(dirname, "Combined.pdf")
print "Output to: ", outputFilename

pageRect = CGRectMake (0, 0, 612, 792)
writeContext = CGPDFContextCreateWithFilename(outputFilename, pageRect)

duplexPages(writeContext, odd, even)

# delete the original files
trashPath = get_trash_path( odd ) # find location of trash
os.rename( odd, os.path.join(trashPath,os.path.split(odd)[1]) )
os.rename( even, os.path.join(trashPath,os.path.split(even)[1]) )
else:
print "Incorrect number of files"

return files


It can be saved as a Finder Plugin to appear on the context menu for Finder items.

Monday, June 8, 2009

VoodooHDA drivers for OS X

Some activity says that microphone-enabling drivers may be available:

http://forums.msiwind.net/osx-drivers/almost-fully-working-voodoohda-speakers-headphone-ext-mic-t10871.html

"system-library-extension; if you have applehda.kext/appleazaliaaudio,kext/voodoohda.kext back them up somewhere and then delete them, then download the voodoo kext. next step launch kext helper and add the kext to the box, enter your password, reboot and then try skype or audacity.

for further help, use the search box. all of these answers are in the forums somewhere. in fact if you go look at the original voodoo posting i think they posted directions in there. if you installed os x to your wind firsthand then im sure you used kext helper at some point. "

Then there is a "new version" that includes preference panels (but looks like pref panel is only update and is a separate file).
http://forum.voodooprojects.org/index.php/topic,139.0.htm

However, people reported problems with the new version and waking from sleep.

Monday, May 11, 2009

Fixed crashing word

Used this article to fix Word settings so it doesn't crash now. Removed the file com.microsoft.Word.plist from ~/Library/Preferences folder, and put settings back how I wanted them :-(

Update 2009 09 06.

I discovered that the crash occurs sometime when going from two screens (Word on the external screen) to one screen. This suggests that it's simply because something isn't checking that the screen coordinates are valid.

Found that if I replaced the part of the above file between the data tags straight after the 2008\Data\Toolbars tag, with what's recreated when the file is deleted, it starts up and doesn't crash.

I have saved a working file in Evernote. Would save it here but then I can't access it when I'm offline.


xxxx



Sunday, May 3, 2009

Power management on Advent 4211

Found what looks like an updated power management kernel extension that specially fixes the iTunes choppiness and hopefully crashes, VoodooPower. Found it in this forum topic, which seems to have several suggested post-install instructions.

So far no choppiness in iTunes, and it hasn't crashed, but then I only just installed it.

Saturday, May 2, 2009

Printing 2 pages per sheet using CUPS PDF driver

I downloaded this CUPS printer driver package and installed it, it worked seemlessly and allowed me to use the advanced printing layouts to create PDFs by choosing a PDF printer.

Friday, May 1, 2009

Word 2008 crashes on startup

Now my Word 2008 install doesn't seem to want to start. Found this KB article that suggests deleting old preferences - I think I may have originally had a trial version of Word, though maybe not.

Held down shift key at startup and it works now. Will have to investigate deleting settings.

Sunday, April 12, 2009

Offline OS Maps with MG Maps

I worked out how to get offline OS Maps in the UK on Mobile GMaps, and posted on the forum here.

My post:
I have worked out how to change MapTileCacher.perl (v1.35) so that you can download OS Maps from multimap.co.uk and use them offline in MGMaps. Very handy for hiking, as these are arguably the best maps in the world. I've only done this on OS X - perhaps someone can apply this technique to other OS's and other places (like within Mobile GMaps itself). 

Both 25,000:1 and 50,000:1 scales are downloaded, corresponding to the cache tool's zoom levels 15 and 14 respectively. The 50,000:1 maps are also provided at zoom levels 13 and 12, but below that you end up with pretty standard Live maps, which are handy for locating yourself, but no better than other map providers'. 

Thanks to Christian Streng's post at http://forum.mgmaps.com/viewtopic.php?t=1116&postdays=0&postorder=asc&&start=250, which set me off in the right direction. 

Firstly, you need to set the User Agent for the wget or curl fetch. If you don't, I think Multimap will 95% of the time give you a 403 forbidden response, at least it did for me. For OS X, at line 75 add a User Agent to the curl call (I don't know what you need to do with wget, I'm on OS X): 
Code:
  $wget = "curl -A Safari/3.2 -# -o TempTile.$pid "; 
  $systemRefererParam = ""; 

Secondly, put the following in place of the Osmarender map type (or your own choice of map style that you don't use). This code works out what the correct URL should be, which is a binary tile system like Live maps only broken into six digit segments. I spent a while finding the right 'miXXX' string in the middle - these combinations (where the string depends on the zoom level) give you the 50,000 or 25,000:1 maps as described above, at different zoom levels. You can zoom in further, but there is no point because you just get a blocky, scaled, version of the 25:000:1 map. (There are many other miXXX strings that give you maps for only parts of the country and/or only certain zoom levels - these ones gave me maps for all the places I tested, which wasn't all that many, but spread right across the country.) 
Code:

elsif(index($MapType,"Osmarender") != -1)     # ************ multimap for UK OS Maps ******************** 
  { 
    $digit = ((($y & 1) << 1) + ($x & 1)); # 'randomise' server access mc0, mc1, mc2 or mc3 
    $url .= "http://mc" . $digit . ".multimap.com/ptiles/map/"; 
    # the following selects the map type to use - only certain combinations of zoom and map type work, and some 
    # map numbers different from these work in some areas, but not UK-wide. Hopefully these particular ones 
    # do work UK-wide. 
    $url .= ($zoomLevel+1) >= 16 ? "mi932" : ($zoomLevel+1) >= 13 ? "mi904" : ($zoomLevel+1) >= 6 ? "mi916" : "mi915";  # select map type 
    $url .= "/" . ($zoomLevel + 1) . "/"; 
    
    # Multimap uses quadrant tiles like Microsoft Live, but splits them up into six-digit long segments. 
    # e.g. http://mc1.multimap.com/ptiles/map/mi902/16/031313/131331/311.png 
      
   $mmchars = 0; 
    for($i=$zoomLevel-1; $i >= 0; $i--) 
    { 
        # put a / every six characters 
        if ( ($mmchars == 6) || ($mmchars == 12) ) 
        { 
          $url .= "/"; 
        } 
        $url .= ((((($y >> $i) & 1) <<>> $i) & 1))); 
        $mmchars++; 
    } 
    $url .= ".png"; 
  } 


The $zoomlevel+1 business is because zoom level 15 in MapTileCacher.perl corresponds to zoom level 16 in Multimap, and so forth. 

With this you can use the cache tool, selecting the Osmarender map type, and run MapTileFE.perl as normal, but instead of Osmarender, MapTileCacher.perl will download lovely OS Maps from multimap.