Skip navigation
theteddy
Currently Being Moderated

Getting the name of the Action that runs this script

Mar 31, 2012 11:33 PM

Tags: #scripting #actions

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

 
Replies
  • Currently Being Moderated
    Apr 1, 2012 9:01 AM   in reply to theteddy

    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.

     
    |
    Mark as:
  • JJMack
    5,977 posts
    Jan 9, 2006
    Currently Being Moderated
    Apr 1, 2012 8:24 PM   in reply to theteddy


    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

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 2, 2012 9:46 AM   in reply to JJMack

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 2, 2012 12:26 PM   in reply to theteddy

    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.

     
    |
    Mark as:
  • JJMack
    5,977 posts
    Jan 9, 2006
    Currently Being Moderated
    Apr 2, 2012 8:49 PM   in reply to Michael L Hale

    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.

     
    |
    Mark as:
  • JJMack
    5,977 posts
    Jan 9, 2006
    Currently Being Moderated
    Apr 2, 2012 8:57 PM   in reply to dgolberg

    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.

     
    |
    Mark as:
  • JJMack
    5,977 posts
    Jan 9, 2006
    Currently Being Moderated
    Apr 2, 2012 9:35 PM   in reply to theteddy

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 2, 2012 9:55 PM   in reply to theteddy

    The array is a great way of storing a lot of data under the same variable and accessing it with only a slight change to the variable name.  They're most useful when paired with for loops, but handy in other instances as well.  To use them, you simply name the array similar to a regular variable "var arrayName = new Array();"  or the quicker method "var arrayName = [];" Next, you need to add data to the array.  There are many ways to do this, and they all serve their purpose, but I just do the simple "arrayName[0] = data1;"  For the next one you'd just change the 0 to a 1, then to a 2, etc. etc.  The best way to look at them is as a list, only, the first item on the list is numbered 0 instead of 1 (I don't know why, that's just how it is).  so if your list is "data1", "data2", and "data3", you can then access the second item in your list (data2) by entering "arrayName[1]" as if it were a regular variable, and whatever is stored there will be called up.  You can store strings, ints, or even other variables.

     

    The "docRef = app.activeDocument" statement serves 2 purposes, really.  One, it ensures that the code is run on the correct document (if more than one is opened at a time; the front most one), and the second, it makes the writing process a lot quicker since you only have to type "docRef" rather than "app.activeDocument" each time you need to specify the document to work with; so it's kind of a speed hack as well to allow you to code faster (and your lines are a lot shorter, so there's less code).

     

    For the "if(docRef.height > docRef.width) { do some stuff; }", this is basically just checking to see if the height is greater than the width; if so (ie: true), "do some stuff" (ie: the code in the statment's { } brackets; in this case, set the variable "scaleup1" to a value of 1); if not (false), go to the next line of code, skipping any in the { }.  The easiest way to look at this is from a mathematical perspective; the docRef (ie: the document you're working with) .height gets the height in whatever measurement is being used (typically pixels), and .width obviously the width.  What the code sees (let's say the file is 800 wide by 600 tall) is; instead of "docRef.width" it sees "800" and instead of "docRef.height" it sees "600"; so effectively, it becomes "if(800 > 600)".  Since 800 IS greater than 600, it runs the code in the { }.  If it wasn't it would skip to either an else statement (if there is one) or the next line of code after the if statement's closing }.

     

    Lastly, the "activeHistoryState" is used to store the current history state (ie: undo point) to a variable.  In the code you listed, this line can actually be removed as it only stores the history state but doesn't actually do anything with it (unless there's more code you left out).  If you wanted to undo everything that was just done once the code finishes, you'd just add the line "docRef.activeHistoryState = savedState;" to the end of the document (savedState is the variable that history state was stored to).

     

    Anyway, I hope this helps clear up some of the things you mentioned having a little trouble understanding.

     
    |
    Mark as:
  • JJMack
    5,977 posts
    Jan 9, 2006
    Currently Being Moderated
    Apr 3, 2012 5:40 AM   in reply to theteddy

    Your confusing me even more now.  Your Script is resampling your images your images are being resampled???.  With a good images printer there is no reason you have to resample your images at all if you have a lot of pixels to begin with.  Images Printers have very high resolution these days with quality paper  they can print in any size pixel they need to up to their maximun resolition.  The Printers DPI resolution setting is a print quality setting not the images DPI and it does not need to match or be a mutiple if the images DPI resolution.  Printer use their higher resolutio small ink droplets to paint in a larger image pixel many drops are deposited into an image's pixle to create a pixel the correct size and color.

     

    My two Plug-in scripts were written to be used as utilites.  They do not resample document.  AspectRatioSelection only created a selection you can use to crop your image to a particular aspect ratio. The selection will be the largest posible selection that is either centered or orignate from the top left cornor.  The LongSidePrintLength just changes the document DPI setting so that the exiting document pixels will print at a size that will result in the desired print size.  All you need to do is create the three step action to create the ptint size image files you want. Thes action cam be batched with Automate>Batch, Scripts>Image Processor or Automate>Image Processor Pro...

     
    |
    Mark as:
  • JJMack
    5,977 posts
    Jan 9, 2006
    Currently Being Moderated
    Apr 3, 2012 7:51 AM   in reply to theteddy

    What image printer these days have a max resolution of only 300 dpi.   My image printer is not new a Epson 4800 have had it for many years.  With high quality paper you can set it to use its highest quality setting of 2880 DPI with lower quality photo paper it quality setting can be set to 1440, 720, 360 it can print pixels any size lower then its print quality setting. There would be no problem printing your 3000 pixels with the images DPI setting set to 1000DPI to print it 3" using high quality paper and using one of the printer two highest quality settings 2880 or 1440 both can handle the 1000dip image setting.  You Eyes will not be able to resolve down to 1000DPI pixel print size most likely your eyes can only do between 200 to 300 dpi at close range.

     

    If your printers max resolution is 300DPI all you would need todo to the actions I outlined for you is to add an Image Size step at the end last step check resample, constrain and set the Print DPI setting to 300 DPI and click OK without setting the width or hight.  Photoshop will resample the correctly size current image document to your Printers resolution.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 28, 2012 5:12 PM   in reply to Michael L Hale

    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!

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 28, 2012 8:02 PM   in reply to theteddy

    theteddy wrote:

     

    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 tried doing that exactly, recorded an action with a single step that consists of calling my test script with the above Action Manager code... when I run the action, it executes the script whose last line of code is simply:

     

    alert(actionName);

     

    but all that ever shows up in the alert box is "Scripts"...

     

    Michael's earlier suggestion to make your action recordable is a sound one and not terribly difficult to implement, I've done this route several times before with great success. What intrigued my about your post and Michael's response was that I asked something similar a couple years ago (might have been on ps-scripts and not here) and was told at the time that there was not a way for a script to tell what action called it, but this thread seemed to show that this was not the case. I thought either the AM code Michael posted had been "discovered" since then or that it was something that was added as a feature to the newer versions of PS.

     

    Thanks for replying "theteddy", hopefully someone will have an idea what is going on.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 28, 2012 9:02 PM   in reply to badeshazer

    The action manager code I posted is not foolproof because there isn't a good way to know what is highlighted in the action panel.

     

    If an action set is hightlighted it will throw an error. You could get around that with a try/catch block.

     

    If an action is highlighted the code works as expected. That is to say "Nm  " returns the action name and "PrNm" returns the actionSet Name.

     

    If a step in an action is highlighted "Nm  " returns the name of the step and "PrNm" returns the action name.

     

    So I guess you could check it the desc.getString( charIDToTypeID("Nm  ") ) == 'Scripts" and if it does( and you don't have any actions named "Scripts" ) you can get the action name using desc.getString( charIDToTypeID("PrNm") )

     

    You could also use desc.getInteger(charIDToTypeID("Cnt ")) == 0 to help determine what is highlighted. When "Cnt " returns 0 that means there is either one step in the action or one action in the set depending on what is highlighted.

     

    But there isn't a 'typename' key in the descriptor to directly determine what is highlighted.

     

    And yes, the "Actn" class was added( I think around CS3 ) so this will not work in older versions of Photoshop.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 30, 2012 9:44 AM   in reply to Michael L Hale

    Michael L Hale wrote:

     

    If an action is highlighted the code works as expected. That is to say "Nm  " returns the action name and "PrNm" returns the actionSet Name.

     

    And the only way that I can think of this being the case is if the script is executed manually instead of being called from an action... if you run a script from an action, and that script uses this AM code to get the actionName/actionSetName, the actionName will always be "Scripts" and the actionSetName will be the name of the action (tested on Mac OSX Lion and Photoshop CS5 standard). As such, I cannot see how "theteddy"'s code in reply #5 is working as he/she claims unless all of these lines:

     

    if (actionName=="4 x 5") { actionnumber =0;  sizeh = 1535; sizew = 1228;}

     

    were changed to read:

     

    if (actionSetName=="4 x 5") { actionnumber =0;  sizeh = 1535; sizew = 1228;}

     

    Hey TheTeddy, if you're still around and following this thread is it possible that either you made these changes in the actual code that you're using as opposed to what you posted?

     

    Here is the code that I have working and added to my function library with comments based on this discussion and my testing:

     

              try {
                   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") );
                   if (actionName != "Scripts") {
                        // We must be running the script manually and an action is highlighted
                        // in the actions palette
                        return actionName;
                   } else {
                        // Script is being run from an action, so the PrNm is actually the action
                        // name and the Nm is the name of the action step (should always be "Scripts")
                        return actionSetName;
                   }
              } catch(e) {
                   // Either the action palette is in button mode, no action is highlighted
                   // or an Action Set is highlighted
                   return null;
              }
    

     


     
    |
    Mark as:
  • Currently Being Moderated
    Apr 29, 2012 10:51 AM   in reply to theteddy

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

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 30, 2012 8:52 AM   in reply to badeshazer

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 30, 2012 9:47 AM   in reply to Michael L Hale

    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...

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 30, 2012 10:11 AM   in reply to badeshazer

    This is an example of using Insert-Syntax highlighting-Plain in the advanced editor.

    
    
    try {
                   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") );
                   if (actionName != "Scripts") {
                        // We must be running the script manually and an action is highlighted
                        // in the actions palette
                        return actionName;
                   } else {
                        // Script is being run from an action, so the PrNm is actually the action
                        // name and the Nm is the name of the action step (should always be "Scripts")
                        return actionSetName;
                   }
              } catch(e) {
                   // Either the action palette is in button mode, no action is highlighted
                   // or an Action Set is highlighted
                   return null;
              }
    
     
    |
    Mark as:
  • Currently Being Moderated
    May 2, 2012 9:50 AM   in reply to Michael L Hale

     

    Michael L Hale wrote:

     

    This is an example of using Insert-Syntax highlighting-Plain in the advanced editor.

     

     

    Michael,

     

    If I copy and paste the code from your post, it works for me as you describe. If I copy and paste the code from the text editor I use for code editing, which is TextWrangler 4.0 on my Mac Pro running OSX Lion, it pastes in as a table... I'm using Safari 5.1.5 as my browser, and this happens regardless of whether I do a simple 'Paste' or whether I do a 'Paste and Match Style'.

     

    Hmm, just on a hunch I just tried removing the two tab characters preceding each line of my code, and with those gone it pastes just fine without converting it to a table. A bit more testing and sure enough, if I put even one tab in front of the first line, it pastes in as a table...

     

    Apologies to anyone bored by this portion of the conversation, but I felt compelled to respond with the solution in case anyone else runs across this issue and might be searching for an answer.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points