Follow along with this tutorial to create a simple Flash plug-in that gets the RGB, CMYK and HSB values of the foreground color in Photoshop CS4. In this application, you will integrate the GetForegroundColor.jsx file within the Flash plug-in that you develop within a Flex Builder MXML project using the PatchPanel SWC library as the mechanism for communicating between your SWF and the host application, which is Photoshop in this example.
After completing this tutorial, you should be able to:
In order to make the most of this article, you need the following software and files:
You must be comfortable with developing in Flex Builder. Basic familiarity with ExtendScript ToolKit would be beneficial, but is optional. You should have already worked through the Hello World examples for PatchPanel.
Steps
Navigate to the /PatchPanel/ PP_IntegrateESwithPP directory and click Choose.
That should return you back to the Flex Builder Import Flex Project dialog box. From here, click the Finish button to import the project into Flex Builder.
Note: Open up the PP_IntegrateESwithPP project folder in Flex Builder. You will find a src folder that contains the basic files we will use to construct this example. In the solutions folder, the final source files are included for reference.
Import the PatchPanel SWC for CS4 applications, cs4.swc, to your project's libs subfolder
Select the folder on your file system containing cs4.swc. Hint: typically, it is in the PatchPanel/cs4/libraries folder. Click Select All and then click Finish.
Note: If you prefer, adding the cs4.swc to the Library build path of your project properties settings instead of importing will achieve the same results.
Import the Photoshop ExtendScript DOM.
// Get the Photoshop ExtendScript DOM
import com.adobe.cs4.Photoshop.*;
Set up onCreationComplete.
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="325" height="330" creationComplete="onCreationComplete()"> <mx:Script > <![CDATA[ // Called when the control is completely initialized private function onCreationComplete():void { // resize the panel Photoshop.$resize( width, height ); } ]]> </mx:Script>
Our code should look like solutions/PP_IntegrateESwithPP_v1.mxml. Verify it does before proceeding.
Define our target application.
private var app : com.adobe.cs4.Photoshop.Application;
Create instances for each of our color variables and define their datatypes.
private var fSC: SolidColor; private var fRGB: RGBColor; private var fCMYK: CMYKColor; private var fHSB: HSBColor;
Create a method that gets the current foreground color
and while we are at it, let's create an instance, app, of the Photoshop.app.
private function getCurrentColors() : void { app = Photoshop.app; }
Our code should look like solutions/PP_IntegrateESwithPP_v2.mxml. Verify it does before proceeding.
Open the application ExtendScript Toolkit (ESTK) and open the GetForeGroundColor.jsx file from within our Flex project directory PP_IntegrateESwithPP/solutions/. ESTK is located:
Mac -
<hard drive>/Applications/Utilities/ExtendScript Toolkit CS4
PC - C:\Program Files\Adobe
Utilities\ExtendScript Toolkit CS4
Ok. Let's run it by clicking the Green play button or selecting the menu option Debug > Run.
Note: In the JavaScript Console panel in ESTK, the results should be displayed for the color variables we requested in lines 6 to 8 of the code. Your exact color values might be different from below, but nonetheless, they should be populated with floating point values.
RGB: red: 43.0038922280073 green: 35.0038927048445 blue: 27.0038912817836 CMYK: cyan: 62.945556640625 magenta: 64.8834228515625 yellow: 72.49755859375 black: 73.1536865234375 HSB: hue: 30.003662109375 saturation: 37.20703125 brightness: 16.864013671875 Execution finished.
Ideally, we want to have access to these color variables within our ActionScript code.
getCurrentColors()in the PP_IntegratesESwithPP.mxml file
in Flex Builder. Because we have already declared these variables, delete the var at the beginning of each line of code.
Our code should look like this:
private function getCurrentColors() : void { app = Photoshop.app; fSC = app.foregroundColor; fRGB = fSC.rgb; fCMYK = fSC.cmyk; fHSB = fSC.hsb; resultText.text = 'foreground color captured'; }
Save our MXML file.
Let's test it! We'll need to install the SWF and JSX files in the proper location so they're available within Photoshop.
Click Finish.
Note: If you already have a copy of the swf and jsx in this directory, Flex Builder will prompt you with a replace dialog box titled 'Question'. If so, click Yes To All. You can then skip the next step because a link to that file will already be established within Photoshop.
In the menu Window > Extensions select PP_IntegrateESwithPP. Your Flash panel should open up. Clicking the Get foreground color button should place the following text within the Results TextArea:
foreground color captured
Note: If you need to make changes to your code, when you are finished, repeat steps 20 to 26 and then continue testing your updated SWF from within Photoshop.
Your results should look like solutions/PP_IntegrateESwithPP_v3.mxml.
Although we could have retrieved and output the results from Photoshop to our TextArea within a single method, we will break it down into two steps simply to show you that we can define and share variables across multiple methods in the same manner that we normally would when using ActionScript.
Create a method that will output the current foreground color RGB, CYMK and HSB values to the TextArea.
private function outputResults(): void { }
Copy the last 3 lines of code from the
GetForegroundColor.jsx in ESTK to our new method outputResults()in the PP_IntegratesESwithPP.mxml file in Flex Builder.
Our code should look like this:
private function outputResults(): void { $.writeln('RGB:\nred: '+fRGB.red+'\ngreen: '+fRGB.green+'\nblue: '+fRGB.blue); $.writeln('\n\nCMYK:\ncyan: '+fCMYK.cyan+'\nmagenta: '+fCMYK.magenta+'\nyellow: '+fCMYK.yellow+'\nblack: '+fCMYK.black); $.writeln('\n\nHSB:\nhue: '+fHSB.hue+'\nsaturation: '+fHSB.saturation+'\nbrightness: '+fHSB.brightness); }
Save our MXML file.
Note: No errors or warnings will be issued. However, for this tutorial, we want the results to be written to our resultsText TextArea.
Our code should look like solutions/PP_IntegrateESwithPP_v4.mxml. Verify it does before proceeding.
Let's replace all $.writeln( with resultsText.text
= and delete all of the last ) at the end of each line. Our outputResults()should
look like this now:
private function outputResults(): void { resultsText.text = 'RGB:\nred: '+fRGB.red+'\ngreen: '+fRGB.green+'\nblue: '+fRGB.blue; resultsText.text += '\n\nCMYK:\ncyan: '+fCMYK.cyan+'\nmagenta: '+fCMYK.magenta+'\nyellow: '+fCMYK.yellow+'\nblack: '+fCMYK.black; resultsText.text += '\n\nHSB:\nhue: '+fHSB.hue+'\nsaturation: '+fHSB.saturation+'\nbrightness: '+fHSB.brightness; }
The only thing left for us to do is to define what method is called when one of our two buttons is clicked. Refer to solutions/PP_IntegrateESwithPP_v5.mxml to verify that your code matches.
For the top button with the label "Get Foreground
Color", on click, let's call getCurrentColors().
<mx:Button x="89" y="181" label="Get Foreground Color" ick="getCurrentColors()"/>
For the bottom button with the label "Output
Results", on click, let's call outputResults().
<mx:Button x="89" y="211" label="Output Results" width="147" click="outputResults()" />
Refer to solutions/PP_IntegrateESwithPP-fin.mxml to verify that your code matches.
Woohoo! We're ready to test it now.
Repeat steps 20 to 26 and to continue testing your updated SWF from within Photoshop.
If you still want more, check out the tutorial PatchPanel: Flash Panels for multiple CS Applications.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
Dr. Woohoo (translation: Serious Fun), is a New Mexico-based artist, designer and developer creating work for a wide range of clients, including Adobe, Taylormade-Adidas Golf, Nike Golf and Camelbak. Working with digital media since 1993, Woohoo has done many professional projects, which are available at blog.drwoohoo.com and at Dr. Woohoo on Flickr.