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, AIR 2 now lets us call
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:
1 2
| 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.
1 2 3 4 5 6 7
| 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:
1 2
| 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="init()">
<![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);
}
}
]]> |
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.
Screen Capture with AIR 2 NativeProcess
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, AIR 2 now lets us call
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:
2
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.
2
3
4
5
6
7
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:
2
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:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="init()">
<![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);
}
}
]]>
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.