Hello
RubyCocoa is no longer part of standard installation of OS X 10.10 or later. Although you can build and install RubyCocoa 1.2.0 that supports Ruby 2.0 from source code, it would be overkill just to take a screencapture of specified region.
So here's an AppleScript script that is a wrapper of pyobjc script to do the same thing as the original rubycocoa script. PyObjC is part of standard installation of OS X 10.5 through 10.12.
--APPLESCRIPT
set {l, t, r, b} to {911, 173, 1738, 794}
set {x, y, w, h} to {l, t, r - l, b - t}
screencapture(x, y, w, h)
on screencapture(x, y, w, h)
(*
number x, y, w, h: rectangle parameters
(x, y) = x, y-coordinates of origin point
(w, h) = width and height of rectangle
return string: file name of saved screen shot
* screen shot is saved as ~/Desktop/yyyy-mm-dd hh.mm.ss.png
*)
set args to ""
repeat with a in {x, y, w, h}
set args to args & space & ("" & a)'s quoted form
end repeat
do shell script "/usr/bin/python <<'EOF' - " & args & "
# coding: utf-8
#
# file:
# screencapture.py
#
# function:
# create screen capture of specified rectangle and save it as time-stamped png image on desktop
#
# usage e.g.:
# ./screencapture.py x y w h
# x, y, w, h: rect(point(x, y), size(w, h))
#
# version:
# 0.10
# - draft
#
import sys, os, objc, time
import Quartz.CoreGraphics as CG
import AppKit as OSX
#
# manual loading of bridgesupport metadata for CGDisplayCreateImageForRect() is required under OS X 10.6.8
#
CG_BRIDGESUPPORT = '''<?xml version=\"1.0\" standalone=\"yes\"?>
<!DOCTYPE signatures SYSTEM \"file://localhost/System/Library/DTDs/BridgeSupport.dtd\">
<signatures version=\"0.9\">
<function name='CGDisplayCreateImageForRect'>
<arg type='I'/>
<arg type64='{CGRect={CGPoint=dd}{CGSize=dd}}' type='{CGRect={CGPoint=ff}{CGSize=ff}}'/>
<retval already_retained='true' type='^{CGImage=}'/>
</function>
</signatures>'''
objc.parseBridgeSupport(
CG_BRIDGESUPPORT,
globals(),
objc.pathForFramework('/System/Library/Frameworks/ApplicationServices.framework/Frameworks/CoreGraphics.framework')
)
def usage():
sys.stderr.write('Usage: %s x y w h\\n' % os.path.basename(sys.argv[0]))
sys.exit(1)
def main():
if not len(sys.argv) == 5: usage()
x, y, w, h = [ float(a) for a in sys.argv[1:5] ]
outfile = os.path.join(
os.path.expanduser('~/Desktop'),
time.strftime('%F %H.%M.%S.png', time.localtime())
)
# outfile = ~/Desktop/yyyy-mm-dd hh.mm.ss.png
img = CGDisplayCreateImageForRect(CG.CGMainDisplayID(), CG.CGRectMake(x, y, w, h))
brep = OSX.NSBitmapImageRep.alloc().initWithCGImage_(img)
data = brep.representationUsingType_properties_(OSX.NSPNGFileType, {})
data.writeToFile_atomically_(outfile, False)
sys.stdout.write('%s\\n' % os.path.basename(outfile))
main()
EOF"
end screencapture
--END OF APPLESCRIPT
And in case, here's the underlying pyobjc script if you prefer to use it in shell.
#!/usr/bin/python
# coding: utf-8
#
# file:
# screencapture.py
#
# function:
# create screen capture of specified rectangle and save it as png image on desktop
#
# usage e.g.:
# ./screencapture.py x y w h
# x, y, w, h: rect(point(x, y), size(w, h))
#
# version:
# 0.10
# - draft
#
import sys, os, objc, time
import Quartz.CoreGraphics as CG
import AppKit as OSX
#
# manual loading of bridgesupport metadata for CGDisplayCreateImageForRect() is required under OS X 10.6.8
#
CG_BRIDGESUPPORT = '''<?xml version="1.0" standalone="yes"?>
<!DOCTYPE signatures SYSTEM "file://localhost/System/Library/DTDs/BridgeSupport.dtd">
<signatures version="0.9">
<function name='CGDisplayCreateImageForRect'>
<arg type='I'/>
<arg type64='{CGRect={CGPoint=dd}{CGSize=dd}}' type='{CGRect={CGPoint=ff}{CGSize=ff}}'/>
<retval already_retained='true' type='^{CGImage=}'/>
</function>
</signatures>'''
objc.parseBridgeSupport(
CG_BRIDGESUPPORT,
globals(),
objc.pathForFramework('/System/Library/Frameworks/ApplicationServices.framework/Frameworks/CoreGraphics.framework')
)
def usage():
sys.stderr.write('Usage: %s x y w h\n' % os.path.basename(sys.argv[0]))
sys.exit(1)
def main():
if not len(sys.argv) == 5: usage()
x, y, w, h = [ float(a) for a in sys.argv[1:5] ]
outfile = os.path.join(
os.path.expanduser('~/Desktop'),
time.strftime('%F %H.%M.%S.png', time.localtime())
)
# outfile = ~/Desktop/yyyy-mm-dd hh.mm.ss.png
img = CGDisplayCreateImageForRect(CG.CGMainDisplayID(), CG.CGRectMake(x, y, w, h))
brep = OSX.NSBitmapImageRep.alloc().initWithCGImage_(img)
data = brep.representationUsingType_properties_(OSX.NSPNGFileType, {})
data.writeToFile_atomically_(outfile, False)
sys.stdout.write('%s\n' % os.path.basename(outfile))
main()
Scripts are briefly tested with pobjc 2.2b3 and python 2.6.1 under OS X 10.6.8.
Good luck,
H