Usinc Command Promt / Shell in Action script 3 - actionscript-3

As title, what command/class can i used for that? and if the function is exist whether function to get callback from commandshell?

You can run and communicate with other processes in AIR as per this article.
So, if you wanted to run the Windows command prompt, you would have to provide the location of cmd.exe which is "%windir%\system32\cmd.exe". Unfortunately, AIR won't understand %windir%, so you will have to actually provide the full path to the Windows directory (usually C: but you will have to figure out how to handle cases where it is not C:).
Annoyingly, the command prompt does not seem to act like a normal input stream; I receive errors when trying to write to it. There may be some way around that that I don't know about it. Instead though, you can just start the command prompt with your arguments.
For instance, the following code will start a command prompt (assuming Windows is on C), print "hello" and trace the output (which in this case will just be "hello").
var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
var file:File = File.applicationDirectory.resolvePath("C:\\Windows\\System32\\cmd.exe");
nativeProcessStartupInfo.executable = file;
var processArgs:Vector.<String> = new Vector.<String>();
processArgs.push("/C echo 'hello'");
nativeProcessStartupInfo.arguments = processArgs;
process = new NativeProcess();
process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);
process.start(nativeProcessStartupInfo);
public function onOutputData(event:ProgressEvent):void
{
trace("Got: ", NativeProcess(event.target).standardOutput.readUTFBytes(process.standardOutput.bytesAvailable));
}

Related

executing bat file from javascript on chrome/firefox

