Prior to AIR 2 the only way to capture the screen was to use something like Marapi Java Bridge that acts as a conduit between your application and the native system.
However, with AIR 2 Beta and the upcoming AIR 2 release, you can now use the command-line tool “screencapture” that comes with Mac OS. Nothing extra needs to be bundled with the application for this to work on a Mac, but on Windows, you need an additional piece of code to execute the screen capture process.
The process of doing a screen capture is pretty easy. You first create a File object that points to the location of the “screencapture” command line on the Mac:
var file:File = File.applicationDirectory;
file = file.resolvePath("usr/sbin/screencapture");
Then create a NativeProcessStartupInfo object that will be used when we start the NativeProcess.The arguments property of the NativeProcessStartupInfo takes a list of arguments that will be passed to the command line tool when its started. For a complete list of arguments used by the screencapture command line tool, type “screencapture –help” in the terminal window. For this example I wanted to push out a file named “screencap.png” to the desktop from a selection of the screen. Optionally, you could capture the image to the clipboard and paste it directly.
var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo(); var args:Vector. = new Vector.(); args[0] = "-i"; args[1] = "screencapture.png"; nativeProcessStartupInfo.arguments = args; nativeProcessStartupInfo.executable = file;
Now we can start the native process:
process = new NativeProcess(); process.start(nativeProcessStartupInfo);
The screencapture command tool will be launched and you will see the selection cursor on your screen. After you make a selection, the file will automatically be saved to the desktop as “screencapture.png”.
Here is the complete project code:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="init()">
<fx:Script>
<![CDATA[
private var process:NativeProcess;
private function init():void
{
launchNativeProcess();
}
private function launchNativeProcess():void
{
if(NativeProcess.isSupported) {
var file:File = File.applicationDirectory;
if (Capabilities.os.toLowerCase().indexOf("win") > -1) {
//file = file.resolvePath("bin/mylocalexe.exe");
} else if (Capabilities.os.toLowerCase().indexOf("mac") > -1) {
file = file.resolvePath("/usr/sbin/screencapture");
}
var args:Vector.<String> = new Vector.<String>();
args[0] = "-i";
args[1] = "screencapture.png";
var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
nativeProcessStartupInfo.arguments = args;
nativeProcessStartupInfo.executable = file;
nativeProcessStartupInfo.workingDirectory = File.desktopDirectory;
process = new NativeProcess();
process.start(nativeProcessStartupInfo);
} else {
trace("Native Process not supported");
}
}
]]>
</fx:Script>
</s:WindowedApplication>
Don’t forget to include support for extended desktop in the application xml file:
<supportedProfiles>extendedDesktop</supportedProfiles>
To get something like this to work on Windows, you need to know a little bit of C or C# so you can call your own service and launch the screen print functionality on Windows. For more about using the NativeProcess in AIR 2 for both Windows and Mac, you check out this Adobe article.
-Mr
is your solution works for html/ajax Adobe Air Apps ?
Adobe air programs can be created with Flash, Flex, or HTML. So this should work if you build your application with AIR and any of those languages.