Skip to content

Instantly share code, notes, and snippets.

@alebianco
Last active December 18, 2018 03:17
Show Gist options
  • Save alebianco/5849643 to your computer and use it in GitHub Desktop.
Save alebianco/5849643 to your computer and use it in GitHub Desktop.
Compile *.fla to *.swf from an AIR application at runtime.

OSX version, rely on applescript to execute a jsfl script (generated at runtime) on the Flash IDE.
AIR application MUST be run on the ExtendedDesktop profile.

Notes: Use as a frame-script and add a button on the stage with instance name "compile_btn".

import flash.desktop.NativeProcess;
import flash.events.ProgressEvent;
import flash.desktop.NativeProcessStartupInfo;
import flash.filesystem.File;
import flash.net.FileFilter;
import flash.events.Event;
import flash.filesystem.FileMode;
import flash.events.MouseEvent;
import flash.filesystem.FileStream;
const script:String = 'fl.publishDocument("{0}", "Default");';
const fla:File = File.desktopDirectory;
fla.addEventListener(Event.SELECT, onFileSelected);
const jsfl:File = File.createTempDirectory().resolvePath("publish.jsfl");
const applescript:File = File.applicationDirectory.resolvePath("/usr/bin/osascript");
const stream:FileStream = new FileStream();
const args = new <String>[];
args.push("-e");
args.push("tell application \"Flash\" to open posix file \"" + jsfl.nativePath + "\"");
const info = new NativeProcessStartupInfo();
info.executable = applescript;
info.arguments = args;
compile_btn.addEventListener(MouseEvent.CLICK, onCompile);
function onCompile(event:MouseEvent):void {
fla.browseForOpen("Select file to compile", [new FileFilter("Flash document", "*.fla")]);
}
function onFileSelected(event:Event):void {
stream.open(jsfl, FileMode.WRITE);
stream.writeUTFBytes(script.replace("{0}", fla.url));
stream.close();
executeScript();
}
function executeScript():void {
const process:NativeProcess = new NativeProcess();
process.addEventListener(NativeProcessExitEvent.EXIT, onExit);
process.start(info);
}
function onExit(event:NativeProcessExitEvent):void {
if (event.exitCode == 0) {
trace("Excution completed.");
} else {
const process:NativeProcess = event.target as NativeProcess;
const error:String = process.standardError.readUTFBytes(process.standardError.bytesAvailable);
trace("ERROR -", error);
}
jsfl.parent.deleteDirectory(true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment