Skip to content

Instantly share code, notes, and snippets.

@rodrigogermanlopez
Last active July 29, 2017 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodrigogermanlopez/9724aee5f108f86261d02c98358fb550 to your computer and use it in GitHub Desktop.
Save rodrigogermanlopez/9724aee5f108f86261d02c98358fb550 to your computer and use it in GitHub Desktop.
Adobe AIR Windows reboot with NativeProcess
package app.utils {
import flash.desktop.NativeProcess;
import flash.desktop.NativeProcessStartupInfo;
import flash.events.ProgressEvent;
import flash.filesystem.File;
/**
*
* Reboots the app in Windows
*
* Example:
* AppRebooter.reboot();
*
* batch passes a "autostart" argument so on the Main class we can read this and restore the app state before reboot.
*
* NativeApplication.nativeApplication.addEventListener( InvokeEvent.INVOKE, onInvoke );
*
* private function onInvoke( event:InvokeEvent ):void {
* var args:Array = event.arguments || [];
* if ( args && args.indexOf( "autostart" ) > -1 ) {
* // restore the last app state here...
* }
* }
*/
public class AppRebooter {
public static function reboot():void {
if( !Capabilities.version.substr(0,3)=="WIN" ) {
trace("Can't reboot in this OS");
return ;
}
var descriptorXML:XML = NativeApplication.nativeApplication.applicationDescriptor;
var ns:Namespace = descriptorXML.namespace();
var appName:String = String( descriptorXML.ns::filename[0] ) + ".exe";
var appPath:String = File.applicationDirectory.resolvePath(appName).nativePath ;
var fileReboot:String = File.applicationDirectory.resolvePath("reboot.cmd").nativePath ;
var np:NativeProcess = new NativeProcess();
var npi:NativeProcessStartupInfo = new NativeProcessStartupInfo();
npi.workingDirectory = File.applicationDirectory;
npi.executable = new File("C:\\WINDOWS\\system32\\cmd.exe");
var args:Vector.<String> = new Vector.<String>();
args.push("/c", fileReboot, appName, appPath );
npi.arguments = args ;
/*np.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, function(e:ProgressEvent){
var str:String = np.standardError.readUTFBytes(np.standardError.bytesAvailable);
trace( "error:"+str);
});
np.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, function(e){
var str:String = np.standardOutput.readUTFBytes(np.standardOutput.bytesAvailable);
trace( "output:" + str )
});*/
np.start(npi);
}
}
}
@echo off
IF "%1"=="" ( SET "appname=MyApp.exe" ) ELSE ( SET "appname=%1" )
IF "%2"=="" ( SET "appdir=%programfiles(x86)%/MyApp/" ) ELSE ( SET "appdir=%2" )
cd "%appdir%"
taskkill /IM "%appname%" /F
timeout /T 2 /nobreak >nul
Start "" /b "%appname%" autostart
rem -- autostart is passed as an argument for InvokeEvent.INVOKE, so we can load the app in the last "saved" state.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment