Skip to main content
Participating Frequently
April 1, 2012
Respondido

Getting the name of the Action that runs this script

  • April 1, 2012
  • 5 respostas
  • 5014 Visualizações

If I create a action in photoshop to run a script called "test script", How can I determine within the "test script" what action name started running this script?  I'm trying to write a script to scale my images.  The scaling will be determined by what Action ran this script.

Example

Action Names are 5x7, 4x6, 8x10

Eash action above will run the same script called "scale me"

Within "scale me", the java code will determine that 4x6 started running "scale me"

"scale me" will go to the section within the java code to scale the image loaded in Photoshop to a 4x6 size.

The reason for writing the "scale me" code this way is that I can maintain only one script to scale my images.

Any suggestions or help would be appreciated.

Raymond

Este tópico foi fechado para respostas.
Melhor resposta por Michael_L_Hale

I don't know of any direct way to determine that name of the action that started a script. You can find the name of the highlighted/selected action with this code.

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID("Actn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

var desc = executeActionGet(ref);

var actionName = desc.getString( charIDToTypeID("Nm  ") );

var actionSetName = desc.getString( charIDToTypeID("PrNm") );

alert( 'Current selected action is: '+actionName+'\rin the action set: '+actionSetName );

But it is not foolproof.

A better way would be to set up your script so that it is action recordable. The details are in the scripting guide under 'JavaScript resource'. One you have that in place any action that records you script will store the values used. When the action is played again you can get those calues from the app.playbackParameters. Fit Image and Export Layers To Files are two Adobe scripts that use a javascriptresource section.

5 Respostas

Known Participant
April 29, 2012

Sorry, no matter what I do, trying to paste my code in here seems to want to insert it as a table....

Inspiring
April 30, 2012

Adobe changed/updated the forum software. Now if you look at the top right of the window where you type your post you should see a link that says 'Use advanced editor'. When you click on that link it chnages the window so that it has a >> icon. You can use that icon to insert code.

Known Participant
April 30, 2012

Michael L Hale wrote:

Adobe changed/updated the forum software. Now if you look at the top right of the window where you type your post you should see a link that says 'Use advanced editor'. When you click on that link it chnages the window so that it has a >> icon. You can use that icon to insert code.

I tried using all of the different highlighting options as well as the quote option underneath the '>>' icon and they all converted my code into the table format. Raw HTML would have required me to manually insert tab stops and line breaks for it to by readable. I just went into the HTML-editing and deleted the table, manually put in <pre></pre> tags and inserted my code between them and that seems to have worked...

Participating Frequently
April 3, 2012

Thanks for all your comments.  Micheal, I took your code for the "ActionReference()" object and it works great!  I completed my code and it is working okay (some more testing needed).

*  My Scaling Impage Script  */
var docRef = app.activeDocument;  //Pointer to current opened document


/*

finish:
if (documents.length == 0)      // Check to see if image opened if not, error message
    {
            alert('There are no opened images\r\r' +"Exiting Script");
            break finish;
}
 
 
*/ 
var savedState = docRef.activeHistoryState      // Save the history state
var originalUnit = preferences.rulerUnits   // Save the existing ruler perferences - units
var ref = new ActionReference();  //Pointer to current selected Action
ref.putEnumerated( charIDToTypeID("Actn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );  // Point to selected Action
var desc = executeActionGet(ref);
var actionName = desc.getString( charIDToTypeID("Nm  ") );  // Get Name of seclected Action
var actionSetName = desc.getString( charIDToTypeID("PrNm") ); // Get Group the selected Action belongs to
var actionnumber = 100;    //actionname =  100  (No Action Defined as default)
var portraitorienation = 0;  // Orientation default to portrait
var resolutions = 300;  // resolution default to 300
var sizeh = 999999;  // default size
var sizew = 999999;  // default size
var resolution = 300;
var scaleup1 = 0;  //enlargement
var scaleup2 = 0;  //enlargement
var scaleflag = 0;
var scaleflagg = 0
//  Assumption is the long side is always defined in sizeh
if (actionName=="4 x 5") { actionnumber =0;  sizeh = 1535; sizew = 1228;}
if (actionName=="4 x 6") { actionnumber =1;  sizeh = 1842; sizew = 1228;}
if (actionName=="5 x 7") { actionnumber =2;  sizeh = 2149; sizew = 1535;}
if (actionName=="6 x 9") { actionnumber =3;  sizeh = 2127; sizew = 1842;}
if (actionName=="8 x 10") { actionnumber =4;  sizeh = 3020; sizew = 2416;}
if (actionName=="8 x 12") { actionnumber =5;  sizeh = 3624; sizew = 2416;}
if (actionName=="11 x 14") { actionnumber =6;  sizeh = 4228; sizew = 3322;}
if (actionName=="12 x 18") { actionnumber =7;  sizeh = 5436; sizew = 3624;}
if (actionName=="12 x 24") { actionnumber =8;  sizeh = 7248; sizew = 3624;}
if (actionName=="12 x 36") { actionnumber =9;  sizeh = 10872; sizew = 3624;}

preferences.rulerUnits = Units.PIXELS


var doch = parseInt (docRef.height);
var docw = parseInt (docRef.width);
scaleflag = doch-docw;
if ( doch > docw ) { scaleflagg =1}
if  (scaleflagg == 0)
{
                portraitorienation = 1;  // Landscape Orientation
                //alert("Seclected Landscape")
                //alert(doch+" Height" +docw +" Width")
}    

var scale1 = docw - sizeh
var scale2 = doch - sizeh

if (docw > sizeh) {scaleup1=1}
if (doch > sizeh) {scaleup2=1}

// We know know what action was started (What size to scale to and the orientation - Landscape or Portrait)

      //      alert ( "Current selected action is: "+actionName+"\r\rin the action set: "+actionSetName +"\r\rAction Number is " +actionnumber +"\r\rDeminsions are (HxW):  "  +sizeh +", "+sizew );

        if (portraitorienation == 1)   //landscape if true
            {
                        if (scaleup1 == 1)
                                    {
                                        alert ( "This Photo is landscape\r\r"+ "The doc H x W are:  " +doch +" x " + docw +"\r\r" +"Resizing to (H X W:  " +sizew +", "+ sizeh + "\r\r"  + "Reduction");                               
                                        docRef.resizeImage (sizeh, undefined, resolution, ResampleMethod.BICUBICSHARPER)  // Reduction in size
                                    }
                        else
                                    {
                                        alert ( "This Photo is landscape\r\r"+ "The doc H x W are:  " +doch +" x " + docw +"\r\r" +"Resizing to (H X W:  " +sizew +", "+ sizeh + "\r\r"   + "Enlargement");                                       
                                        docRef.resizeImage (sizeh, undefined, resolution, ResampleMethod.BICUBICSMOOTHER)  //Enlargement in size
                                    } 
            }                   
        else  // portrait
            { 
                    if (scaleup2 ==1)
                                    {
                                        alert ( "This Photo is Portrait\r\r"+ "The doc H x W are:  " +doch +" x " + docw +"\r\r" +"Resizing to (H X W:  " +sizeh +", "+ sizew + "\r\r"  + "Reduction");                                                 
                                       docRef.resizeImage (undefined, sizeh, resolution, ResampleMethod.BICUBICSHARPER)
                                    }
                        else
                                    {
                                        alert ( "This Photo is Portrait\r\r"+ "The doc H x W are:  " +doch +" x " + docw +"\r\r" +"Resizing to (H X W:  " +sizeh +", "+ sizew + "\r\r"   + "Enlargement");
                                        docRef.resizeImage (undefined, sizeh, resolution, ResampleMethod.BICUBICSMOOTHER)
                                    } 
            }  

preferences.rulerUnits = originalUnit  // Restore ruler perferences - units

My script is a single file that is run by the Actions, I've created for each size and each action runs the same script.  When the script runs, it determines via "ActionReference();" what the current selected Action name is and then applies the scaling.  This script has logic for landscape and portrait and will scale using the different algorithms (bicubic xxxx) depending if it is reducing the image or enlarging.   The parametersfor the scaling is long side and constraint proportions.

So if I have to add another size to the code, all I need to do is to add one line of code to the script, copy any of the existing Actions and rename the copied Action to the size and I am done.

I'm not a experience programmer so pls don't laugh at my coding but have done some C, C++, Fortran (Yes I'm an electrical engineer) in my days.  A number of things has stumped me abit and one of them was the use of arrays which didn't work for me too well.  I was to use the array for storing my parameters of each size.  Another one that really got me puzzled was the use of the docRef = activeDocument object.  The docRef.height and docRef.width will tell you the dimensions of the document based on the units set for rules.  I set mine for pixels.  The problem I had was the statement if(docRef.height > docRef.width) { do some stufff}.  If I change the  > to < for the same document it alway gives me a "true" logical.  After some thoughts today, I suspect that true is anything not 0 and false is only 0.  I will try this out tonight.  This theory does not fit well with my understanding on how the if statement works in other languages.  Any comments on this would be appreciated.

Oh and I noticed some extra code var declarations that is not needed such as the activeHistoryState - was determining if I need to use this or the perferences.rulerunits to restore my ruler perferences and apparently the activeHistoryState does not and forgot to remove from the code.

JJMack
Community Expert
Community Expert
April 3, 2012

All the resizes I see in you script only scales one side  the width or height using sizeh for the action number the other side is undefined lets Photoshop set the other side of the image which I would guess means matain current aspect ratio.  Your using ruler units pixels resolution 300DPI yet none of your sizeh nembers when devided by 300 match up with your inch sizes?  I must be missing somthing for the what I see does look like it would work to me.

JJMack
Participating Frequently
April 3, 2012

Yes my understanding is that if you undefined the width or height parameter in the resizeimage, PS will proportionally constraint proportions.  My script does not crop as I do this manually so the aspect is correct before scaling.  What this script will do is scale the image down to the size and dpi, I need.  My pro print shop takes 300 dpi so if I want 5 x 7, it would mean 1500 x 2100 pixels.  Yes you are correct, the math here does not match my table in the script.  The script has 1535 x 2145 pixels.  The reason for this is because the printers used recommends these pixels for the sizes as the printer will truncated when printed and cut.

This is the main reason for this script-to have the images sized correctly so that there is no need for the pro shop to resample my images to match their printing.  I want full control of the processing of my images including color management.

The script appears to be working correctly but I need to test this bait more before I put it into full operation so there might be some bugs to addressed.

In practical sense, I would use this script for sizes under 8 x 12. Larger sizes, I would use gxxx fractals to enlarge manually as I believe this plugin works better for enlargements IMHO

Inspiring
April 2, 2012

I don't know of any methods to get the action; but would a custom UI work for you?  You could create a window that comes up with a set of radio buttons containing the different sizes you can run, then just select the correct one and hit Ok.  A lot less actions to sort through this way.

JJMack
Community Expert
Community Expert
April 3, 2012

The problem with a UI is its not something you want in a batch process that what a plug-in is about you use the Plug-in Script UI while recording an action the plug-in script records the setting use into the actions step. When the Action is played the Action passes the settings to the script dialog and the script bypasses displaying the UI.

JJMack
JJMack
Community Expert
Community Expert
April 2, 2012


Action Names are 5x7, 4x6, 8x10

Eash action above will run the same script called "scale me"

The first problem I see is your use of "scale me" and then the use of three different aspect ratios 5:7, 2:3 and 4:5.  You need two scripts one script to crop the image to the aspect ratio you need and a second one to scale the image to the size you want and then there the other problem of orintation. All the sizes you have noted 5x7, 4x6 and 8x10 are portrait images do you also want to be able to handle landscape images and would you like to be able to batch process these actions of yours?

Actually to do what you want to do is easy if you download my crafting actions package.  In that package I include a dozen scripts two are Plug-in scripts I wrote based on Adobe Fit Image Plug-in scripts. These two scripts work on both Portrait and Landscape images. To the aspect ratio script a 2 and 3 aspect would be 2:3 for  Portraits and a 3:2 for Landscapes The script preserves images orintation. What is great about a Plug-in Script like Fit image is when you record its use in an action the parameters you use in its dialiog are recorded into the actions step by the Script so that when the action is played the script's dialog is not displayed unless you turn it on in the action step, Instead the Recorded settings are set into the dilaog and the dialog is not displayed.  The two Plug-in Scripts in my package are named AspectRatioSelection.jsx and LongSidePrintLength.jsx.  The actions you would record are quite  simple.

Step 1 Menu File>Automate>AspectRatioSelection  in the dialog set 2, 3,  centered, rectangle, replace, 0 feather, no antialias then click OK

Step 2 Menu Image>Crop

Step 3 Menu File>Automate>LongSidePrintLength in the dialog set 6 then click OK

That would be your 4x6 action your other two actions would be the same actions with different settings recorded into the two plug-in steps.

Crafting Actions Package

Contains

  • Action Actions Palette Tips.txt
  • Action Creation Guidelines.txt
  • Action Dealing with Image Size.txt
  • Action Enhanced via Scripted Photoshop Functions.txt
  • CraftedActions.atn Sample Action set includes an example Watermarking action
  • Sample Actions.txt Photoshop CraftedActions set saved as a text file.
  • 12 Scripts for actions

Example

Download

JJMack
Inspiring
April 2, 2012

JJMack wrote:

The first problem I see is your use of "scale me" and then the use of three different aspect ratios 5:7, 2:3 and 4:5.  You need two scripts one script to crop the image to the aspect ratio you need and a second one to scale the image to the size you want and then there the other problem of orintation.

Multiple scripts are not needed to handle the different sizes and aspects ratios. The script logic could handle any number of sizes( scaling and cropping ).

The problem is that when it runs it doesn't know what size is wanted. By adding the javascriptresource section to the script the size needed is stored in the recorded action that calls the script.

JJMack
Community Expert
Community Expert
April 3, 2012

Michael L Hale wrote:

Multiple scripts are not needed to handle the different sizes and aspects ratios. The script logic could handle any number of sizes( scaling and cropping ).

The problem is that when it runs it doesn't know what size is wanted. By adding the javascriptresource section to the script the size needed is stored in the recorded action that calls the script.

Yes a custom script can be written to do both the crop and the scaling one could also not use crop an try content aware sizing.  But for best results cropping and scaling is best IMO.  I wrote the plug-in script for several reasons. I don't like to resample I rather Crop to a given aspect ratio and scale by changing the DPI.   I also did not code the Aspect Ratio Script to  actually do a crop because any crop changes an images composition.  While a center crop is often satisfactory its not always.  With these plug-in script it very ease to create interactive Actions for making any aspect ratio standard size print file where the user controls the final composition. The action just has one additional step over the one I posted.  After the Aspect Ratio Selection step you add a Transform Selection step that rotated the selection 180 degrees which you make interactive.  When the action runs the user is put into an interactive Transform Selection dialog. Holding down the shift key will constrain the tramsform to maintain the Selection Aspect Ratio adding the Alt key will Transform from the center. The user controls the final composition.  The 180 rotation was recorded  to keep the same selection so that if the default center crop is OK all the user has to do is press enter. If the 180 was not recorded pressing the enter key would cancel the interactive transform selection step and the action would stop.

JJMack
Inspiring
April 1, 2012

I don't know of any direct way to determine that name of the action that started a script. You can find the name of the highlighted/selected action with this code.

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID("Actn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

var desc = executeActionGet(ref);

var actionName = desc.getString( charIDToTypeID("Nm  ") );

var actionSetName = desc.getString( charIDToTypeID("PrNm") );

alert( 'Current selected action is: '+actionName+'\rin the action set: '+actionSetName );

But it is not foolproof.

A better way would be to set up your script so that it is action recordable. The details are in the scripting guide under 'JavaScript resource'. One you have that in place any action that records you script will store the values used. When the action is played again you can get those calues from the app.playbackParameters. Fit Image and Export Layers To Files are two Adobe scripts that use a javascriptresource section.

Known Participant
April 29, 2012

Ok, I'm using Photoshop CS5 standard, and for me the above Action Manager code returns the name of the currently highlighted/selected STEP of the action I'm running, not the name of the action. Since the step that runs this code from within my actions is simply labeled "Scripts", that is all I ever get in the actionName variable. The actionSetName variable is always properly set. This is consistent whether my action palette is in button mode or not.

I further verified this by highlighting various actions and specific steps within various actions and executing the above AM code by selecting File->Scripts->Browse... and selecting my script file with the AM code. If I highlight the name of an action, that is what my alert(actionName) displays. If I highlight a specific step within an action, the text for that step is what is displayed, not the name of the action.

Michael, I'm not sure if this is what you meant by your caveat "This is not foolproof..." but the original poster seemed to get this working so I'm puzzled by what I might be doing wrong?

Appreciate any insight, thanks!

Participating Frequently
April 29, 2012

I am the originator of the question.  My action has only one step and that is to run the script.  The script retrieves the name of the selected script and scales occordingly as per my code in this post.  It works great.  I have no other steps in the script so didn't realize that the action name brings back the step name selected.  I guess if I selected the script step, my script won't work as per your description.  Nice to know that

The "not fool proof" as I found out does not work with the PS batch jobs so that's something I have to figure out one of these days.  I guess a work around for this is to have a script file for each size and this file initializes a variable to set size and then have another file with the rest of the common code and then use the "include" syntax in each script file for each size.  I be interested in any follow ups