True Power of Photoshop Scripting: Actions


    Yesterday I wrote about some basic setup information to get started with Photoshop scripting, you can check out that post here. I will be building off of that post to continue with the rest of this post; so lets get started!

    Many advanced programs such as the entire Microsoft Office suite has the ability to record actions such as macros. These macros can be reran by performing certain events, such as on startup, on shutdown, or on some type of keyboard command being struck. This type of behavior is called an action within Photoshop, and I believe that the true power of Photoshop scripting is to call these actions from code. When you create these actions, you can have them do anything in a recorded sequence, and calling them from a script is puuurrdy powerful. The next portion will talk about these actions really quick, but if you know all about them, then scroll down to the scripting portion.

Creating a New Action Within an Action Set

NewActionSet

   

NewSetDialog

Actions

 

Calling the Recorded Action within a Script

   To get started here, lets look at the complete code from the previous post. This code can be ran from File >> Scripts >> {Name of the Script}. Once again, this script will get access to the application and then create three new documents of varying sizes.

//Set a variable holding the current preferences, because we will change
//them to our own needs
var startPreferences = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

//Create a new document by using the global 'app' variable
var newFoo = app.documents.add(630,215,128,"Foo");
newFoo = null;

var newSmallFoo = app.documents.add(70,113,128,"FooSmall"); newSmallFoo = null;

var newLongFoo = app.documents.add(218,60,128,"FooLong");
newLongFoo = null;

var newScreencastFoo = app.documents.add(388, 250,128,"FooScreencast");
newScreencastFoo = null;

//set the preferences back to what they were 
//before we adjusted them
app.preferences.rulerUnits = startPreferences;
 //This is the variable we defined earlier

    The next step will be to get load the action set into the Actions Palette. The code to do this is straight forward, calling the load method on the app global object and passing it a new File object that is a just a string representation of absolute path to the .ATN file.

app.load(new File(app.path.toString() + "/Presets/Photoshop Actions/RyanKeeter.atn"));

    The final step will be to run an action within a set that we just loaded. The method to call this is “doAction,” and the parameters are two strings, the name of the action and then the name of the action set. I will show the action set that I will be using to call methods on:

ActionsPalette

 

Below is what this call looks like:

//The parameters are the name of the action, and then
//the action set they belong to
app.doAction('Foo Action','RyanKeeter');

  REMEMBER: the action will be conducted upon the current document that has focus.

  My example and current need is simple, but you could expand it to whatever you like. My need is to create three documents of specific sizes, and then stripe some color across the documents that is common across my whole site, so lets see the entire code and then the video performing this script:

var startPreferences = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

//Load the *.atn files, or the action files
app.load(new File(app.path.toString() + "/Presets/Photoshop Actions/RyanKeeter.atn"));

var newFoo = app.documents.add(630,215,128,"Foo");
newFoo = null;

//Now that the action files are loaded, letssetup our Foo file
app.doAction('Foo Action','RyanKeeter');

var newSmallFoo = app.documents.add(70,113,128,"FooSmall"); newSmallFoo = null;

//Lets setup our Small Foo file
app.doAction('Foo Small','RyanKeeter');

var newLongFoo = app.documents.add(218,60,128,"FooLong");
newLongFoo = null;

//Lets stup out Long foo file
app.doAction('Foo Long','RyanKeeter');

var newScreencastFoo = app.documents.add(388, 250,128,"FooScreencast");
newScreencastFoo = null;

//Finally, lets setup our screenCast foo file
app.doAction('Foo Screencast','RyanKeeter');

app.preferences.rulerUnits = startPreferences; 
//This is the variable we defined earlier

Rockin, here is a vid of this all happening, hope this all helps you as much as it helps me!


Untitled from ryankeeter on Vimeo.

 

Rock on.

Ryan.

kick it on DotNetKicks.com
kick it on DotNetKicks.com

#1 WebDevVote on 12.03.2008 at 10:41 AM

You're been voted!

Track back from webdevvote.com/.../True_Power_of_P