[171] | 1 | from CoreGraphics import *
|
---|
| 2 | import sys
|
---|
| 3 | import os
|
---|
| 4 | import subprocess
|
---|
| 5 | import tempfile
|
---|
| 6 | from Carbon.File import FSPathMakeRef
|
---|
| 7 | from Carbon.Folder import FSFindFolder
|
---|
| 8 | from Carbon.Folders import kUserDomain, kDesktopFolderType
|
---|
| 9 | from Carbon.CF import CFURLCreateFromFileSystemRepresentation
|
---|
| 10 | from LaunchServices.Launch import LSSetExtensionHiddenForRef
|
---|
| 11 |
|
---|
| 12 | def check(retval):
|
---|
| 13 | if retval != 0:
|
---|
| 14 | sys.exit(retval)
|
---|
| 15 |
|
---|
| 16 | def dumpPNG(pdf, pageNumber, outFileBase):
|
---|
| 17 | if pageNumber is None:
|
---|
| 18 | outFile = outFileBase + ".png"
|
---|
| 19 | pageNumber = 1
|
---|
| 20 | else:
|
---|
| 21 | outFile = outFileBase + "-%03d.png" % pageNumber
|
---|
| 22 |
|
---|
| 23 | w = pdf.getTrimBox(pageNumber).getWidth()
|
---|
| 24 | h = pdf.getTrimBox(pageNumber).getHeight()
|
---|
| 25 |
|
---|
| 26 | ctx = CGBitmapContextCreateWithColor(int(w), int(h), CGColorSpaceCreateDeviceRGB(), (0,0,0,0))
|
---|
| 27 | ctx.drawPDFDocument(pdf.getTrimBox(pageNumber), pdf, pageNumber)
|
---|
| 28 | ctx.writeToFile(outFile, kCGImageFormatPNG)
|
---|
| 29 | return outFile
|
---|
| 30 |
|
---|
| 31 | tempDir = tempfile.mkdtemp()
|
---|
| 32 | pdfFile = os.path.join(tempDir, 'screenshot.pdf')
|
---|
| 33 |
|
---|
| 34 | check(subprocess.call(['/usr/sbin/screencapture', '-iW', pdfFile]))
|
---|
| 35 |
|
---|
| 36 | pdf = CGPDFDocumentCreateWithProvider(CGDataProviderCreateWithFilename(pdfFile))
|
---|
| 37 | numberOfPages = pdf.getNumberOfPages()
|
---|
| 38 |
|
---|
| 39 | desktopDir = FSFindFolder(kUserDomain, kDesktopFolderType, False).as_pathname()
|
---|
| 40 | desktopContents = os.listdir(desktopDir)
|
---|
| 41 | index = 1
|
---|
| 42 | outFile = None
|
---|
| 43 | while outFile is None:
|
---|
| 44 | outFile = 'Screenshot %d' % index
|
---|
| 45 | for filePath in desktopContents:
|
---|
| 46 | if filePath.startswith(outFile):
|
---|
| 47 | index += 1
|
---|
| 48 | outFile = None
|
---|
| 49 | break
|
---|
| 50 | outFile = os.path.join(desktopDir, outFile)
|
---|
| 51 |
|
---|
| 52 | writtenPaths = []
|
---|
| 53 | if numberOfPages == 1:
|
---|
| 54 | writtenPaths = [dumpPNG(pdf, None, outFile)]
|
---|
| 55 | else:
|
---|
| 56 | for pageNumber in xrange(1, numberOfPages+1):
|
---|
| 57 | writtenPaths.append(dumpPNG(pdf, pageNumber, outFile))
|
---|
| 58 |
|
---|
| 59 | os.remove(pdfFile)
|
---|
| 60 | os.rmdir(tempDir)
|
---|
| 61 |
|
---|
| 62 | for path in writtenPaths:
|
---|
| 63 | LSSetExtensionHiddenForRef(FSPathMakeRef(path)[0], True)
|
---|
| 64 |
|
---|
| 65 | # Carbon.File module doesn't implement FNNotify
|
---|
| 66 | subprocess.call(['/usr/bin/osascript', '-e', 'tell app "Finder" to update desktop'])
|
---|
| 67 |
|
---|
| 68 | # LaunchServices module doesn't implement LSOpenFrom*Spec
|
---|
| 69 | subprocess.call([os.path.join(os.getenv('RESOURCEPATH'), 'launch'), '-i', 'tv.kungfoo.1001'] + writtenPaths)
|
---|