Flash ActionScript 2 PrintJob function example

This ActionScript 2 code demonstrates how to use the PrintJob() function to generate a print job from JPG images in a subfolder.

In summary, what occurs is that all the images are imported/created as child movie clips of the parent movie clip, printZone, and then all the child movie clips are passed to the PrintJob object, pj.

The biggest problem with this code is that for some reason the print dialog box does not give you the option to print selected pages.

var printPath:String = "pages/"; // The is relative path to the subfolder containing the images.
var printLabel:String = "page-"; // For some reason I decided to use "page-" as the prefix for all the image files.
var printType:String = ".jpg"; // This is the file type for the images. You can use SWF files as well as PNGs instead, if you like.
var numPages:Number = 32; // The number of pages is hard coded but, given a chance to do it again, I'd probably go with an XML file definition.
var printXMin:Number = 0; // This variable and the next define the position on the page for the image. In this case, the upper left-hand corner is set to zero.
var printYMin:Number = 0;
var printXMax:Number = 1275; // This variable and the next define the number of pixels to force into the page from the source image.
var printYMax:Number = 1650; // And I set these variable to match the actual sizes of the JPG images so it would print the entire JPG image.
var printXScale:Number = 50; // An actual 8 1/2 X 11 sheet of paper is about 637.5 X 825 screen pixels and since the images are so much larger
var printYScale:Number = 50; // I had to scale them down 50% to fit on the page.

printCatalog = function() { // This function is called when some print button movie clip is released.
  loadingIndicator._visible = true; // The loadingIndicator movie clip nests a progressBar movie clip that is used to feedback progress for processes in general.
  var printLoader:MovieClipLoader = new MovieClipLoader(); // This is the MovieClipLoader object that manages to import the JPGs to the printZone movie clip.
  var printLoaderListener:Object = new Object(); // This object hosts the functions that are called for the MovieClipLoader object.
  var pageCount:Number = 1; // This variable keeps track of what page is being processed. 


  printLoaderListener.onLoadProgress = function (target_mc:MovieClip, bytesLoaded:Number, bytesTotal:Number) {// This function updates the loadingIndicator to show progress of loading the individual pages.
    loadingIndicator.progressBar._width = Math.round(bytesLoaded/bytesTotal*100);

  printLoaderListener.onLoadInit = function (mc:MovieClip) {//This function is called when the image is done loading.
    pageCount++; //When the image is loaded then the pageCounter is incremented. This could probably be put after the if-statement and the (numPages+1) term in the if-statement could be simply numPages.
    if (pageCount==(numPages+1)) {
      printDocument(); // If this is the last page then go ahead and print the document.
    } else {
      printNextPage(); // Only when the image is loaded do we proceed to load the next page image.    }

  printDocument = function () {//This function simply takes the entire array of movie clip children from printZone and adds them as pages to the PrintJob() object, pj.
    var pj = new PrintJob();
    if(pj.start()) {
      var i = new Number();
      for (i=1;i<=numPages;i++) {
        printZone["page"+i]._xscale = printXScale;
        printZone["page"+i]._yscale = printYScale;
        pj.addPage(eval("printZone.page"+i), {xMin:printXMin,xMax:printXMax,yMin:printYMin,yMax:printYMax},{printAsBitmap:true}, 1);
        loadingIndicator.progressBar._width = Math.round(i/numPages*100);//Here the progress of the pages adds is reflected in the loadingIndicator movie clip.
      loadingIndicator._visible = false;
      delete pj;

  printNextPage = function () { //This function parses the file name of the page and then
    var pageString = new String();
    if (pageCount<10) {// For whatever reason, I formatted the files to be "page-001.jpg, page-002.jpg, ... , page-010.jpg, etc,..." This if-then statement formats the page element to match.
      pageString = "00"+pageCount.toString();
    } else {
      pageString = "0"+pageCount.toString();
    var fileName:String = printPath+printLabel+pageString+printType;
    printZone.createEmptyMovieClip("page"+pageCount, pageCount);
    printLoader.loadClip(fileName, eval("printZone.page"+pageCount));// Here the image is called to load.

  printNextPage(); // So here's where we start by loading the first page.

This swath of code is fairly down-and-dirty. It could probably stand to have some error handlers. Also, a lot of this information could be offloaded to an XML file and make it more flexible.


Posted: July 14th, 2009 | Author: | Filed under: Code Reference | Tags: , | 1 Comment »

Action Script 2.0 for playing a loop of a series of SWF files defined in an array.

So, yeah. Say you want to create a SWF that imports a series of SWFs and loops them. Well, here’s one way to do this.

// Action Script 2.0 for playing a loop of a series of SWF files defined in an array.
// Completion of the individual SWFs is defined as the event when the current frame
// is equal to the total number of frames.

fscommand("showmenu", "false");

// You'll need a movie clip on the stage named movies_mc to hang the SWFs from. If you decide not to have one you can uncomment the following line to generate one.
//this.createEmptyMovieClip("movies_mc", 1);
var main_mc:MovieClipLoader = new MovieClipLoader();

var mcListener:Object = new Object();

var movieTitle:Array = ["movie1.swf", "movie2.swf", "movie3.swf", "movie4.swf", "movie5.swf"];
var i:Number = 0;
var ii:Number = 0;

mcListener.onLoadInit = function(target_mc:MovieClip) {
    target_mc.onEnterFrame = function() {
        if (this._currentframe >= this._totalframes) {
            trace("total frames = " + this._totalframes);

            loadNextMovie(movieTitle[modFunction(i, movieTitle.length)]);
            delete this.onEnterFrame;
        } else {
            trace(target_mc._currentframe+" OF "+target_mc._totalframes);

function loadNextMovie(moviePath) {
    if (i>0){main_mc.unloadClip(movies_mc);}
    trace("loading movie:" + moviePath + ", i="+i);
    main_mc.loadClip(moviePath, movies_mc);

function modFunction(n:Number,b:Number):Number {
  //****** MOD FUNCTION
  // n = number to apply the mod function to. b = base of for the mod function
  var result:Number = new Number();
  result = n - b*(Math.floor(n/b));
  return result;


This script should be loaded into the first frame of a single frame FLA.

Posted: May 25th, 2009 | Author: | Filed under: Code Reference | Tags: , | No Comments »