NativeProcess is not Supported AIR 3.2 Desktop? - actionscript-3

I am creating an application which capture the screenshot of the desktop.
When I'm compiling and run the application on Flash, only than it will works fine but when I published it with AIR 3.2 Desktop. Than It will not supported the NativeProcess.
I am running the application on Mac Machine.
Here is the below code which I am using:
if (NativeProcess.isSupported)
{
var file:File = File.desktopDirectory;
if (Capabilities.os.toLowerCase().indexOf("win") > -1)
{
file = file.resolvePath("bin/");
}
else if (Capabilities.os.toLowerCase().indexOf("mac") > -1)
{
file = file.resolvePath("/usr/sbin/screencapture/");
trace(" in "+file.nativePath);
status.text = file.nativePath;
}
var str = "screencapture" + String(count) + ".png";
trace(" String "+str);
var args:Vector.<String> = new Vector.<String>();
args[0] = str;
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");
status.text = " Native Process not supported";
}
Any Help is highly appreciated

Related

script to read/write a file that is stored in a local "dropbox" folder or a local "google drive"

For the moment, the dropbox API allows a browser to access (read/write) all type of files in a dropbox folder on your local drive by script. The file path and name can be specified in the script. The script works on webpages that are stored on a local drive, but also works on webpages that are stored on a remote site when using Chrome.
Please see these jsfiddles with a primitive implementation to test writing and reading a text file in a local dropbox folder. The scripts are written in pure vanilla javascript code, and no not need a library.
Only a one-time access token is needed and no further authentication procedure.
This jsfiddle (click) demonstrates: Write text file to local dropbox folder
function WriteFile() {
var H = new Array(6);
H[0] = "Authorization";
H[1] = document.getElementById("bear").value;
H[2] = "Content-type";
H[3] = "application/octet-stream";
H[4] = "Dropbox-API-Arg";
H[5] = '{"path": "/' + document.getElementById("pana").value;
H[5] = H[5] + '","mode":{".tag":"overwrite"}}';
var X = new XMLHttpRequest();
X.onreadystatechange = acb;
X.open("POST", "https://content.dropboxapi.com/2/files/upload", true);
X.setRequestHeader(H[0], H[1]);
X.setRequestHeader(H[2], H[3]);
X.setRequestHeader(H[4], H[5]);
X.send(document.getElementById("myText").value);
document.getElementById("demo").innerHTML = "writing . . .";
}
function acb(A) {
var X = A.target;
var G = X.readyState;
if (G != 4) {
return 0;
}
document.getElementById("demo").innerHTML = "status: " + X.status;
}
This jsfiddle (click) demonstrates: Read text file from local dropbox folder
function ReadFile() {
document.getElementById("text").value = "";
var H = new Array(6);
H[0] = "User-Agent";
H[1] = "api-explorer-client";
H[2] = "Authorization";
H[3] = document.getElementById("bear").value;
H[4] = "Content-type";
H[5] = "application/json";
var X = new XMLHttpRequest();
X.onreadystatechange = acb;
X.open("POST", "https://api.dropboxapi.com/2/sharing/create_shared_link", true);
X.setRequestHeader(H[0], H[1]);
X.setRequestHeader(H[2], H[3]);
X.setRequestHeader(H[4], H[5]);
X.send('{"path": "/' + document.getElementById("pana").value + '"}');
document.getElementById("stat").innerHTML = "Reading . . .";
}
// async call back 1
function acb(A) {
var X = A.target;
if (X.readyState != 4) {
return 0; // when ready
}
var stat = X.status;
document.getElementById("stat").innerHTML = "status: " + stat;
if (stat != 200) {
document.getElementById("text").value = "NOT FOUND";
return 0; // error
}
var B = X.responseText;
var C = JSON.parse(B);
var D = C.url;
var E = D.split(".com");
var F = E[1];
var G = "https://dl.dropboxusercontent.com" + F;
document.getElementById("text").value = G;
X = new XMLHttpRequest();
X.onreadystatechange = acb2;
X.open("GET", G, true);
X.send(null);
}
// async callback 2
function acb2(A) {
var X = A.target;
if (X.readyState != 4) {
return 0; // when ready
}
var stat = X.status;
if (stat != 200) {
document.getElementById("text").value = "Can't Read the file";
return 0; // error
}
var B = X.responseText;
document.getElementById("text").value = B;
}
I tried to obtain the same functionality for files in a "google drive" on a local drive , using the "google drive API", but without success. Can someone achieve this, eventually using other cloud storage systems.

use as3 NativeProcess to run windows cmd

I used the NativeProcess to run my .cmd file to use the convert of Imagemagick on Windows.
the cmd is
convert -resize 1%x2% 3% 4%
On my computer,it looked normal.
But on some other computer,the bug looked like the “-resize” was be garbled.
then I changed the cmd
convert 1% 2%x3% 4% 5%
but on some computer,it still have the bug.
Is that the air output UTF-8,and the windows CMD use Unicode?
How to use it will not produce this error?
my code is
private var mProcess:NativeProcess;
private function convertImgToTargetPng(filePath:String,resultFilePath:String,width:Number,height:Number,scale:Number=1):void
{
var convertFile:File = File.applicationDirectory.resolvePath("script").resolvePath("convert.cmd");
var nativeProcessStartupInfo = new NativeProcessStartupInfo();
nativeProcessStartupInfo.executable = convertFile;
nativeProcessStartupInfo.workingDirectory = File.userDirectory;
var processArgs:Vector.<String> = new Vector.<String>();
processArgs.push("-resize");
processArgs.push(width*scale);
processArgs.push(height*scale);
processArgs.push(filePath);
processArgs.push(resultFilePath);
nativeProcessStartupInfo.arguments = processArgs;
mProcess = new NativeProcess();
mProcess.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, onConvertErrorHandler);
mProcess.addEventListener(NativeProcessExitEvent.EXIT, onConvertExitHandler);
try
{
mProcess.start(nativeProcessStartupInfo);
}
catch (e : Error)
{
Alert.show("convert failed: " + e.message);
}
}
private function onConvertErrorHandler(event : ProgressEvent):void
{
var data:String = mProcess.standardError.readUTFBytes(mProcess.standardError.bytesAvailable);
Alert.show("convert error :" + data);
}
private function onConvertExitHandler(event : NativeProcessExitEvent):void
{
mProcess.removeEventListener(ProgressEvent.STANDARD_ERROR_DATA, onConvertErrorHandler);
mProcess.removeEventListener(NativeProcessExitEvent.EXIT, onConvertExitHandler);
Alert.show("convert success");
}
Untested but you can try...
Make a changed CMD file that looks like: convert 1% 2% 3% 4%
and update your AS3 code section to look like this...
var processArgs:Vector.<String> = new Vector.<String>();
processArgs.push("-resize");
processArgs.push(String ( String(width*scale) + "x" + String(height*scale) ) );
processArgs.push(filePath);
processArgs.push(resultFilePath);
Let me now how it goes.
Also instead of a .cmd file, why not just run the process with convert.exe file itself? :
If that convert.exe is located here : c:\program files\imagemagick\convert.exe Then use path location in AS3 code as c:\\program files\\imagemagick\\convert.exe
private function convertImgToTargetPng(filePath:String,resultFilePath:String,width:Number,height:Number,scale:Number=1):void
{
//# Use your location of convert.exe
var convertFile : File = new File("C:\\imageMagick\\converter.exe");
if ( convertFile.exists ) //# If found in your location
{
var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo(); //vc
nativeProcessStartupInfo.executable = convertFile;
nativeProcessStartupInfo.workingDirectory = File.userDirectory;
var processArgs:Vector.<String> = new Vector.<String>();
processArgs.push("-resize");
processArgs.push(String ( String(width*scale) + "x" + String(height*scale) ) );
processArgs.push(filePath);
processArgs.push(resultFilePath);
nativeProcessStartupInfo.arguments = processArgs;
mProcess = new NativeProcess();
mProcess.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, onConvertErrorHandler);
mProcess.addEventListener(NativeProcessExitEvent.EXIT, onConvertExitHandler);
try { mProcess.start(nativeProcessStartupInfo); }
catch (e : Error)
{ Alert.show("convert failed: " + e.message); }
}
}

How to get byteArray or base64 string from RenderTexture Cocos2d-JS

I am making a game on cocos2d-JS for facebook in which there is a requirement of sharing a screenshot of the game.
I am able to take the screenshot but now I am unable to upload it in the Parse.com server because it requires base64 format or byte array. I am unable to find any solution of converting Sprite in to this format.. Here's my code so result when I do addchild its coming proper .. I have also added my commented code so that it will help to understand that I have tried lot of things but couldnt achieve the same.
shareToSocialNetworking: function () {
cc.director.setNextDeltaTimeZero(true);
var newsize = cc.director.getVisibleSize();
var renderText = new cc.RenderTexture(newsize.width,newsize.height);
renderText.begin();
cc.director.getRunningScene().visit();
renderText.end();
var result = cc.Sprite.create(renderText.getSprite().getTexture());
result.flippedY = true;
this._mainViewNode.addChild(result,6000);
//renderText.saveToFile("screenshot.jpg",cc.IMAGE_FORMAT_PNG);
//var based = renderText.getSprite().getTexture().getStringForFormat().toString();
//var data = based.getData();
var file = new Parse.File("screen.jpg", { base64: this.getBase64(result) });
//var file = new Parse.File("screen.jpg", data, "image/png");
var self = this;
file.save().then(function() {
// The file has been saved to Parse.
alert(file.url);
this.onSharePictureInfoLink(file.url());
}, function(error) {
// The file either could not be read, or could not be saved to Parse.
});
//
//var ccImage = renderText.newCCImage();
//
//var str = ccImage.getData();
},
is there any workaround that can be done
there is a private variable called _cacheCanvas, which is the instance of the offscreen canvas
you can simply do renderText._cacheCanvas.toDataURL()
Here's how you can take the screnshot from cocos2d-JS
screenshot: function (fileName) {
var tex = new cc.RenderTexture(winSize.width, winSize.height, cc.Texture2D.PIXEL_FORMAT_RGBA8888);
tex.setPosition(cc.p(winSize.width / 2, winSize.height / 2));
tex.begin();
cc.director.getRunningScene().visit();
tex.end();
var imgPath = jsb.fileUtils.getWritablePath();
if (imgPath.length == 0) {
return;
}
var result = tex.saveToFile(fileName, cc.IMAGE_FORMAT_JPEG);
if (result) {
imgPath += fileName;
cc.log("save image:" + imgPath);
return imgPath;
}
return "";
}
then make a Java call from Javascript
public static void ScreenShot()
{
Bitmap imageBitmap = BitmapFactory.decodeFile(Cocos2dxHelper.getCocos2dxWritablePath() + "/" + "screenshot.png");
String fileHolder = "SampleFolder";
File filepathData = new File("/sdcard/" + fileHolder);
//~~~Create Dir
try {
if (!filepathData.exists())
{
filepathData.mkdirs();
filepathData.createNewFile();
FileWriter fw = new FileWriter(filepathData + fileHolder);
BufferedWriter out = new BufferedWriter(fw);
String toSave = String.valueOf(0);
out.write(toSave);
out.close();
}
}
catch (IOException e1) {
}
//~~~Create Image
File file = new File("/sdcard/" + "Your filename");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
imageBitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
}
catch (Exception e) {}
Uri phototUri = Uri.fromFile(file);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
//~~~Add Code Below
}
Do not forget to add permission for external storage

upload file to box api v2 using as3

I'm trying to upload file to box using v2. The only result i get is the error while uploading. I'm able to authenticate, get the auth token but not able to upload, I have given the code which i'm using. If i'm wrong anyway just correct me. Any help will be appreciated!!
public function upload(argFile:flash.filesystem.File):void{
argFile.addEventListener(Event.COMPLETE,
function onComplete(event:Event):void
{
trace("complete:"+event.toString());
}
);
argFile.addEventListener(IOErrorEvent.IO_ERROR,
function onIOError(event:IOErrorEvent):void
{
trace("Error:"+event.toString());
}
);
var url:String = "https://api.box.com/2.0/files/data";
// Setup the custom header
var customHeader:String = "BoxAuth api_key=" + api_key + "&auth_token=" + auth_token;
var headers:Array = [
new URLRequestHeader("Authorization", customHeader)
];
// create the url-vars
var urlVars:URLVariables = new URLVariables();
urlVars.filename1 = '#'+argFile.name;
urlVars.folder_id = "0";
// create the url-reqeust
var request:URLRequest = new URLRequest();
request.contentType = "application/octet-stream";
request.method = URLRequestMethod.POST;
// set ther url
request.url = url;
// set the header
request.requestHeaders = headers;
// set the vars
request.data = urlVars;
try {
argFile.upload(request);
} catch (e:Error)
{
trace(e.toString(), "Error (catch all)");
}
}

Upload pre-selected file with actionscript

I'm creating a photoshop extension where I need to save the file people are working on and upload it to a server. So I want the extension to be able to automatically choose the current file and upload it to my server.
Problem is I don't know how to pre-select a file for people. Here's my code so far:
var app:Application = Photoshop.app;
var doc:Document = app.documents.add();
doc.selection.selectAll();
var color:RGBColor = new RGBColor();
color.red = 0;
color.green = 0;
color.blue = 255;
doc.selection.fill(color);
var saveOptions:JPEGSaveOptions = new JPEGSaveOptions();
//Add other PDF save options here.
doc.saveAs(File.applicationStorageDirectory, saveOptions);
var jsonOBJ:Object = {};
jsonOBJ.option = "iphone";
jsonOBJ.title = "c";
jsonOBJ.description = "s";
jsonOBJ.app_store_url = "iphone";
jsonOBJ.tags = "c";
jsonOBJ.temp_number = 1;
var _service:HTTPService = new HTTPService();
_service.url = "http://localhost:3000/designs";
_service.method = "POST";
_service.contentType = "application/json";
_service.resultFormat = "text";
_service.useProxy = false;
_service.makeObjectsBindable = true;
_service.addEventListener(FaultEvent.FAULT,faultRX);
_service.addEventListener(ResultEvent.RESULT,resultRX);
_service.showBusyCursor = true;
_service.send( JSON.encode( jsonOBJ ) );
function resultRX():void
{
trace(ResultEvent.RESULT);
}
function faultRX():void
{
trace(FaultEvent.FAULT);
}
var file:FileReference;
var filefilters:Array;
var req:URLRequest;
filefilters = [ new FileFilter('Images', '*.jpg') ]; // add other file filters
file.browse(filefilters);
It's a photoshop extension. I ended up using FileReference and link to the file. it worked. I dunno how to actually upload the image though. When I use file.upload(requestUrl), it sends along a wrong content type. –