One question that continually gets asked about JSFL is “How do I import a jpg or sound via JSFL?” And the answer is always, “You can’t.”
But wait!
Peter Elst recently asked just that question recently on the Extend Flash list, and of course got just that answer from myself and others.
However, he dug in and came up with a sort of a solution! Here’s the code he posted:
fl.openFileDialog(); fl.getDocumentDOM().selectAll(); fl.getDocumentDOM().clipCopy(); fl.getDocumentDOM().close(false); fl.documents[0].clipPaste();
Basically it opens a file dialog where the user can select the resource he wants to import. He’ll have to change the file filter to *.* in order to see all the files.
The cool thing is that you can then select a bitmap file rather than a fla or swf! Actually I was completely unaware that you could do this in Flash at all, not to mention JSFL. If you open a jpg, Flash creates a new document and imports that jpg to stage.
At that point, Peter’s script selects and copies the bitmap and closes the newly created file. You’ll only notice a quick flicker as it creates, imports, selects, copies and closes. It then pastes the bitmap in the original doc. Beautiful!
You can also choose a sound file such as an mp3. It will create a new doc and import the mp3 into the library. I haven’t dug into how you would get that into a new document. probably create a movie clip, assign that sound to its timeline, copy and past the movie clip. That should copy in the sound file as well. Or something like that…
Unfortunately, I can only get this working using the fl.openFileDialog command. It doesn’t seem to want to work with fl.openDocument, even giving it the correctly formatted uri string, it says it can’t find the file if it’s not a fla or other normally openable flash file types. Kind of a bummer, because it might be useful to pass in a path to a known, pre-existing jpg or what have you, rather than browsing.
At any rate, this is a great find, Peter, and helps to make a lot of things possible until 8ball comes out with a hopefully more robust JSFL framework.