source: releases/1001Screenshot/1.0/screenshot.py@ 361

Last change on this file since 361 was 171, checked in by Nicholas Riley, 20 years ago

1001Screenshot 1.0

File size: 2.2 KB
Line 
1from CoreGraphics import *
2import sys
3import os
4import subprocess
5import tempfile
6from Carbon.File import FSPathMakeRef
7from Carbon.Folder import FSFindFolder
8from Carbon.Folders import kUserDomain, kDesktopFolderType
9from Carbon.CF import CFURLCreateFromFileSystemRepresentation
10from LaunchServices.Launch import LSSetExtensionHiddenForRef
11
12def check(retval):
13 if retval != 0:
14 sys.exit(retval)
15
16def 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
31tempDir = tempfile.mkdtemp()
32pdfFile = os.path.join(tempDir, 'screenshot.pdf')
33
34check(subprocess.call(['/usr/sbin/screencapture', '-iW', pdfFile]))
35
36pdf = CGPDFDocumentCreateWithProvider(CGDataProviderCreateWithFilename(pdfFile))
37numberOfPages = pdf.getNumberOfPages()
38
39desktopDir = FSFindFolder(kUserDomain, kDesktopFolderType, False).as_pathname()
40desktopContents = os.listdir(desktopDir)
41index = 1
42outFile = None
43while 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
50outFile = os.path.join(desktopDir, outFile)
51
52writtenPaths = []
53if numberOfPages == 1:
54 writtenPaths = [dumpPNG(pdf, None, outFile)]
55else:
56 for pageNumber in xrange(1, numberOfPages+1):
57 writtenPaths.append(dumpPNG(pdf, pageNumber, outFile))
58
59os.remove(pdfFile)
60os.rmdir(tempDir)
61
62for path in writtenPaths:
63 LSSetExtensionHiddenForRef(FSPathMakeRef(path)[0], True)
64
65# Carbon.File module doesn't implement FNNotify
66subprocess.call(['/usr/bin/osascript', '-e', 'tell app "Finder" to update desktop'])
67
68# LaunchServices module doesn't implement LSOpenFrom*Spec
69subprocess.call([os.path.join(os.getenv('RESOURCEPATH'), 'launch'), '-i', 'tv.kungfoo.1001'] + writtenPaths)
Note: See TracBrowser for help on using the repository browser.