<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ajar Productions &#187; jsfl</title>
	<atom:link href="http://ajarproductions.com/blog/category/jsfl/feed/" rel="self" type="application/rss+xml" />
	<link>http://ajarproductions.com/blog</link>
	<description>Animation &#124; Design &#124; Development</description>
	<lastBuildDate>Mon, 21 May 2012 02:13:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Creating Flash Extensions &#8211; Responding to a Dialog Choice</title>
		<link>http://ajarproductions.com/blog/2012/01/09/creating-flash-extensions-responding-to-a-dialog-choice/</link>
		<comments>http://ajarproductions.com/blog/2012/01/09/creating-flash-extensions-responding-to-a-dialog-choice/#comments</comments>
		<pubDate>Mon, 09 Jan 2012 17:30:13 +0000</pubDate>
		<dc:creator>Justin</dc:creator>
				<category><![CDATA[jsfl]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://ajarproductions.com/blog/?p=1078</guid>
		<description><![CDATA[In a previous tutorial, I showed the basics of creating dialogs. In this post, I&#8217;ll elaborate on how to respond to a dialog, specifically how to cease execution when the user has selected cancel. Here&#8217;s the code sample that was included in the aforementioned tutorial: if (result == null) { //do nothing } else { [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a title="Creating Flash Extensions — Pt. 4: User Interaction" href="http://ajarproductions.com/blog/2011/03/03/creating-flash-extensions-pt-4-ui/" target="_blank">previous tutorial,</a> I showed the basics of creating dialogs. In this post, I&#8217;ll elaborate on how to respond to a dialog, specifically how to cease execution when the user has selected <em>cancel</em>.</p>
<p>Here&#8217;s the code sample that was included in the aforementioned tutorial:</p>
<p><code>if (result == null) {<br />
//do nothing<br />
} else {<br />
//use the value of result to proceed with the script<br />
}</code></p>
<p>This example reflects code for custom <a title="XMLUI reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7fea.html" target="_blank">XMLUI</a> panel, but this technique can also be used for alerts, prompts, and confirmations.</p>
<p>There are 2 easy ways to halt the script if the user has selected <em>cancel</em> and the result is <em>null</em>:</p>
<ol>
<li>Call a function only if the result is not <em>null</em>.</li>
<li>If the conditional statement above occurs inside a function block, call <em>return</em> to exit the function.</li>
</ol>
<p>Here&#8217;s a code sample for method 1:</p>
<p><code>var dom = fl.getDocumentDOM();<br />
var xpanel = dom.xmlPanel(absoluteFileLocation);<br />
if (result == null) {<br />
//do nothing<br />
} else {<br />
run();<br />
}</code></p>
<p>You&#8217;d then define a function that execute all of the requisite code:<br />
<code>function run(){<br />
//all the action takes place here<br />
}</code></p>
<p>If the result is <em>null</em>, the function is never triggered.</p>
<p>Here&#8217;s a sample for method 2:</p>
<p><code>run();<br />
function run(){<br />
var dom = fl.getDocumentDOM();<br />
var xpanel = dom.xmlPanel(absoluteFileLocation);<br />
if (result == null) return;<br />
//continue executing otherwise<br />
}</code></p>
<p>Essentially, the entire process is wrapped in a function, so it&#8217;s easy to jump right out when needed.</p>
<p>Either method will do, sometimes one method suits a particular script.</p>
]]></content:encoded>
			<wfw:commentRss>http://ajarproductions.com/blog/2012/01/09/creating-flash-extensions-responding-to-a-dialog-choice/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Script: Backup and Restore Flash Pro Config Files</title>
		<link>http://ajarproductions.com/blog/2011/09/01/script-backup-and-restore-flash-pro-config-files/</link>
		<comments>http://ajarproductions.com/blog/2011/09/01/script-backup-and-restore-flash-pro-config-files/#comments</comments>
		<pubDate>Thu, 01 Sep 2011 15:39:40 +0000</pubDate>
		<dc:creator>Justin</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[jsfl]]></category>

		<guid isPermaLink="false">http://ajarproductions.com/blog/?p=1042</guid>
		<description><![CDATA[I had a customer who was losing Flash configuration data due to roaming profiles in Windows, similar to the situation described on this forum thread. So, I wrote this script as a potential antidote to that problem. When the script is run (by opening with Flash Pro), it will allow you to either back-up or [...]]]></description>
			<content:encoded><![CDATA[<p>I had a customer who was losing Flash configuration data due to roaming profiles in Windows, similar to the situation described on <a href="http://forums.adobe.com/message/3893312" target="_blank">this forum thread</a>.</p>
<p>So, I wrote this script as a potential antidote to that problem. When the script is run (by opening with Flash Pro), it will allow you to either back-up or restore all of your configuration files for that installation of Flash Pro.</p>
<p>In certain specific cases (no guarantees)—where the machine, the username, and the Flash version remain consistent—this could be used to maintain a <a href="http://smartmouth.ajarproductions.com" target="_blank">SmartMouth</a> activation (as well as other extensions and settings) through a Flash, CS, or OS reinstall.</p>
<p>The script is pretty experimental at this point, so please use with caution.</p>
<p><strong>Download</strong><br />
<a href="http://ajarproductions.com/flash_extensions/backup_restore_Flash_config.jsfl">backup_restore_Flash_config.jsfl</a> (right-click and choose <em>Save Link As&#8230;</em>)</p>
]]></content:encoded>
			<wfw:commentRss>http://ajarproductions.com/blog/2011/09/01/script-backup-and-restore-flash-pro-config-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating Flash Extensions — Pt. 7: Distributing to Others</title>
		<link>http://ajarproductions.com/blog/2011/03/23/creating-flash-extensions-pt-7-distributing/</link>
		<comments>http://ajarproductions.com/blog/2011/03/23/creating-flash-extensions-pt-7-distributing/#comments</comments>
		<pubDate>Wed, 23 Mar 2011 21:18:56 +0000</pubDate>
		<dc:creator>Justin</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[jsfl]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://ajarproductions.com/blog/?p=887</guid>
		<description><![CDATA[One of the absolute coolest things about Flash extensions is that you can easily share them with others. This tutorial will show you how to create package that can be emailed or posted to a website. That package can then be downloaded by others and installed using Extension Manager. Extension Manager Extension Manager (EM) is [...]]]></description>
			<content:encoded><![CDATA[<p>One of the absolute coolest things about Flash extensions is that you can easily share them with others. This tutorial will show you how to create package that can be emailed or posted to a website. That package can then be downloaded by others and installed using Extension Manager.</p>
<p><span id="more-887"></span></p>
<h2>Extension Manager</h2>
<p><a title="Extension Manager" href="http://www.adobe.com/exchange/em_download/" target="_blank">Extension Manager</a> (EM) is a free application that is automatically installed when you install any of the Adobe Creative Suite applications, including Flash Professional. If you have Flash installed, you have EM installed, whether you knew it or not. You can install extensions using EM, as well as package them.</p>
<p><img class="alignnone size-full wp-image-895" title="Extension Manager" src="http://ajarproductions.com/blog/wp-content/uploads/2011/03/jsfl_share_em.gif" alt="" width="550" height="264" /></p>
<h2>Packaging an Extension</h2>
<p>When you package an extension with EM, all of your extension files are collected into a single file or <em>package</em>. That package can then be distributed. Double-clicking on the package will automatically launch EM and begin the (brief) installation process. Before packaging your extension, you&#8217;ll need to create an MXI descriptor file.</p>
<h3>The MXI file</h3>
<p>An <a title="full MXI documentation (pdf)" href="http://www.adobe.com/go/em_file_format" target="_blank">MXI</a> file is a special <a title="XML Wikipedia entry" href="http://en.wikipedia.org/wiki/XML" target="_blank">XML</a> file that contains information about the extension (title, author, version, copyright, license agreement, which files it includes, where to install the files, etc.). Here’s sample text from an MXI file:</p>
<p><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />
&lt;macromedia-extension<br />
name="Sample Extension"<br />
version="1.0.0"<br />
type="command"&gt;<br />
&lt;author name="Your Name" /&gt;<br />
&lt;products&gt;<br />
&lt;product name="Flash" version="7" primary="true" /&gt;<br />
&lt;/products&gt;<br />
&lt;description&gt;<br />
&lt;![CDATA[<br />
This extension does A and B.<br />
]]&gt;<br />
&lt;/description&gt;<br />
&lt;ui-access&gt;<br />
&lt;![CDATA[<br />
The command can be found in ‘Commands &gt; Sample Extension’<br />
]]&gt;<br />
&lt;/ui-access&gt;<br />
&lt;license-agreement&gt;<br />
&lt;![CDATA[<br />
]]&gt;<br />
&lt;/license-agreement&gt;<br />
&lt;files&gt;<br />
&lt;file source="Sample Extension.jsfl" destination="$flash/Commands" /&gt;<br />
&lt;/files&gt;<br />
&lt;/macromedia-extension&gt;</code></p>
<p>I generally start from an existing MXI, and make any necessary changes. In the sample text above, you&#8217;ll (minimally) want to change the following items: the <em>name</em> and <em>version </em>attributes in the root tag, the <em>name</em> attribute in the <em>author</em> tag, the <em>files</em> tag(s) (and attributes), as well as the <em>description</em> and <em>ui-access</em> tag content. The <em>version</em> attribute in the product tag determines the oldest version of Flash in which your extension can be installed (7 = MX 2004, 8 = Flash 8, 9 = CS3, 10 = CS4, and so on).</p>
<p>The version you choose will depend heavily on which JSFL commands your extension uses. Note the <a title="document.crop() reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7e27.html" target="_blank">Version</a> heading at the top of the each page within the Extending Flash documentation; that will tell you the earliest version in which a particular object, method, or property is available. The <em>ui-access</em> tag is an area where you can provide instructions to the user on how to launch your extension. Both the <em>ui-access</em> and <em>description</em> will appear in EM once the extension has been installed (see the image at the top of this page). Note that both tags also contain <a title="CDATA Wikipedia entry" href="http://en.wikipedia.org/wiki/CDATA" target="_blank">CDATA</a> wrappers. The CDATA tags allow the content inside to be interpreted as plain text instead of XML content.</p>
<p>The MXI file can be edited in a simple text editor like Notepad (Windows) or TextEdit (Mac). You should have a <em>file</em> tag (inside the <em>files</em> tags) for each file used in your extension. For example, a basic tool, like the one in the <a title="Creating Flash Extensions — Pt. 6: Custom Tools" href="http://ajarproductions.com/blog/2011/03/16/creating-flash-extensions-pt-6-tools/" target="_blank">previous tutorial</a>, would require at least 3 <em>file</em> tags:</p>
<p><code>&lt;files&gt;<br />
&lt;file source="TestTool.jsfl" destination="$flash/Tools" /&gt;<br />
&lt;file source="TestTool.png" destination="$flash/Tools" /&gt;<br />
&lt;file source="TestTool.xml" destination="$flash/Tools" /&gt;<br />
&lt;/files&gt;</code></p>
<p>The <em>source</em> attribute should reference the location of the source file relative to the location of the MXI file, so that EM knows where to look for the files when packaging. For this reason, it&#8217;s easiest to keep the MXI file in the same directory as your source files. Note the use of <em>$flash</em> in the <em>destination</em> attribute. This is a handy-dandy way to reference the location of the user&#8217;s Flash Configuration directory, regardless of their version or language. The version of EM that the user (or their system) opens will determine which version of Flash that receives your extension.</p>
<h3>Generating the package</h3>
<p>After you’ve edited your MXI file in a text editor, open it in EM. It will ask where you want to save your packaged file (usually I opt to keep it with my MXI and source files). Once you’ve given your file a name and location, EM will package all the files referenced in the <em>files</em> tag of the MXI file and include them in a single MXP (or ZXP for CS5-specific extensions) file. You can now post this extension to your website or the <a title="Adobe Exchange site" href="http://adobe.com/exchange" target="_blank">Adobe Exchange</a> and share it with others. All potential users of your extension have to do (after downloading ) is double-click your MXP file and they&#8217;re off and running.</p>
<p><img class="alignnone size-full wp-image-896" title="Resulting MXP Package" src="http://ajarproductions.com/blog/wp-content/uploads/2011/03/jsfl_share_sm.gif" alt="" width="278" height="38" /></p>
<h2>Where to Go From Here</h2>
<p>That wraps up this series on creating Flash extensions. Thanks for reading!</p>
<p>There’s a wealth of cool extensions already available on the web, including a few on <a title="Our Flash Extensions (tons of freebies)" href="http://ajarproductions.com/blog/flash-extensions/" target="_blank">our site</a>. You can always learn from comparing your JSFL to that of others on the web. If you&#8217;re interested in learning more JSFL, don&#8217;t forget to check out (my shameless plug of) Chapter 4 in <a href="../books/animation-in-flash/" target="_blank">Animation with Scripting</a>, the book that I co-wrote with <a title="Chris' portfolio" href="http://mudbubble.com" target="_blank">Chris Georgenes</a>. There&#8217;s also a nice intro to JSFL, contributed by <a title="Dave's site" href="http://dave-logan.com" target="_blank">Dave Logan</a>, in Chris&#8217; <a title="How to Cheat in Flash" href="http://amzn.to/hpvDxM" target="_blank">How to Cheat in Flash</a> series. And, don&#8217;t forget about the excellent <a title="Extending Flash page on Amazon" href="http://amzn.to/dG53on" target="_blank">Extending Flash MX 2004</a> book.</p>
<p><a href="http://ajarproductions.com/blog/books/animation-in-flash/"><img class="alignnone" title="Animation with Scripting" src="http://www.adobepress.com/ShowCover.asp?isbn=0321683692&amp;type=c" alt="" width="125" height="155" /></a></p>
<p>If these tutorials helped you to create any extensions, please feel free to share them in the comments.</p>
<h2>Also in This Series</h2>
<ul>
<li><a href="../2011/02/08/creating-flash-extensions-pt1/">Creating Flash Extensions — Pt. 1: Introduction</a></li>
<li><a href="../2011/02/14/creating-flash-extensions%e2%80%94pt-2/">Creating Flash Extensions — Pt. 2: The DOM</a></li>
<li><a href="../2011/02/23/creating-flash-extensions-pt-3-manipulating-the-library/">Creating Flash Extensions — Pt. 3: Manipulating the Library</a></li>
<li><a href="../2011/03/03/creating-flash-extensions-pt-4-ui/">Creating Flash Extensions — Pt. 4: User Interaction</a></li>
<li><a href="../2011/03/10/creating-flash-extensions-pt-5-swf-panels/">Creating Flash Extensions — Pt. 5: SWF Panels</a></li>
<li><a href="../2011/03/16/creating-flash-extensions-pt-6-tools/">Creating Flash Extensions — Pt. 6: Custom Tools</a></li>
<li>Creating Flash Extensions — Pt. 7: Distributing to Others</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://ajarproductions.com/blog/2011/03/23/creating-flash-extensions-pt-7-distributing/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Creating Flash Extensions — Pt. 6: Custom Tools</title>
		<link>http://ajarproductions.com/blog/2011/03/16/creating-flash-extensions-pt-6-tools/</link>
		<comments>http://ajarproductions.com/blog/2011/03/16/creating-flash-extensions-pt-6-tools/#comments</comments>
		<pubDate>Wed, 16 Mar 2011 04:27:38 +0000</pubDate>
		<dc:creator>Justin</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[jsfl]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://ajarproductions.com/blog/?p=853</guid>
		<description><![CDATA[Flash offers the ability to create your own tools and store them in the Flash Toolbar. Tools are probably the most underutilized type of Flash extension. This tutorial will walk through the basics of creating a  tool. Building a Tool This tutorial will demonstrate how to create a line tool. Flash already has a line [...]]]></description>
			<content:encoded><![CDATA[<p>Flash offers the ability to create your own tools and store them in the Flash Toolbar. Tools are probably the most underutilized type of Flash extension. This tutorial will walk through the basics of creating a  tool.<br />
<span id="more-853"></span></p>
<h2>Building a Tool</h2>
<p>This tutorial will demonstrate how to create a line tool. Flash  already has a line tool, but this will serve as a jumping off  point that can be easily expanded into a more complex tool.</p>
<h3>File setup</h3>
<p>Similar to Commands and SWF Panels, Tools have their own folder in the Configuration directory (see the 1st tutorial for how to <a title="How to locate the Flash config directory" href="http://ajarproductions.com/blog/2011/03/10/2011/02/08/creating-flash-extensions-pt1/#locateConfig" target="_blank">locate your Configuration directory</a>). The Polystar tool (found with the Rectangle tool in the Toolbar) is a custom tool that comes installed with Flash. You can use the Polystar tool files in the <em>Configuration/Tools</em> directory as a guide when building new tools. Follow the steps below to create a tool from scratch.</p>
<ol>
<li>In Flash, create a new JSFL script (<strong>File &gt; New &gt; Flash JavaScript File</strong>).</li>
<li>Save the script as <strong>TestTool.jsfl</strong> in your <em>Configuration/Tools</em> directory.</li>
<li>Create a 16 x 15 pixel PNG image (using Flash, Photoshop, Fireworks, or your favorite image editor) and save it in the <em>Tools</em> directory as <strong>TestTool.png</strong>. This will serve as the icon in the Toolbar. I just created a diagonal line.<br />
<a href="http://ajarproductions.com/blog/wp-content/uploads/2011/03/TestTool.png"><img class="alignnone size-full wp-image-866" style="border: 0pt none; background: none repeat scroll 0% 0% #cdcdcd;" title="TestTool icon" src="http://ajarproductions.com/blog/wp-content/uploads/2011/03/TestTool.png" alt="" width="16" height="15" /></a><br />
You can also right-click and choose <em>Save Image As&#8230;</em> to save the image on this page.</li>
<li>Optionally (pun intended), you can create a <strong>TestTool.xml</strong> file in the <em>Tools</em> directory. This file can be used to establish options for the tool that can be adjusted by the user. This tutorial will not cover creating options, but you can look at the <em>Tools/PolyStar.xml</em> options for inspiration.</li>
</ol>
<p>Now that you files are set up, you&#8217;ll start adding code to your JSFL script.</p>
<h3>Predefined Functions</h3>
<p>One concept that is unique to creating a tool involves prescribed functions that Flash will call as your tool is put to use: <em>configureTool</em>, <em>notifySettingsChanged</em>, <em>setCursor</em>, <em>activate</em>, <em>deactivate</em>, <em>mouseDown</em>, <em>mouseMove</em>, and <em>mouseUp</em>. Most of these functions have to be present (and contain no errors) when Flash is started for your tool to load properly.</p>
<p>Add the following code to the top of your script:</p>
<p><code>function configureTool() {<br />
theTool = fl.tools.activeTool;<br />
theTool.setToolName( "testtool" );<br />
theTool.setIcon( "TestTool.png" );<br />
theTool.setMenuString( "Test Tool" );<br />
theTool.setToolTip( "Test Tool" );<br />
theTool.setOptionsFile( "TestTool.xml" );<br />
theTool.setPI( "shape" );<br />
}</code></p>
<p>The <em>configureTool</em> function is triggered by Flash on start-up.  The first line grabs the <a title="fl.activeTool reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7f18.html" target="_blank">active tool</a>, which happens to be your tool while your <em>configureTool</em> function is executing. The stored variable is then used to establish all of the basic setup options for the tool. The name Flash uses internally is assigned using <em>setToolName</em>. The file name called in <em>setIcon</em> should match the PNG that you saved to your <em>Tools</em> directory. The <em>setMenuString</em> and <em>setToolTip</em> methods determine how the tool name is displayed in the Flash interface and Toolbar (on rollover).  The <em>setOptionsFile</em> points to the options file that you (optionally) saved. Lastly, the <a title="setPI reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-77f1.html" target="_blank"><em>setPI</em></a> method determines which settings are shown in the Properties panel (&#8220;PI&#8221; used to stand for Properties Inspector, the previous name of the Properties panel). In this case, the shape (stroke, fill, and color) options will be displayed, since the end goal is to draw a line on Stage.</p>
<p>Add this function to the end of your script:</p>
<p><code>function notifySettingsChanged() {<br />
theTool = fl.tools.activeTool;<br />
}</code></p>
<p>The <em>notifySettingsChanged</em> function will be triggered after the user has changed any of the tool settings (those established in the XML options file). This function allows you to update any variables before taking further action. For instance, suppose you wanted to use you own stroke width setting, separate from the Properties panel. You would store the default stroke width in a variable, and update the variable to match any new settings once the <em>notifySettingsChanged</em> function is called. Since you&#8217;re not adding any options for this tutorial, this function is essentially empty, but it&#8217;s good to have it available in case you want to add this functionality later.</p>
<p>Add the following code to your script:</p>
<p><code>function setCursor() {<br />
fl.tools.setCursor( 0 );<br />
}</code></p>
<p>The setCursor function is also automatically triggered by Flash. You then use the <a title="setCursor reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7f05.html" target="_blank">fl.tools.setCursor</a> method to determine what type of cursor is displayed when you tool is in use. The options are as follows:</p>
<ul>
<li>0 = Plus cursor (+)</li>
<li>1 = black arrow</li>
<li>2 = white arrow</li>
<li>3 = four-way arrow</li>
<li>4 = two-way horizontal arrow</li>
<li>5 = two-way vertical arrow</li>
<li>6 = X</li>
<li>7 = hand cursor</li>
</ul>
<p>Add the <em>activate</em> function to be called when the user selects your tool:</p>
<p><code>function activate() {<br />
var theTool = fl.tools.activeTool;<br />
}</code></p>
<p>Consequently, you&#8217;ll also want to add the following <em>deactivate </em>function for later use:</p>
<p><code>function deactivate() {<br />
}</code></p>
<p>This function is generally called when the user selects a different tool. You can use this function to shut off any event listeners or end any processes that do need to execute while the tool is inactive.</p>
<p>Add the following to the end of your script:</p>
<p><code>function mouseDown() {<br />
fl.drawingLayer.beginDraw();<br />
startPt = fl.tools.snapPoint( fl.tools.penDownLoc );<br />
}</code></p>
<p>The <em>mouseDown</em> function will be triggered by, you guessed it, the user pressing the mouse button (while over the Stage area). The first line inside this function will activate the <em><a title="drawingLayer reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7ff7.html" target="_blank">drawingLayer</a></em> using the <em><a title="beginDraw reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7c6a.html" target="_blank">beginDraw</a></em> method. The drawing layer is a temporary visual feedback system for the user. It helps to indicate what the tool will accomplish before the user releases the mouse button and commits to an action.</p>
<p><img class="alignnone size-full wp-image-874" title="Drawing Layer in Action" src="http://ajarproductions.com/blog/wp-content/uploads/2011/03/jsfl_tools_drawlayer.gif" alt="" width="566" height="413" /></p>
<p>When you tool is complete, it will display a preview using the drawing layer, as shown in the image above. Additionally, the starting point is stored for later using in the <em>mouseDown</em> function. The <a title="snapPoint reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-77d7.html" target="_blank"><em>snapPoint</em></a> method is used to snap the location to the nearest geometric object, but this snapping can be omitted.</p>
<p>Add the following function to your script:</p>
<p><code>function mouseMove(mouseLoc) {<br />
if (fl.tools.mouseIsDown) {<br />
var pt1 = startPt;<br />
var pt2 = fl.tools.snapPoint( mouseLoc );<br />
fl.drawingLayer.beginFrame();<br />
path = fl.drawingLayer.newPath();<br />
path.addPoint(pt1.x, pt1.y);<br />
path.addPoint(pt2.x, pt2.y);<br />
fl.drawingLayer.drawPath(path);<br />
fl.drawingLayer.endFrame();<br />
}<br />
}</code></p>
<p>The <em>mouseMove</em> function is also built-in, and rather self-explanatory. It receives an argument with the current location of the mouse. The next line checks if the mouse button is down (pressed), if not, you won&#8217;t want to draw anything for this tool. Inside that <em>if</em> check, the starting point is stored as a new variable, in case you want to manipulate it without altering the <em>startPt</em> variable, and the current location of the mouse is stored. The <em><a title="beginFrame reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7c69.html" target="_blank">beginFrame</a> </em>method clears out and initializes the drawing layer for new data. A path is then created, and the two stored points are mapped to the path. The <a title="drawPath reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7c66.html" target="_blank"><em>drawPath</em></a> method renders that path onto the drawing layer (so that the user can see it). The <a title="endFrame reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7c64.html" target="_blank"><em>endFrame</em></a> method signals the end of any temporary drawing for the moment.</p>
<p>The <em>mouseMove</em> function is then called repeatedly as the user moves the mouse.</p>
<p>As an alternative to creating a path and adding points, you could use the <a title="moveTo reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7c61.html" target="_blank"><em>moveTo</em></a> and <a title="lineTo reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7c62.html" target="_blank"><em>lineTo</em></a> methods to generate the temporary line:</p>
<p><code>fl.drawingLayer.moveTo( pt1.x,  pt1.y );<br />
fl.drawingLayer.lineTo( pt2.x,  pt2.y );</code></p>
<p>While these methods may be more familiar to you from using ActionScript, you&#8217;ll see in the next bit of code why you&#8217;ve created a <a title="Path object reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7f3e.html" target="_blank">Path object</a> instead for this scenario.</p>
<p>Add this last function to your script:</p>
<p><code>function mouseUp() {<br />
fl.drawingLayer.endDraw();<br />
path.makeShape();<br />
}</code></p>
<p>The <a title="endDraw reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7c65.html" target="_blank"><em>endDraw</em></a><em> </em> method exits the drawing mode completely. Since you&#8217;ve created a path object already, you simply render the path (using the default stroke and fill settings) via the <a href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-79d6.html"><em>makeShape</em></a> method.</p>
<p>You script is now complete!</p>
<h2>Loading the New Tool</h2>
<p>Make sure the files you&#8217;ve created are all in the <em>Configuration/Tools</em> directory and follow the steps below to access and use your tool in Flash.</p>
<ol>
<li>Restart Flash.</li>
<li>Access the Customize Tools Panel dialog box, via <strong>Edit &gt; Customize Tools panel</strong> (Windows), or <strong>Flash &gt; Customize Tools panel</strong> (Mac).<br />
<img class="alignnone size-full wp-image-876" title="Customize Tools Panel" src="http://ajarproductions.com/blog/wp-content/uploads/2011/03/jsfl_tool_customize.gif" alt="" width="652" height="329" /></li>
<li>Locate <strong>Test Tool</strong> in the Available Tools menu at the left. The Current Selection menu at the right indicates the tools currently assigned to the selected location in the Tools panel at the far left. If you can&#8217;t find your tool in the list, you likely have an error in your script. Try debugging your script and restarting Flash.</li>
<li>Browse through the tools to specify the a location, click on the tool in the image of the Tools panel, then click the <em>&gt;&gt; Add &gt;&gt; </em>button. I chose to add the new tool with the Line Tool.</li>
<li>Click <em>OK</em> to save the tool settings.</li>
<li>Create a new document, <strong>File &gt; New&#8230; &gt; ActionScript 3.0</strong>. <em>Note</em>: your tool with work the same regardless of which version of ActionScript you&#8217;re using.</li>
<li>Select the <strong>Test Tool</strong> (from underneath the Line Tool, or wherever you added the Test Tool).<br />
<img class="alignnone size-full wp-image-878" title="Test Tool inside the Toolbar" src="http://ajarproductions.com/blog/wp-content/uploads/2011/03/jsfl_tools_toolbar.gif" alt="" width="135" height="72" /></li>
<li>Draw a line on Stage by pressing the mouse button in one location, dragging to another location, then releasing.<br />
<img class="alignnone size-full wp-image-879" title="The resulting line" src="http://ajarproductions.com/blog/wp-content/uploads/2011/03/jsfl_tools_line.gif" alt="" width="564" height="417" /></li>
</ol>
<p>And there you have it!</p>
<h2>Additional Tips</h2>
<p>So far, you&#8217;ve run through the bare-bones of creating a simple tool, but there are many bells and whistles that you can add. In addition to creating a settings XML file, you might consider adding some of the functionality covered in this section.</p>
<p>For instance, inside you <em>mouseMove</em> function, you might want to constrain you line to 45-degree (or 30-degree) angles when the shift button is pressed (the way many other design tools do):</p>
<p><code>if (fl.tools.shiftIsDown){<br />
//code constrain angle<br />
}</code></p>
<p>You may also want to check for a minimum distance when drawing on Stage, so that it&#8217;s more difficult for the user to accidentally draw a 1-pixel long line (see the PolyStar tool for an example of this).</p>
<p>If you play around with the tool as is, it will work pretty well on the main Timeline, but it might produce some funny results inside of nested symbols. That&#8217;s a symbol instance often has a registration point that doesn&#8217;t match the registration point of the Stage. In order to fix this, use a function to transform your coordinates to the current <a title="viewMatrix reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7cb6.html" target="_blank"><em>viewMatrix</em></a>, like so:</p>
<p><code>function transformPoint( pt,  mat ) {<br />
var x = pt.x*mat.a + pt.y*mat.c + mat.tx;<br />
var y = pt.x*mat.b + pt.y*mat.d + mat.ty;<br />
pt.x = x;<br />
pt.y = y;<br />
return pt;<br />
}</code></p>
<p>A sample call to the function might look like this:</p>
<p><code>var pt1 = transformPoint( startPt, fl.getDocumentDOM().viewMatrix);</code></p>
<p>Lastly, debugging. If you&#8217;re having trouble with your tool loading, try running the script by double-clicking on the JSFL file (or clicking the <strong>Run Script</strong> button in the Flash Script Editor). In most cases, if you have a syntax error in your script, it will be displayed in the Output panel, along with a line number.</p>
<p>Instead of restarting Flash every time you make a code update to your tool, you can simply run the following JSFL code in a new script:<br />
<code>fl.reloadTools();</code></p>
<p>I have this script saved as a command, and I add the following line to clear out any old errors in the Output panel:</p>
<p><code>fl.outputPanel.clear();</code></p>
<p>That does it for this tutorial. If you want to know more about building tools, check out <a title="Extending Flash page on Amazon" href="http://amzn.to/dG53on" target="_blank">Extending Flash MX 2004</a>.</p>
<h2>Deco tools</h2>
<p>Deco tools were added in Flash CS4 and CS5. You can also create your own Deco tool by placing a JSX file in the <em>Configuration/ProcScripts</em> directory. Note that a JSX file is the type of JavaScript used for other Creative Suite applications (not JSFL). There is very little documentation on creating a Deco tool. Here are some links with more info (<a href="http://www.adobe.com/devnet/flash/articles/deco_intro.html#articlecontentAdobe_numberedheader_2" target="_blank">1</a>, <a href="http://www.adobe.com/technology/graphics/deco.html" target="_blank">2</a>, <a href="http://forums.adobe.com/thread/638659" target="_blank">3</a>). The best way to start is to copy one of the existing JSX files and modify the code. This may be a topic in a future post.</p>
<p>The next tutorial focuses on <a title="Creating Flash Extensions — Pt. 7: Distributing to Others" href="http://ajarproductions.com/blog/2011/03/23/creating-flash-extensions-pt-7-distributing/">distributing extensions</a> to others.</p>
<h2>Also in This Series</h2>
<ul>
<li><a href="../2011/02/08/creating-flash-extensions-pt1/">Creating Flash Extensions — Pt. 1: Introduction</a></li>
<li><a href="../2011/02/14/creating-flash-extensions%e2%80%94pt-2/">Creating Flash Extensions — Pt. 2: The DOM</a></li>
<li><a href="../2011/02/23/creating-flash-extensions-pt-3-manipulating-the-library/">Creating Flash Extensions — Pt. 3: Manipulating the Library</a></li>
<li><a href="../2011/03/03/creating-flash-extensions-pt-4-ui/">Creating Flash Extensions — Pt. 4: User Interaction</a></li>
<li><a href="../2011/03/10/creating-flash-extensions-pt-5-swf-panels/">Creating Flash Extensions — Pt. 5: SWF Panels</a></li>
<li>Creating Flash Extensions — Pt. 6: Custom Tools</li>
<li><a href="../2011/03/23/creating-flash-extensions-pt-7-distributing/">Creating Flash Extensions — Pt. 7: Distributing to Others</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://ajarproductions.com/blog/2011/03/16/creating-flash-extensions-pt-6-tools/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Creating Flash Extensions — Pt. 5: SWF Panels</title>
		<link>http://ajarproductions.com/blog/2011/03/10/creating-flash-extensions-pt-5-swf-panels/</link>
		<comments>http://ajarproductions.com/blog/2011/03/10/creating-flash-extensions-pt-5-swf-panels/#comments</comments>
		<pubDate>Thu, 10 Mar 2011 22:59:19 +0000</pubDate>
		<dc:creator>Justin</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[jsfl]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://ajarproductions.com/blog/?p=823</guid>
		<description><![CDATA[If you&#8217;ve been following this tutorial series thus far, this might be the tutorial you&#8217;ve been waiting for. The ability to load a SWF into the Flash Professional interface is definitely one of the coolest aspects of Flash extensibility. There are 2 distinct advantages to building a Flash panel: Endless visual possibilities The power of [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve been following this tutorial series thus far, this might be the tutorial you&#8217;ve been waiting for. The ability to load a SWF into the Flash Professional interface is definitely one of the coolest aspects of Flash extensibility. There are 2 distinct advantages to building a Flash panel:</p>
<ol>
<li>Endless <strong>visual</strong> possibilities</li>
<li>The power of <strong>ActionScript</strong></li>
</ol>
<p>The 1st advantage should be fairly obvious to anyone familiar with Flash&#8217;s design and animation capabilities. The 2nd advantage may be slightly less obvious, since ActionScript and JSFL can appear so similar, but ActionScript (3, specifically) has a whole host of capabilities that JSFL does not (network connectivity, timed events, and the ability to interface with nearly any file type using <em>ByteArray</em>, to name a few). The topic of exploiting ActionScript capabilities in a panel actually deserves its own blog post, but for now it&#8217;s sufficient to say that SWF panels make cool stuff possible.</p>
<p>This tutorial will serve as a primer, getting you started on the basics of how to load your panel into the Flash interface and how to communicate with JSFL through your SWF.</p>
<p><span id="more-823"></span></p>
<h2>A Simple Panel</h2>
<p>A Flash panel is merely a published SWF that can be loaded into the Flash Professional interface via <strong>Window &gt; Other Panels</strong>. You can design custom panels to look like the panels that come installed with Flash, or you can make them entirely unique.</p>
<h3>Setting up the Flash file</h3>
<p>Follow the steps below to create a simple panel.</p>
<ol>
<li>Create a new Flash file (AS3),<strong> File &gt; New&#8230; &gt; ActionScript 3.0</strong>.</li>
<li>Save the file (<strong>File &gt; Save As&#8230;</strong>) as <strong>myPanel</strong>.</li>
<li>In the Properties panel, update the document size to <strong>200</strong> x <strong>300 </strong>px. The default width for a panel is generally 200 pixels.</li>
<li>If you like, update the Stage Color. When you click on the color selector, you can use the eye dropper to select the gray color within part of the Flash interface, so that your panel color matches the rest of Flash.</li>
<li>Open the Components panel, <strong>Window &gt; Components</strong>.</li>
<li>Twirl open the User Interface folder and drag a Button component onto the Stage.<br />
<img class="alignnone size-full wp-image-835" title="Button component" src="http://ajarproductions.com/blog/wp-content/uploads/2011/03/jsfl_swf_btn.gif" alt="" width="430" height="197" /></li>
<li>Give the Button instance a name of <strong>btn</strong> in the Properties panel. An instance name will allow you to reference the button from ActionScript.</li>
<li>Change the width of the button to <strong>180</strong> and adjust its position on Stage as needed.</li>
<li>Add a label to the button of <strong>Create Rectangle</strong>.<br />
<img class="alignnone size-full wp-image-836" title="Button Properties" src="http://ajarproductions.com/blog/wp-content/uploads/2011/03/jsfl_swf_props.gif" alt="" width="282" height="438" /></li>
</ol>
<p>For simplicity sake, you will use the script created in <a title="Creating Flash Extensions — Pt. 2: The DOM" href="http://ajarproductions.com/blog/2011/02/14/creating-flash-extensions%e2%80%94pt-2/" target="_blank">Part 2</a> of this series, but once you get the hang of this, you&#8217;ll be able to add as much complexity as you like.</p>
<h3>Adding the Code</h3>
<p>From a SWF, your ActionScript needs a way to talk with JSFL: enter the <em>MMExecute</em> function. This function will pass a string to be interpreted as JSFL. The JSFL code will only be executed when you&#8217;ve properly loaded your panel. Your JSFL code will not execute when you run Test Movie. More on how to install your panel in a moment.</p>
<p>When you pass JSFL code as a string, be careful to escape any characters such as quotation marks (using a backslash, e.g. /&#8221;) that will disrupt the string in ActionScript. Alternatively, if you use double-quotes for JSFL, you can use single-quotes to wrap your string, or vice versa. For example, to trigger a JSFL alert from ActionScript, you could use the following code:<br />
<code>MMExecute("alert('hello');");</code></p>
<p>Follow these steps to add the functional code to your panel:</p>
<ol>
<li>Create a new layer named <strong>actions</strong> and lock the layer.</li>
<li>Select the first frame of the actions layer and open the Actions panel (<strong>Window &gt; Actions</strong>).</li>
<li>Add the following code inside your actions panel:</li>
</ol>
<p><code>btn.addEventListener(MouseEvent.CLICK, onBtnClick);<br />
function onBtnClick(e:MouseEvent):void {<br />
var jsfl:String = new String();<br />
jsfl += "var dom = fl.getDocumentDOM();";<br />
jsfl += "dom.addNewRectangle({left:0, top:0,right:dom.width, bottom:dom.height}, 0);";<br />
MMExecute(jsfl);<br />
}</code></p>
<ol style="counter-reset: start 4;">
<li>Publish your SWF (<strong>Control &gt; Test Movie</strong>).</li>
</ol>
<p>The first line of the ActionScript adds a listener to the button component that will fire when the button is clicked. The next line begins the definition of the <em>onBtnClick</em> function that executes when the button instance is clicked. Inside the function, a string is created to house the JSFL code. That string is then passed to be executed as JSFL by the <em>MMExecute</em> function. Generally, I like to concatenate my strings on separate lines using the <em>+= </em>operator, because it keeps my JSFL somewhat readable inside the ActionScript. You&#8217;ll learn how to store the JSFL code externally later in the tutorial (a technique that I much prefer).</p>
<p>Make sure you have no errors in your Compiler Errors panel. If you press the button right now in the Test Movie window, nothing will happen, but you&#8217;ll fix that in the next step.</p>
<h3>Loading your panel into Flash Professional</h3>
<p>In order to have your SWF show up as a panel, you will need to place it in the <em>Configuration/WindowSWF</em> folder. See the 1st tutorial for how to <a title="How to locate the Flash config directory" href="../2011/02/08/creating-flash-extensions-pt1/#locateConfig" target="_blank">locate your Configuration directory</a>. Follow the steps below to launch and run your panel.</p>
<ol>
<li>Place a copy of the <em>myPanel.swf</em> file inside the <em>Configuraton/WindowSWF</em> folder.</li>
<li>Restart Flash.</li>
<li>Create a new document, <strong>File &gt; New &gt; ActionScript 3.0</strong>.</li>
<li>Open your new panel under <strong>Window &gt; Other Panels &gt; myPanel</strong> (the panel name will always be the file name minus the <em>.swf</em> extension).</li>
<li>Click the <strong>Create Rectangle</strong><em> </em>button inside the panel.</li>
</ol>
<p>You should now see a rectangle matching the Stage dimensions with the stroke and fill colors from your toolbar.</p>
<p><img class="alignnone size-full wp-image-834" title="The SWF panel in action" src="http://ajarproductions.com/blog/wp-content/uploads/2011/03/jsfl_swf_panel.gif" alt="" width="575" height="376" /></p>
<p>Success!</p>
<h2>Accessing an External Script</h2>
<p>So far, you&#8217;ve run JSFL code as a string inside ActionScript. This works fine for simple panels like the one you built in the steps above, but what about more complex actions? I recommend using the <a title="fl.runScript() reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7fde.html" target="_blank">fl.runScript</a> function.</p>
<h3>Running an entire Script</h3>
<p>If you want to run an entire script, all you need to do is pass the complete path of your script. Suppose you wanted to include a <em>myPanelActions.jsfl</em> file in the <em>WindowSWF</em> directory with you SWF file, you could use the following ActionScript:<br />
<code>MMExecute("fl.runScript(fl.configURI + 'WindowSWF/myPanelActions.jsfl')</code><code>;</code><code>");</code></p>
<p>That bit of code would execute the entire <em>myPanelActions.jsfl</em> script.</p>
<h3>Running a single function</h3>
<p>Now suppose you wanted to include several actions in a single JSFL script. So, now you have a <em>createRectangle</em> function inside your <em>myPanelActions.jsfl</em> file, along with several other functions. You could modify the <em>onBtnClick</em> function from above as follows:</p>
<p><code>function onBtnClick(e:MouseEvent):void {<br />
MMExecute("fl.runScript(fl.configURI + 'WindowSWF/myPanelActions.jsfl', 'createRectangle';)");<br />
}</code></p>
<p>That would allow you add several other buttons (and actions) to your panel, while keeping your ActionScript nice and clean. This technique also reduces the need to keep track of so many double- and single-quotes. Additionally, it&#8217;s much easier to debug (find and fix errors) by testing a separate JSFL script.</p>
<h2>SWF Panel Examples</h2>
<p>Now that you&#8217;ve got the hang of the basics, you can create all kinds of neat Flash panels. Here are some examples of <a title="Flash Extensions by Ajar Productions" href="http://ajarproductions.com/blog/flash-extensions/" target="_blank">publicly available</a> Flash panels that I&#8217;ve created. Maybe they&#8217;ll provide some inspiration as to what you can create.</p>
<h3>Queasy Tools</h3>
<p>Queasy Tools is a <a title="Wikipedia entry on &quot;Macro&quot;" href="http://en.wikipedia.org/wiki/Macro_%28computer_science%29" target="_blank">macro</a>-style panel that I created to clean up my Commands menu. It allows me to quickly tween and perform common actions that normally require several steps.<br />
<a href="http://ajarproductions.com/blog/2008/07/15/queasy-tools-panel-v150/" target="_blank"><img class="alignnone" title="Queasy Tools SWF Panel" src="http://ajarproductions.com/blog/wp-content/uploads/2008/07/queasytools_v1_5_0_screencap.jpg" alt="" width="221" height="304" /></a></p>
<h3>EaseCaddy</h3>
<p>EaseCaddy is a panel that stores custom eases for animation. Note the advantages of making such a tool as a Flash panel (displaying the list, a visible graph of the ease, graphical buttons, etc).</p>
<p><a href="http://ajarproductions.com/blog/2008/07/15/queasy-tools-panel-v150/" target="_blank"></a><a href="http://ajarproductions.com/blog/2009/02/26/the-missing-flash-panel-easecaddy/" target="_blank"><img class="alignnone" title="EaseCaddy SWF Panel" src="http://ajarproductions.com/flash_extensions/images/EaseCaddy_panel.gif" alt="" width="290" height="360" /></a></p>
<h3>FrameSync</h3>
<p>FrameSync is a panel to speed up lip-syncing. It can pull frame labels from a symbol and display them in the panel. The labels can then be clicked to update the content on the Timeline. Because of ActionScript&#8217;s timing mechanisms, the panel can also refresh automatically to capture changes in user selections.</p>
<p><a href=".http://ajarproductions.com/blog/2009/02/26/the-missing-flash-panel-easecaddy/" target="_blank"></a><a href="http://ajarproductions.com/blog/2008/09/09/new-flash-extension-framesync/" target="_blank"><img class="alignnone" title="FrameSync SWF Panel" src="http://ajarproductions.com/flash_extensions/images/FrameSync_v2_img1.gif" alt="" width="508" height="447" /></a></p>
<p>Hopefully these examples provide you with some ideas for your own SWF panels. Please feel free to post links to your Flash panel creations in the comments.</p>
<h2>Bonus: Productivity Script</h2>
<p>Once you&#8217;ve loaded your panel into the Flash interface, you don&#8217;t need to restart Flash each time you make an update to your panel. You simply need to close your panel and re-open it inside Flash.</p>
<p>Instead of running Test Movie, then copying my SWF to the config directory with each update, I created the following script that I use as a Command to publish my SWF directly to the <em>WindowSWF</em> folder:</p>
<p><code>var dom = fl.getDocumentDOM();<br />
var fileName = dom.name.split(".fla").join(".swf");<br />
var swfFile = fl.configURI + "WindowSWF/" + fileName;<br />
dom.exportSWF(swfFile, true);</code></p>
<p>So you can even use JSFL to speed up extension production!</p>
<p>The next tutorial shows how to build a <a href="http://ajarproductions.com/blog/2011/03/16/creating-flash-extensions-pt-6-tools/">custom tool for the Toolbar</a>.</p>
<h2>Also in This Series</h2>
<ul>
<li><a href="../2011/02/08/creating-flash-extensions-pt1/">Creating Flash Extensions — Pt. 1: Introduction</a></li>
<li><a href="../2011/02/14/creating-flash-extensions%e2%80%94pt-2/">Creating Flash Extensions — Pt. 2: The DOM</a></li>
<li><a href="../2011/02/23/creating-flash-extensions-pt-3-manipulating-the-library/">Creating Flash Extensions — Pt. 3: Manipulating the Library</a></li>
<li><a href="../2011/03/03/creating-flash-extensions-pt-4-ui/">Creating Flash Extensions — Pt. 4: User Interaction</a></li>
<li>Creating Flash Extensions — Pt. 5: SWF Panels</li>
<li><a href="../2011/03/16/creating-flash-extensions-pt-6-tools/">Creating Flash Extensions — Pt. 6: Custom Tools</a></li>
<li><a href="../2011/03/23/creating-flash-extensions-pt-7-distributing/">Creating Flash Extensions — Pt. 7: Distributing to Others</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://ajarproductions.com/blog/2011/03/10/creating-flash-extensions-pt-5-swf-panels/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Creating Flash Extensions — Pt. 4: User Interaction</title>
		<link>http://ajarproductions.com/blog/2011/03/03/creating-flash-extensions-pt-4-ui/</link>
		<comments>http://ajarproductions.com/blog/2011/03/03/creating-flash-extensions-pt-4-ui/#comments</comments>
		<pubDate>Thu, 03 Mar 2011 22:27:38 +0000</pubDate>
		<dc:creator>Justin</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[jsfl]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://ajarproductions.com/blog/?p=804</guid>
		<description><![CDATA[The ability to include user interaction into a JSFL script can provide a significant level of flexibility and feedback. Flexibility and feedback are especially useful when you plan to distribute your extension to others. Flexibility allows the script to run differently depending on current conditions. Is there a document open? Are items selected in the [...]]]></description>
			<content:encoded><![CDATA[<p>The ability to include user interaction into a JSFL script can provide a significant level of flexibility and feedback. Flexibility and feedback are especially useful when you plan to distribute your extension to others. <strong>Flexibility</strong> allows the script to run differently depending on current conditions. Is there a document open? Are items selected in the Library? Etc. If so, you can ask the user how to proceed. <strong>Feedback</strong> lets the user know that the script has run properly (or not), so that he/she can proceed with confidence. This tutorial will cover the primary types of user interaction available in JSFL.</p>
<p><span id="more-804"></span></p>
<h2>Basic Interaction</h2>
<p>There are 3 types of basic interaction: <em>alerts</em>, <em>confirms</em>, and <em>prompts</em>.</p>
<h3>Alert</h3>
<p>The <a title="alert method reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7f12.html" target="_blank">alert</a> method displays a <em>modal</em> message (i.e., nothing else can take place while the message box is open) with an <em>OK</em> button.</p>
<p><img class="alignnone size-full wp-image-806" title="Alert Box" src="http://ajarproductions.com/blog/wp-content/uploads/2011/02/jsfl_ui_alert.gif" alt="" width="154" height="118" /></p>
<p>Sample code:</p>
<p><code>alert('this is an alert');</code></p>
<p>The alert box serves as a simple form of communication to indicate to the user that a process succeeded, failed, or somewhere in between. Since there is only 1 button in the prompt, and the prompt must be closed before the user can continue working in Flash, there is no new information that is retrieved from the user after the message is closed. This 1-way communication is unique to the alert prompt.</p>
<h3>Confirm</h3>
<p>The <a title="confirm method reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7f16.html" target="_blank">confirm</a> method displays a modal box with an <em>OK</em> button and a <em>Cancel</em> button.</p>
<p><img class="alignnone size-full wp-image-807" title="Confirm Box" src="http://ajarproductions.com/blog/wp-content/uploads/2011/02/jsfl_ui_confirm.gif" alt="" width="260" height="119" /></p>
<p>Sample code:</p>
<p><code>var success = confirm('Are you sure you want to do this?');</code></p>
<p>Because the confirm dialog box includes 2 buttons, a boolean (true/false) value is returned to indicate which button has been pressed. This allows the script to behave differently depending on the user&#8217;s choice:</p>
<p><code>if (success) {<br />
//do something<br />
} else {<br />
//do something else<br />
}</code></p>
<p>The confirm dialog can be used for any &#8220;fork in the road&#8221; where it might be beneficial for the user to have input before proceeding.</p>
<h3>Prompt</h3>
<p>The <a title="prompt method reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7f15.html" target="_blank">prompt</a> method includes an <em>OK</em> button, a <em>Cancel</em> button, and a textfield in which a user can type a response.</p>
<p><img class="alignnone size-full wp-image-808" title="Prompt" src="http://ajarproductions.com/blog/wp-content/uploads/2011/02/jsfl_ui_prompt.gif" alt="" width="339" height="152" /></p>
<p><code>var result = prompt('Enter your text:');</code></p>
<p>If the user clicks <em>OK</em>, then the value entered in returned. If the user clicks <em>cancel</em>, a value of <em>null</em> is returned. This allows you to use the entered value to determine what occurs in the script:</p>
<p><code>if (result == null) {<br />
//do nothing<br />
} else {<br />
//use the value of result to proceed with the script<br />
}</code></p>
<p>The prompt dialog is useful for allowing the user to name items and instances. You can also convert entered values to numbers using the <em>parseInt</em> and <em>parseFloat</em> JavaScript methods.</p>
<h2>Advanced Interaction</h2>
<p>In addition to the basic user interaction methods, you can also display a completely custom <a title="XMLUI reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7fea.html" target="_blank">XMLUI</a> panel, using the <a title="document.xmlPanel reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7ca9.html" target="_blank">document.xmlPanel</a> method. These panels can contain any number of interactive items, including: buttons, checkboxes, color selectors, drop-down lists, sliders, radio buttons, text boxes, text inputs, and even SWFs.</p>
<p><img class="alignnone size-full wp-image-811" title="Combine Text XMLUI Panel" src="http://ajarproductions.com/blog/wp-content/uploads/2011/03/jsfl_ui_xmlui.gif" alt="" width="232" height="308" /></p>
<p>The content of the panel is defined by an <a title="XML entry on Wikipedia" href="http://en.wikipedia.org/wiki/Xml" target="_blank">XML</a> file, using specific tags. The tags control the layout of the panel as well as the items that appear in the panel. The panel is then launched by accessing the stored XML file:</p>
<p><code>var dom = fl.getDocumentDOM();<br />
var xpanel = dom.xmlPanel(absoluteFileLocation);</code></p>
<p>Here&#8217;s the content of the XML file that renders the Combine TextFields dialog with radio buttons above:</p>
<p><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />
&lt;dialog id="combineTF" title="Combine TextFields" buttons="accept,cancel"&gt;<br />
&lt;vbox&gt;<br />
&lt;label value="Sort from:" /&gt;<br />
&lt;radiogroup id="sortby" tabindex="5"&gt;<br />
&lt;radio label="top" accesskey="t" selected="true"/&gt;<br />
&lt;radio label="left" accesskey="l" /&gt;<br />
&lt;radio label="bottom" accesskey="b" /&gt;<br />
&lt;radio label="right" accesskey="r" /&gt;<br />
&lt;/radiogroup&gt;<br />
&lt;/vbox&gt;<br />
&lt;spacer /&gt;<br />
&lt;hbox&gt;<br />
&lt;label value="Separator:" /&gt;<br />
&lt;textbox id="separator" maxlength="20" multiline="false"<br />
value="" tabindex="1" size="12" literal="false" /&gt;<br />
&lt;/hbox&gt;<br />
&lt;/dialog&gt;</code></p>
<p>Just like the <em>prompt</em> method, the <em>xmlPanel</em> method will return <em>null</em> if the user clicks <em>Cancel</em>. If the user clicks <em>OK</em>, an object is returned containing all of the values set in the XMLUI dialog box. You can then loop through the object&#8217;s properties as shown in the <a title="document.xmlPanel reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7ca9.html" target="_blank">document.xmlPanel</a> reference, or access them directly by name, for example:</p>
<p><code>var separator = xpanel["separator"];</code></p>
<p>The only full reference for the tags and syntax used to create XMLUI dialogs can be found in <a title="Extending Flash page on Amazon" href="http://amzn.to/dG53on" target="_blank">Extending Flash MX 2004</a>, an excellent and important desk reference for anyone interested in Flash extensions. Even though the book is quite dated (in software years), much of the JSFL <a title="API entry on Wikipedia" href="http://en.wikipedia.org/wiki/Api" target="_blank">API</a> remains the same, and the book is still useful, even if you only use it for the XMLUI reference (which I&#8217;m constantly using).</p>
<p>In the next tutorial, you&#8217;ll learn how to build a persistent <a href="http://ajarproductions.com/blog/2011/03/10/creating-flash-extensions-pt-5-swf-panels/">SWF panel</a> like <a href="http://ajarproductions.com/blog/2008/07/15/queasy-tools-panel-v150/" target="_blank">Queasy Tools</a>.</p>
<h2>Also in This Series</h2>
<ul>
<li><a href="../2011/02/08/creating-flash-extensions-pt1/">Creating Flash Extensions — Pt. 1: Introduction</a></li>
<li><a href="../2011/02/14/creating-flash-extensions%e2%80%94pt-2/">Creating Flash Extensions — Pt. 2: The DOM</a></li>
<li><a href="../2011/02/23/creating-flash-extensions-pt-3-manipulating-the-library/">Creating Flash Extensions — Pt. 3: Manipulating the Library</a></li>
<li>Creating Flash Extensions — Pt. 4: User Interaction</li>
<li><a href="../2011/03/10/creating-flash-extensions-pt-5-swf-panels/">Creating Flash Extensions — Pt. 5: SWF Panels</a></li>
<li><a href="../2011/03/16/creating-flash-extensions-pt-6-tools/">Creating Flash Extensions — Pt. 6: Custom Tools</a></li>
<li><a href="../2011/03/23/creating-flash-extensions-pt-7-distributing/">Creating Flash Extensions — Pt. 7: Distributing to Others</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://ajarproductions.com/blog/2011/03/03/creating-flash-extensions-pt-4-ui/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Creating Flash Extensions — Pt. 3: Manipulating the Library</title>
		<link>http://ajarproductions.com/blog/2011/02/23/creating-flash-extensions-pt-3-manipulating-the-library/</link>
		<comments>http://ajarproductions.com/blog/2011/02/23/creating-flash-extensions-pt-3-manipulating-the-library/#comments</comments>
		<pubDate>Wed, 23 Feb 2011 23:08:37 +0000</pubDate>
		<dc:creator>Justin</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[jsfl]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://ajarproductions.com/blog/?p=766</guid>
		<description><![CDATA[Part of being a solid Flash user and a good collaborator is maintaining clean, navigable files. This means naming layers and library items clearly, deleting unused material, keeping folders organized, etc. Because these actions must be applied to all files and are quite repetitive, they&#8217;re good candidates for automation. You can use JSFL to script [...]]]></description>
			<content:encoded><![CDATA[<p>Part of being a solid Flash user and a good collaborator is maintaining clean, navigable files. This means naming layers and library items clearly, deleting unused material, keeping folders organized, etc. Because these actions must be applied to all files and are quite repetitive, they&#8217;re good candidates for automation. You can use JSFL to script all of these actions into a single keystroke or mouse click. This tutorial will focus on creating a Command (saving a Command is explained in the <a href="http://ajarproductions.com/blog/2011/02/08/creating-flash-extensions-pt1/" target="_blank">first tutorial</a>) that will clean up the items in the Library.</p>
<p><span id="more-766"></span></p>
<p>The image below shows a sampling from a typical Flash Library. With only 14 items, this Library is not terribly difficult to sort through, but as the document grows and new items are added, this Library will become unwieldy.</p>
<p><img class="alignnone size-full wp-image-773" title="Unorganized Flash document library" src="http://ajarproductions.com/blog/wp-content/uploads/2011/02/jsfl_lib_1.gif" alt="" width="283" height="321" /></p>
<p>There are a number of different ways to organize a Library. Sometimes it makes sense to organize your Library items by purpose, such as collecting all the artwork for a single animated character into one Library folder. Another common approach is to organize Library items into folders by type (i.e. movie clips in one folder, sound clips in another, and so on).</p>
<p>This tutorial will help you create a script to organize your Library by item type using methods and properties found in the <a href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7f94.html" target="_blank">Library object</a> and the <a href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7f92.html" target="_blank">Item object</a>. The Library object references the document Library itself, which contains a list Item objects. Those items will have unique methods and properties based on their types (such as the <a title="SymbolItem reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7f76.html" target="_blank">SymbolItem</a> or the <a title="VideoItem reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7f74.html" target="_blank">VideoItem</a>), but every item in the Library, regardless of type, will inherit the methods and properties of the Item object.</p>
<h2>Organzing Bitmaps</h2>
<p>You&#8217;ll first create a script to organize a single item type. Bitmaps can mess up a Library pretty quickly, so you&#8217;ll begin by wrangling those.</p>
<p>1. Open an existing document with an unorganized Library, or create a new document and add some Library items to test the script on.</p>
<p>2. Create a new JSFL script (<strong>File &gt; New &gt; Flash JavaScript File</strong>).</p>
<p>3. Add the following variables to your script so that they can be easily reference in the rest of your code:</p>
<p><code>var dom = fl.getDocumentDOM();<br />
var lib = dom.library;<br />
var items = lib.items; </code></p>
<p>4. Add a <em>folderName</em> variable after the previous variables :</p>
<p><code>var folderName = "bitmaps";</code></p>
<p>5. Add the following line to check for the existence of the folder, and to create the folder if it doesn&#8217;t exist after the code in the previous steps:</p>
<p><code>if( ! lib.itemExists(folderName) ) lib.newFolder(folderName);</code></p>
<p>The exclamation point (!) inverts the truth value (i.e. to see that the item does <em>not</em> exist). Technically, the <em>if</em> statement is unnecessary, since the <a title="newFolder reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7a27.html" target="_blank">new folder</a> will only be created if the path doesn&#8217;t already exist, but it&#8217;s a good practice to have the check, in case the functionality changes in a future version of Flash, or you want to update the script later to act differently if the folder has already been created.</p>
<p>6. Add the following code to loop through the library items and move only the bitmaps to the &#8220;bitmaps&#8221; folder:</p>
<p><code>for(var i=0; i&lt;lib.items.length; i++){<br />
var o = lib.items[i];<br />
if(o.itemType=="bitmap") lib.moveToFolder(folderName, o.name);<br />
}</code></p>
<p>When run on the Library shown above, the results appear as follows:</p>
<p><img class="alignnone size-full wp-image-774" title="New library folder created by JSFL" src="http://ajarproductions.com/blog/wp-content/uploads/2011/02/jsfl_lib_2.gif" alt="" width="282" height="325" /></p>
<p>You can use the same logic to create and organize folders for any of the available <a title="itemType reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7a6f.html" target="_blank">item types</a>.</p>
<h2>Organizing Multiple Item Types</h2>
<p>Now that the bitmaps are under control, why not take care of some other items? Follow the steps below to create and organize several folders.</p>
<p>1. Add a new array to the existing set of variables and create several new items based the folder names you want to appear in your Library:</p>
<p><code>var dom = fl.getDocumentDOM();<br />
var lib = dom.library;<br />
var items = lib.items;<br />
var folderTypes = new Array();<br />
folderTypes['bitmaps'] = "bitmap";<br />
folderTypes['mcs'] = "movie clip";<br />
folderTypes['graphics'] = "graphic";<br />
folderTypes['audio'] = "sound";<br />
folderTypes['fonts'] = "font";</code></p>
<p>Each new type should be assigned a value that matches the name of an <a title="itemType reference" href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7a6f.html" target="_blank">itemType</a>.</p>
<p>2. Then start a <em>for in</em> loop that encompasses the previous <em>for</em> loop the <em>folderTypes</em> that you&#8217;ve created:</p>
<p><code>for(var type in folderTypes) {<br />
if(!lib.itemExists(folderName)) lib.newFolder(folderName);<br />
for(var i=0; i&lt;lib.items.length; i++){<br />
var o = lib.items[i];<br />
if(o.itemType=="bitmap") lib.moveToFolder(folderName, o.name);<br />
}<br />
}</code></p>
<p>3. Add <em>folderName</em> and <em>itemType</em> variables inside the <em>for in</em> loop to store information from the <em>folderTypes</em> array:</p>
<p><code>for(var type in folderTypes) {<br />
var folderName = type;<br />
var itemType = folderTypes[folderName];<br />
</code></p>
<p>4. Alter the hard-coded &#8220;bitmap&#8221; itemType to reference the <em>itemType</em> variable, so that it will update as the <em>for in</em> loop runs:</p>
<p><code>if(o.itemType==itemType) lib.moveToFolder(folderName, o.name);<br />
</code></p>
<p>Voilà! An organized Library:</p>
<p><img class="alignnone size-full wp-image-775" title="Organized Flash document Library" src="http://ajarproductions.com/blog/wp-content/uploads/2011/02/jsfl_lib_3.gif" alt="" width="276" height="407" /></p>
<p>You can customize the script to suit your individual or project-based needs.</p>
<p>If you’re interested in learning more about workflow automation in Flash, you can also check out Chapter 4 of <a href="../2011/02/14/books/animation-in-flash/" target="_blank">Animation with Scripting</a>.</p>
<p>The next tutorial focuses on adding <a title="Creating Flash Extensions - Part 4" href="http://ajarproductions.com/blog/2011/03/03/creating-flash-extensions-pt-4-ui/">user interaction</a> to JSFL scripts.</p>
<h2>Also in This Series</h2>
<ul>
<li><a href="../2011/02/08/creating-flash-extensions-pt1/">Creating Flash Extensions — Pt. 1: Introduction</a></li>
<li><a href="../2011/02/14/creating-flash-extensions%e2%80%94pt-2/">Creating Flash Extensions — Pt. 2: The DOM</a></li>
<li>Creating Flash Extensions — Pt. 3: Manipulating the Library</li>
<li><a href="../2011/03/03/creating-flash-extensions-pt-4-ui/">Creating Flash Extensions — Pt. 4: User Interaction</a></li>
<li><a href="../2011/03/10/creating-flash-extensions-pt-5-swf-panels/">Creating Flash Extensions — Pt. 5: SWF Panels</a></li>
<li><a href="../2011/03/16/creating-flash-extensions-pt-6-tools/">Creating Flash Extensions — Pt. 6: Custom Tools</a></li>
<li><a href="../2011/03/23/creating-flash-extensions-pt-7-distributing/">Creating Flash Extensions — Pt. 7: Distributing to Others</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://ajarproductions.com/blog/2011/02/23/creating-flash-extensions-pt-3-manipulating-the-library/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating Flash Extensions — Pt. 2: The DOM</title>
		<link>http://ajarproductions.com/blog/2011/02/14/creating-flash-extensions%e2%80%94pt-2/</link>
		<comments>http://ajarproductions.com/blog/2011/02/14/creating-flash-extensions%e2%80%94pt-2/#comments</comments>
		<pubDate>Mon, 14 Feb 2011 21:40:54 +0000</pubDate>
		<dc:creator>Justin</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[jsfl]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://ajarproductions.com/blog/?p=752</guid>
		<description><![CDATA[This tutorial will focus on a couple of basics for using JSFL (defined in the previous tutorial) to alter a Flash document. Everything that can be manipulated in JSFL is considered an object. The Document Object Model (DOM) is basically the hierarchy or structure (model) of objects within a particular document. If you’ve written JavaScript [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial will focus on a couple of basics for using JSFL (defined in the previous <a href="http://ajarproductions.com/blog/2011/02/08/creating-flash-extensions-pt1/" target="_blank">tutorial</a>) to alter a Flash document. Everything that can be manipulated in JSFL is considered an object. The Document Object Model (DOM) is basically the hierarchy or structure (model) of objects within a particular document. If you’ve written JavaScript for a web browser, you’re probably somewhat familiar with accessing the DOM of an HTML document.</p>
<p><span id="more-752"></span>There’s an order to everything you do within a Flash document, whether you&#8217;ve realized before or not. The DOM makes this order explicit. Let&#8217;s start by looking at the most basic Stage object in Flash, known as an <em>element</em>. All Stage objects (e.g. bitmaps, groups, graphic symbols, and movieclip symbols) inherit the <em>properties</em> (things that describe the object) and <em>methods</em> (things that can be done to or by the object) of a basic element. The document object hierarchy for any element on Stage is as follows:</p>
<p>Flash &gt; Document &gt; Timeline &gt; Layer &gt; Frame &gt; Element</p>
<p>Each element is on a single frame, that frame is stationed on a single layer, and so on, all the way up to the level of the document, then Flash itself at the very top. So, to access the 1st element, in the 1st frame, on the 1st layer, within the first Timeline (aka Scene), within the 1st open document in Flash, you could use the following JSFL code:</p>
<p><code>fl.documents[0].timelines[0].layers[0].frames[0].elements[0];</code></p>
<p>The 0s (i.e. <em>documents[0]</em>) access the first item in an Array (collection of objects). This is known as <em>array notation</em>. Take a look at the <a href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7ffa.html" target="_blank">documentation</a> for the Document object. It has a number of methods and properties. I frequently return to the documentation when writing new scripts. I recommend downloading the <a href="http://help.adobe.com/en_US/flash/cs/extend/flash_cs5_extending.pdf" target="_blank">PDF of the entire section</a> for quick reference.</p>
<p>Follow the steps below to utilize two of the Document properties to improve the script from the first <a href="../2011/02/08/creating-flash-extensions-pt1/" target="_blank">tutorial</a>:</p>
<ol>
<li>Create a new Flash Document (AS2 or AS3), using <strong>File &gt; New</strong>.</li>
<li>Create a new Flash JavaScript file (JSFL), using <strong>File &gt; New</strong>.</li>
<li>Add the following code to your JSFL script and click the <strong>Run Script</strong> button:</li>
</ol>
<p><code>var dom = fl.getDocumentDOM();<br />
dom.addNewRectangle({left:0, top:0,right:dom.width, bottom:dom.height}, 0);</code></p>
<p>Switch back to the new document that you created. You should see a rectangle (with the fill and stroke colors from your Toolbar) that fills the Stage.</p>
<p><img class="alignnone size-full wp-image-755" title="jsfl_dom_1" src="http://ajarproductions.com/blog/wp-content/uploads/2011/02/jsfl_dom_1.gif" alt="Stage Size Rectangle created with JSFL" width="400" height="296" /></p>
<p>Let&#8217;s take a closer look at the code that you added to accomplish this. The first line creates a new variable with the name <em>dom</em>. This line also uses the <a href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7fa0.html" target="_blank">Flash object (fl)</a> to store a reference to the current document. That will save some typing in the next line and keep the script a little bit easier to read.</p>
<p>In the second line, you&#8217;ve replaced the top and left positions of the new rectangle to correspond with the <em>x </em>and <em>y</em> coordinates at the top-left corner of the Stage (0, 0). Additionally, you access the <em>width</em> and <em>height</em> properties from the <em>dom</em> variable to match the rectangle&#8217;s dimensions with those of the Stage.  This new rectangle is much more useful than the one created previously. For instance, this rectangle could now be used as a background, a mask, or (given a few more steps) to create a matte to hide the edges of the Stage. If you’re interested in learning more about creating mattes and masks with JSFL, check out Chapter 4 of <a href="../books/animation-in-flash/" target="_blank">Animation with Scripting</a>.</p>
<p>Note from the structure listed above that you can also use the DOM to alter <a href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7f6e.html" target="_blank">frames</a>, <a href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7f70.html" target="_blank">layers</a>, and <a href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7f8f.html" target="_blank">timelines</a>. For instance, the following code will create a new layer named <em>artwork</em>:</p>
<p><code>var dom = fl.getDocumentDOM();<br />
dom.getTimeline().addNewLayer("artwork");</code></p>
<p>Try experimenting with methods and properties found in the documentation to make changes in Flash documents. I recommend using &#8220;test&#8221; documents, rather than actual work files, in case something unexpected happens.</p>
<p>A similar object structure exists in JSFL to manipulate Libary items. The <a title="Creating Flash Extensions Pt. 3 - Manipulating the Library" href="http://ajarproductions.com/blog/2011/02/23/creating-flash-extensions-pt-3-manipulating-the-library/">next tutorial</a> dives deeper into the Library object.</p>
<h2>Also in This Series</h2>
<ul>
<li><a href="../2011/02/08/creating-flash-extensions-pt1/">Creating Flash Extensions — Pt. 1: Introduction</a></li>
<li>Creating Flash Extensions — Pt. 2: The DOM</li>
<li><a href="../2011/02/23/creating-flash-extensions-pt-3-manipulating-the-library/">Creating Flash Extensions — Pt. 3: Manipulating the Library</a></li>
<li><a href="../2011/03/03/creating-flash-extensions-pt-4-ui/">Creating Flash Extensions — Pt. 4: User Interaction</a></li>
<li><a href="../2011/03/10/creating-flash-extensions-pt-5-swf-panels/">Creating Flash Extensions — Pt. 5: SWF Panels</a></li>
<li><a href="../2011/03/16/creating-flash-extensions-pt-6-tools/">Creating Flash Extensions — Pt. 6: Custom Tools</a></li>
<li><a href="../2011/03/23/creating-flash-extensions-pt-7-distributing/">Creating Flash Extensions — Pt. 7: Distributing to Others</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://ajarproductions.com/blog/2011/02/14/creating-flash-extensions%e2%80%94pt-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating Flash Extensions — Part 1: Introduction</title>
		<link>http://ajarproductions.com/blog/2011/02/08/creating-flash-extensions-pt1/</link>
		<comments>http://ajarproductions.com/blog/2011/02/08/creating-flash-extensions-pt1/#comments</comments>
		<pubDate>Tue, 08 Feb 2011 20:42:10 +0000</pubDate>
		<dc:creator>Justin</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[jsfl]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://ajarproductions.com/blog/?p=737</guid>
		<description><![CDATA[The ability to create extensions is one of my favorite features in Flash Pro. You can extend Flash to include new features and automation in the form of Commands, Panels, and Tools. This series of tutorials will show you how to create your own extensions for Flash. There is a special scripting language that allows [...]]]></description>
			<content:encoded><![CDATA[<p>The ability to create <a href="http://ajarproductions.com/blog/flash-extensions/" target="_blank">extensions</a> is one of my favorite features in Flash Pro. You can extend Flash to include new features and automation in the form of Commands, Panels, and Tools. This series of tutorials will show you how to create your own extensions for Flash. There is a special scripting language that allows you to control Flash, and it is known as <em>JSFL</em>.<br />
<span id="more-737"></span></p>
<h2>JSFL (JS[JavaScript]FL[Flash])</h2>
<p>JSFL is a variant of JavaScript specifically written to interact with the Flash authoring environment. Like Actionscript, JavaScript is based on the ECMAScript standard. So, if you have any familiarity with JavaScript, or ActionScript, you will find this knowledge useful in writing JSFL. It should be noted that JSFL and ActionScript are distinct languages with distinct purposes. The latter is compiled into a SWF, and that SWF can play in the ubiquitous Flash Player. Conversely, JSFL code is executed on the spot and is used to control Flash Professional itself. Thus, JSFL can accomplish nearly everything that a user can within Flash, such as create layers, create objects, select frames, manipulate Library items, open files, and save files. Additionally, JSFL allows you to script a few tasks that users cannot normally perform (at least not easily or quickly).</p>
<p>You create a new JSFL script by choosing <strong>File &gt; New</strong> and selecting <strong>Flash JavaScript File</strong> in the New Document dialog box. The file extension for a JSFL script is always <em>.jsfl</em>. In this tutorial, you&#8217;ll start visually by copying user actions from the History Panel.</p>
<h2>The History Panel</h2>
<p>The History Panel functions as an advanced undo/redo. This panel also has the capacity to show the JSFL code necessary to duplicate each (enabled) user action.  Follow the steps below to create a JSFL command using the History Panel.</p>
<ol>
<li>Open a new Flash file.</li>
<li>Open the History Panel: <strong>Window &gt; Other Panels &gt; History</strong>.</li>
<li>Select the Rectangle Tool and draw a rectangle on the stage.  Notice that this action is recorded in the History Panel.<br />
<a href="http://ajarproductions.com/blog/wp-content/uploads/2011/02/jsfl_history_1.jpg"><img class="alignnone size-full wp-image-739" title="jsfl_history_1" src="http://ajarproductions.com/blog/wp-content/uploads/2011/02/jsfl_history_1.jpg" alt="Rectangle Step in History Panel" width="540" height="171" /></a></li>
<li>Click on the fly-out menu on the top-right of the History panel. Here you can change the display format and tooltip display.<br />
<a href="http://ajarproductions.com/blog/wp-content/uploads/2011/02/jsfl_history_2.jpg"><img class="alignnone size-full wp-image-740" title="jsfl_history_2" src="http://ajarproductions.com/blog/wp-content/uploads/2011/02/jsfl_history_2.jpg" alt="History Panel fly-out menu" width="547" height="140" /></a><br />
Change the display to show<strong> JavaScript in Panel</strong> (if it’s not selected already).<br />
<a href="http://ajarproductions.com/blog/wp-content/uploads/2011/02/jsfl_history_3.jpg"><img class="alignnone size-full wp-image-741" title="jsfl_history_3" src="http://ajarproductions.com/blog/wp-content/uploads/2011/02/jsfl_history_3.jpg" alt="JavaScript displayed in History Panel" width="540" height="169" /></a></li>
<li>Change to the Selection tool. Select the rectangle on stage and delete it.</li>
<li>Grab the slider on the left side of the History Panel and drag it up so that it’s parallel to the original rectangle command. Note that sliding the arrow undid the deletion of the rectangle. This slider functions like an undo and redo.<br />
<a href="http://ajarproductions.com/blog/wp-content/uploads/2011/02/jsfl_history_4.jpg"><img class="alignnone size-full wp-image-742" title="jsfl_history_4" src="http://ajarproductions.com/blog/wp-content/uploads/2011/02/jsfl_history_4.jpg" alt="Undo/redo slider" width="103" height="27" /></a></li>
<li>Drag the slider down to the <em>deleteSelection</em> command. Now select the original <em>addNewRectangle</em> command and hit the <strong>Replay</strong> button. This will create a rectangle with the same dimensions as the original rectangle.<br />
<a href="http://ajarproductions.com/blog/wp-content/uploads/2011/02/jsfl_history_5.jpg"><img class="alignnone size-full wp-image-743" title="jsfl_history_5" src="http://ajarproductions.com/blog/wp-content/uploads/2011/02/jsfl_history_5.jpg" alt="Selecting steps in the History Panel" width="540" height="171" /></a></li>
<li>With the <em>addNewRectangle</em> command still selected, hit <strong>Copy Steps</strong> button in the bottom right of the History Panel.</li>
<li>Drag the undo/redo slider to the very top of the History Panel to revert the document to its opened state.</li>
<li>Open a new file: <strong>File &gt; New</strong>. When the New Document dialog appears, choose <strong>Flash JavaScript File</strong> and click OK.</li>
<li>Paste the stored command into the newly opened script, <strong>Edit &gt; Paste</strong>.</li>
<li>Hit the <strong>Run Script</strong> button within the script editor and return to the Flash document.<br />
<a href="http://ajarproductions.com/blog/wp-content/uploads/2011/02/jsfl_history_6.jpg"><img class="alignnone size-full wp-image-744" title="jsfl_history_6" src="http://ajarproductions.com/blog/wp-content/uploads/2011/02/jsfl_history_6.jpg" alt="JSFL History command shown in Script Editor" width="522" height="76" /></a><br />
Note that a rectangle has been drawn on the stage in the same place and with the same dimensions as the one that was drawn with the rectangle tool. This script can now be modified to do something more useful, like get the document size and draw a rectangle to match the dimensions of the document, or looped to create several rectangles at once.</li>
</ol>
<h2>Saving a Command</h2>
<p>To save your JSFL script for later use in Flash, you&#8217;ll want to save your script to the <em>Configuration/Commands</em> directory. This will allow you to execute your script using the Commands menu (and optionally, a keyboard shortcut). The first step is to locate the <em>Configuration</em> directory for your installation of Flash.<br />
<a name="locateConfig"> </a></p>
<h3>Locating your configuration directory</h3>
<p>To locate your Configuration directory, you can check these <a href="http://help.adobe.com/en_US/flash/cs/extend/WS5b3ccc516d4fbf351e63e3d118a9024f3f-7fe8.html" target="_blank">common locations</a>, or simply create and run a new JSFL script with the following code:</p>
<p><code>fl.trace(fl.configURI);<br />
</code><br />
This script will print the location of your Configuration directory in the Output panel. Note that each installation of Flash has its own Configuration directory.</p>
<p>The <a href="http://ajarproductions.com/blog/2011/02/14/creating-flash-extensions%E2%80%94pt-2/">next tutorial in this series</a> shows you how to use JSFL to manipulate other parts of your Flash document. If you&#8217;re interested in learning more JSFL, you can also check out Chapter 4 of <a href="http://ajarproductions.com/blog/books/animation-in-flash/" target="_blank">Animation with Scripting</a>.</p>
<h2>Also in This Series</h2>
<ul>
<li>Creating Flash Extensions — Pt. 1: Introduction</li>
<li><a href="../2011/02/14/creating-flash-extensions%e2%80%94pt-2/">Creating Flash Extensions — Pt. 2: The DOM</a></li>
<li><a href="../2011/02/23/creating-flash-extensions-pt-3-manipulating-the-library/">Creating Flash Extensions — Pt. 3: Manipulating the Library</a></li>
<li><a href="../2011/03/03/creating-flash-extensions-pt-4-ui/">Creating Flash Extensions — Pt. 4: User Interaction</a></li>
<li><a href="../2011/03/10/creating-flash-extensions-pt-5-swf-panels/">Creating Flash Extensions — Pt. 5: SWF Panels</a></li>
<li><a href="../2011/03/16/creating-flash-extensions-pt-6-tools/">Creating Flash Extensions — Pt. 6: Custom Tools</a></li>
<li><a href="../2011/03/23/creating-flash-extensions-pt-7-distributing/">Creating Flash Extensions — Pt. 7: Distributing to Others</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://ajarproductions.com/blog/2011/02/08/creating-flash-extensions-pt1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Animation with Scripting for Adobe Flash is now shipping!</title>
		<link>http://ajarproductions.com/blog/2010/09/13/animation-for-flash-now-shipping/</link>
		<comments>http://ajarproductions.com/blog/2010/09/13/animation-for-flash-now-shipping/#comments</comments>
		<pubDate>Mon, 13 Sep 2010 19:08:02 +0000</pubDate>
		<dc:creator>Justin</dc:creator>
				<category><![CDATA[animation]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[jsfl]]></category>
		<category><![CDATA[books]]></category>

		<guid isPermaLink="false">http://ajarproductions.com/blog/?p=631</guid>
		<description><![CDATA[I recently completed a book with with star animator Chris Georgenes entitled Animation with Scripting for Adobe Flash Professional CS5 Studio Techniques. My wife AJ, the other half of Ajar Productions, also served as Technical Editor on the book. As you can imagine, we&#8217;re very excited about this book&#8217;s release! Here&#8217;s the info from the [...]]]></description>
			<content:encoded><![CDATA[<p>I recently completed a book with with star animator <a href="http://mudbubble.com/" target="_blank">Chris Georgenes</a> entitled <em>Animation with Scripting for Adobe Flash Professional CS5 Studio Techniques.</em> My wife AJ, the other half of Ajar Productions, also served as Technical Editor on the book. As you can imagine, we&#8217;re very excited about this book&#8217;s release!</p>
<p><img src="http://ajarproductions.com/pages/images/flashbook_big.jpg" border="0" alt="Animation with Scripting for Flash book cover" width="283px" height="350px" /></p>
<p>Here&#8217;s the info from the back cover:</p>
<p>In <em>Animation with Scripting for Adobe Flash Professional CS5 Studio Techniques</em>,  top animation designer Chris Georgenes and designer/developer Justin  Putney reveal how to create and successfully animate characters in Adobe  Flash Professional CS5 and how to push the limits of timeline animation  with stunning visual effects using ActionScript® 3.0.</p>
<p>This Studio Techniques book is designed for intermediate or advanced  users who understand the basics of Flash and want to create a more  immersive interactive experience. The book includes coverage of  storyboarding, 2D character design and rigging, character animation,  visual FX with code, workflow automation, and publishing your animation  on the web and to mobile devices.</p>
<p>You can order the book from these fine retailers:</p>
<p><a href="http://click.linksynergy.com/fs-bin/click?id=Uq4cDpmt1Rk&amp;subid=&amp;offerid=145244.1&amp;type=10&amp;tmpid=3565&amp;RD_PARM1=http%253A%252F%252Fwww.peachpit.com%252Fstore%252Fproduct.aspx%253Fisbn%253D0321683692" target="_blank">Peachpit</a> | <a href="http://www.amazon.com/gp/product/0321683692?ie=UTF8&amp;tag=ajarprodu-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0321683692">Amazon</a> | <a href="http://click.linksynergy.com/fs-bin/click?id=Uq4cDpmt1Rk&amp;subid=&amp;offerid=145244.1&amp;type=10&amp;tmpid=3566&amp;RD_PARM1=http%253A%252F%252Fwww.adobepress.com%252Fbookstore%252Fproduct.asp%253Fisbn%253D0321683692">Adobe Press</a></p>
<p>Let us know if you have any questions about the book. We hope you find it useful!</p>
]]></content:encoded>
			<wfw:commentRss>http://ajarproductions.com/blog/2010/09/13/animation-for-flash-now-shipping/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

