Monthly Archives: December 2010

Create Flash Documents Programmatically

Sometimes it’s very useful to be able to create flash documents programmatically (e.g. batch conversions). Adobe Flash supports automation via the JavaScript Flash language (JSFL). Finally when the script is finished you can call it from the command line using AppleScript.

How it works exactly is demonstrated in the following one minute screencast:

Embedding Flash Into Java Applications

To be able to embed Flash into a Java application one needs a third party library. ComfyJ from TeamDev is such a library.

ComfyJ is a bidirectional Java-COM bridge which allows me to use ActiveX/COM objects from Java.

Here a screenshot with a SWF running inside a Java Swing user interface:
FlashItExample

In Java the initialization looks like:

    private static final String FLASH_PROGID = "ShockwaveFlash.ShockwaveFlash";
    private OleContainer _container = new OleContainer();

    …

        OleFunctions.oleInitialize();


        getContentPane().add(_container, BorderLayout.CENTER);
        createOleObject();
        addWindowListener(new WindowAdapter()
        {
            public void windowOpened(WindowEvent e)
            {
                showOleObject();
            }

            public void windowClosing(WindowEvent e)
            {
                destroyOleObject();
            }
        });


Some simple helper methods:


    private void createOleObject()
    {
        _container.createObject(FLASH_PROGID);
    }

    private void showOleObject()
    {
        _container.doVerb(OleVerbs.SHOW);
    }

    private void destroyOleObject()
    {
        _container.destroyObject();
    }

To open a SWF file:


            OleMessageLoop.invokeMethod(this, "openFile", new Object[]{filename});

And finally here is the openFile method:


    public void openFile(String filePath)
    {
        File file = new File(filePath);

        if(!file.exists()) {
            throw new RuntimeException("Couldn't find file with movie: " + filePath);
        }

        IDispatchImpl flash = new IDispatchImpl(_container.getOleObject());
        Automation flashAutomation = new Automation(flash);

        // load movie
        flashAutomation.setProperty("Movie", filePath);
        flashAutomation.invoke("Play");
    }


The class path should look similar to:

flashIt.jar;lib/comfyj-2.7.jar;lib/jniwrap-3.8.jar;lib/log4j-1.2.15.jar;lib/slf4j-api-1.5.8.jar;lib/slf4j-log4j12-1.5.8.jar;lib/winpack-3.8.jar

And don’t forget to pass a parameter to the Java VM with the (relative) path to the folder with the native DLLs:

-Djava.library.path=dll

BTW, I’m not associated with TeamDev other than being a mostly happy user. Here’s the link to the library:

http://www.teamdev.com/comfyj/