I tried running this batch file using ActiveXobject, but this only works on IE. Can I get a solution to run it on Chrome/Firefox?
If you are developing a Firefox extension, the following code works. It launches D:\test.bat file.
var file = null;
var process = null;
var args = [""];
// create an nsIFile for the executable
file = Components.classes["#mozilla.org/file/local;1"].createInstance(Components.interfaces.nsIFile);
file.initWithPath("D:\\test.bat");
// create an nsIProcess
process = Components.classes["#mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
process.init(file);
// Launch the process
process.run(false , args, args.length);

AIR Native process Error

I try to launch a programm with NativeProcess on Mac.
pathEV="/Applications/MyFolder/MyAppOSX.app"
var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
var fileEV:File = new File();
fileEV=fileEV.resolvePath(pathEV);
nativeProcessStartupInfo.executable = fileEV;
var process:NativeProcess = new NativeProcess();
process.start(nativeProcessStartupInfo);
But this error appear:
Error #3214: NativeProcessStartupInfo.executable does not specify a valid executable file
Can you help me to solve that?
Thanks
Haven't tested it yet, but I assume you'd need to run open with the path to your application as a parameter, like this:
pathEV:String="/Applications/MyFolder/MyAppOSX.app"
var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
var fileEV:File = new File();
fileEV=fileEV.resolvePath( '/usr/bin/open' );
nativeProcessStartupInfo.arguments = Vector.<String>([pathEV]);
nativeProcessStartupInfo.executable = fileEV;
var process:NativeProcess = new NativeProcess();
process.start(nativeProcessStartupInfo);
And yes, this definitely needs extendedDesktop rights
FYI: The Vector shizzle is correct, I use it to convert the array to Vector, not as a constructor function
Here's an explanation how to launch .app files from the terminal
Could it be that you need to add <supportedProfiles>extendedDesktop</supportedProfiles> to your manifest file?
Also, I believe.app is not an executable. Its actually just an application bundle within which exists the actual executable. Try pointing to /bin/ls first to make sure all else is OK and then dig inside the .app folder to find your executable.
Why do you need a nativeProcess? Why not just use fileEV.openWithDefaultApplication() ?

Trouble in using NativeProcess, actionscript3 to convert files

I am trying to make a program to convert png files in ATF textures but i am having some trouble when i try to use the NativeProcess... I am using actionscript 3 with IntelliJ IDEA.
I want to pass that prompt command png2atf -c p -i starling-atf.png -o starling.atf, to my NativeProcess...
So, i choose a png file, from a File().load object, and then i want to take this file and send as a parameter to my NativeProcess and make the conversation over the prompt command (png2atf -c p -i starling-atf.png -o starling.atf)....
Any ideas?
#puggsoy u are write, the problem were spaces... i put some spaces with the args, thats why..
here its the right code:
f.nativePath = "C:/projects/SDK/Adobe Gaming SDK 1.0.1/Utilities/ATF Tools/Windows/png2atf.exe";
nativeProcessStartupInfo.executable = f;
// now create the arguments Vector to pass it to the executable file
var processArgs:Vector.<String> = new Vector.<String>();
processArgs[0] = "-c";
processArgs[1] = arg;
processArgs[2] = "-i";
processArgs[3] = input;
processArgs[4] = "-o";
processArgs[5] = output;
nativeProcessStartupInfo.arguments = processArgs;
process = new NativeProcess();
process.start(nativeProcessStartupInfo);

Flash Builder will not read local JSON file . .

So I've tried to build a small utility to view the contents of a JSON file in an easy-to-understand manner (for non-tech people).
I have Googled far and wide, high and low, but every example that shows how to consume a JSON file in Flash Builder uses the HTTP service, pointing to a file on the web.
Here I am, sitting in front of my MacBook, wondering why I can't make this work. In the documentation I've found (sort of relating to this issue), they always show Windows examples, and they seem to work fine:
C://me/projects/json/my_json.json
Perhaps I'm completely missing the obvious, but is this possible on a Mac as well?
I've tried
file:///Users/me/projects/json/my_json.json
That doesn't work. I've tried some "resolve to path" syntax, but the HTTP service does not seem to allow for anything but file paths in quotes.
Would anyone be able to pint me in the right direction?
Use the File API. It's really easy, here's a quick code sample:
// Get a File reference, starting on the desktop.
// If you have a specific file you want to open you could do this:
// var file:File = File.desktopDirectory.resolvePath("myfile.json")
// Then skip directly to readFile()
var file:File = File.desktopDirectory;
// Add a listener for when the user selects a file
file.addEventListener(Event.SELECT, onSelect);
// Add a listener for when the user cancels selecting a file
file.addEventListener(Event.CANCEL, onCancel);
// This will restrict the file open dialog such that you
// can only open .json files
var filter:FileFilter = new FileFilter("JSON Files", "*.json");
// Open the file browse dialog
file.browseForOpen("Open a file", [filter]);
// Select event handler
private function onSelect(e:Event):void
{
// Remove listeners on e.currentTarget
// ...
// Cast to File
var selectedFile:File = e.currentTarget as File;
readFile(selectedFile);
}
private function onCancel(e:Event):void
{
// Remove listeners on e.currentTarget
// ...
}
private function readFile(file:File):void
{
// Read file
var fs:FileStream = new FileStream();
fs.open(selectedFile, FileMode.READ);
var contents:String = fs.readUTFBytes(selectedFile.size);
fs.close()
// Parse your JSON for display or whatever you need it for
parseJSON(contents);
}
You hinted at this in your post about examples being for Windows and you being on a Mac but I'll state it explicitly here: you should always use the File API because it is cross platform. This code will work equally well on Windows and Mac.

Is there a way generate a shortcut file with adobe air?

Good afternoon,
I would like create a application that can can create folders and short cuts to folders in the file system. The user will click a button and it will put a folder on there desktop that has short cuts to files like //server/folder1/folder2 Can you create a desktop shortcut with code in adobe air? How would you do that? How do you create a folder? I keep thinking this should be easy but i keep missing it.
Thank you for your help sorry for the trouble,
Justin
If your deployment profile is Extended Desktop, you may be able to use NativeProcess and some simple scripts that you could package with your app. This approach would entail handling the functionality on a per OS basis, which would take some work and extensive testing. However, I wanted to at least share a scenario that I verified does work. Below is a test case that I threw together:
Test Case: Windows 7
Even though the Adobe documentation says that it prevents execution of .bat files, apparently it doesn't prevent one from executing the Windows Scripting Host: wscript.exe. This means you can execute any JScript or VBScript files. And this is what you would use to write a command to create a shortcut in Windows (since Windows doesn't have a commandline command to create shortcuts otherwise).
Here's a simple script to create a shortcut command, which I found on giannistsakiris.com, (converted to JScript):
// File: mkshortcut.js
var WshShell = new ActiveXObject("WScript.Shell");
var oShellLink = WshShell.CreateShortcut(WScript.Arguments.Named("shortcut") + ".lnk");
oShellLink.TargetPath = WScript.Arguments.Named("target");
oShellLink.WindowStyle = 1;
oShellLink.Save();
If you package this in your application in a folder named utils, you could write a function to create a shortcut like so:
public function createShortcut(target:File, shortcut:File):void {
if (NativeProcess.isSupported) { // Note: this is only true under extendedDesktop profile
var shortcutInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
// Location of the Windows Scripting Host executable
shortcutInfo.executable = new File("C:/Windows/System32/wscript.exe");
// Argument 1: script to execute
shortcutInfo.arguments.push( File.applicationDirectory.resolvePath("utils/mkshortcut.js").nativePath);
// Argument 2: target
shortcutInfo.arguments.push("/target:" + target.nativePath);
// Argument 3: shortcut
shortcutInfo.arguments.push("/shortcut:" + shortcut.nativePath);
var mkShortcutProcess = new NativeProcess();
mkShortcutProcess.start(shortcutInfo);
}
}
If one wanted to create a shortcut to the Application Storage Directory on the Desktop, the following would suffice:
var targetLocation:File = File.applicationStorageDirectory;
var shortcutLocation:File = File.desktopDirectory.resolvePath("Shortcut to My AIR App Storage");
createShortcut(targetLocation, shortcutLocation);
Obviously there's a lot of work to be done to handle different OS environments, but this is at least a step.
As far as I know, File class does not allow the creation of symbolic links. But you can create directories with createDirectory(): http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/File.html#createDirectory%28%29
Check if this can be useful: http://www.mikechambers.com/blog/2008/01/17/commandproxy-net-air-integration-proof-of-concept/
Air doesnt let you create shortcuts natively. Here's a workaround that works with Windows [may work on Mac but I don't have a machine to test].
Using Air, create a file that contains the following plain text
[InternetShortcut]
URL=C:\path-to-folder-or-file
Replace path-to-folder-or-file with your folder/file name
Save the file as test.url
Windows recognizes this file as a shortcut.
It is possible to coerce Adobe Air into creating symbolic links, other useful things, on a Mac. Here's how I did it:
You will need AIRAliases.js - Revision: 2.5
In the application.xml add:
<!-- Enables NativeProcess -->
<supportedProfiles>extendedDesktop desktop</supportedProfiles>
In the Air app JavaScript:
// A familiar console logger
var console = {
'log' : function(msg){air.Introspector.Console.log(msg)}
};
if (air.NativeProcess.isSupported) {
var cmdFile = air.File.documentsDirectory.resolvePath("/bin/ln");
if (cmdFile.exists) {
var nativeProcessStartupInfo = new air.NativeProcessStartupInfo();
var processArgs = new air.Vector["<String>"]();
nativeProcessStartupInfo.executable = cmdFile;
processArgs.push("-s");
processArgs.push("< source file path >");
processArgs.push("< link file path >");
nativeProcessStartupInfo.arguments = processArgs;
nativeProcess = new air.NativeProcess();
nativeProcess.addEventListener(air.NativeProcessExitEvent.EXIT, onProcessExit);
nativeProcess.addEventListener(air.ProgressEvent.STANDARD_OUTPUT_DATA, onProcessOutput);
nativeProcess.addEventListener(air.ProgressEvent.STANDARD_ERROR_DATA, onProcessError);
nativeProcess.start(nativeProcessStartupInfo);
} else {
console.log("Can't find cmdFile");
}
} else {
console.log("Not Supported");
}
function onProcessExit(event) {
var result = event.exitCode;
console.log("Exit Code: "+result);
};
function onProcessOutput() {
console.log("Output: "+nativeProcess.standardOutput.readUTFBytes(nativeProcess.standardOutput.bytesAvailable));
};
function onProcessError() {
console.log("Error: "+nativeProcess.standardError.readUTFBytes(nativeProcess.standardError.bytesAvailable));
};
Altering the syntax of the command and parameters passed to NativeProcess you should be able to get real shortcuts on Windows too.