Code:
function screenshotRegionToFile(x,y,w,h,path){
//Largely ported from:
//NSBitmapImageRep *imgRep = [[image representations] objectAtIndex: 0];
//NSData *data = [imgRep representationUsingType: NSPNGFileType properties: nil];
//[data writeToFile: @"/path/to/file.png" atomically: NO];
ObjC.import('Foundation')
ObjC.import('AppKit')
var rect = $.CGRectMake(x,y,w,h)
//Get DisplayID from rectangle
var displays = Ref()
$.CGMainDisplayID() //Fully initialise CoreGraphics Framework
$.CGGetDisplaysWithRect(rect,1,displays,null)
displayID = displays[0]
image = $.CGDisplayCreateImageForRect(displayID,rect);
bitmap = $.NSBitmapImageRep.alloc.initWithCGImage(image)
pngData = bitmap.representationUsingTypeProperties($.NSPNGFileType,$())
pngData.writeToFileAtomically(path,true)
displayID.release
image.release
bitmap.release
pngData.release
}
Example usage:
screenshotRegionToFile(100,100,200,200,"/Users/Sancarn/Pictures/100-100-200-200-image.png")
Example - Taking a screenshot of the active window:
var system = Application("System Events")
var processes = system.processes()
getFrontProc: {
for(var i = 0; i<processes.length;i++){
if(processes[i].frontmost()){
break getFrontProc
}
}
}
var window = processes[i].windows[0]
var frame = window.attributes['AXFrame'].value()
var ffdFrame = [frame[0], frame[1], frame[2]-frame[0], frame[3]-frame[1]]
screenshotRegionToFile(ffdFrame[0], ffdFrame[1], ffdFrame[2], ffdFrame[3],"/Users/Sancarn/Pictures/MyCurrentWindow.png")
Example - Getting a screenshot of a region offset to a UI Element of another application:
button = Application("System Events").applicationProcesses["ScreenFlow"].windows[0].buttons[3]
if(button.description() == "Fast Forward"){
frame = button.attributes['AXFrame'].value()
ffdFrame = [frame[0], frame[1], frame[2]-frame[0], frame[3]-frame[1]]
ffdFrame = [ffdFrame[0] + ffdFrame[2] + 5, ffdFrame[1]+2, ffdFrame[2]+78, ffdFrame[3]-5]
screenshotRegionToFile(ffdFrame[0], ffdFrame[1], ffdFrame[2], ffdFrame[3],"/Users/Sancarn/Pictures/newPicture.png")
}
The above code consistently returns this region of screenflow:
From here on in I can analyse the pixels of this bar while increasing the timer, and find the pixel checks necessary to find any given time in screenflow.