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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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

Very much appreciated with you answers to my questions. I want to focus on your comment on if (docRef.height .gt. docRef.width)

I totally agree with your statement however during my debugging, I found that if you change .gt. to .lt. It is always true and will execute the (commans)which puzzled me for a long time. At the same time, I also used arrays to store my size parameters and thought this was causing issues so I deleted the arrays and used the current method.

Please note I am on my iPad and it does have the curly brackets

In summary

Docref.height .gt. docref.width). ( instruction)

Docref.height .lt. Docref.width). ( instruction)

Both lines will execute the ( instruction)

I scratched my head on this one for a very long time even thought I know that the values were different so my work around was the current method. I am suspecting that if 0-false and true for all negative and positive values. Haven't tested this out yet. This is against all my understand of how it's suppose to work. 0-all zero or neg values and true for all positive values

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

If you have a image that is 3000 pixels and you want to print it to 3 inches, and you print it on a printer that does 300 dpi, you will get a 10 inch print. Unless you resample and reduce the image size to 900 pixels. I rather do it on my computer then have the pront shop do it for me

What I am saying is that I want to match my pixels to the printers capability and not have any resampling/conversation outside my process

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.

I've seen these discussions get side tracked all the time. My initial inquire was suggestions on how to get action name that starts the code. I thank all for their comments. I got what I need. End of story. This discussions has end

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?

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

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.

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.

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:

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;
}

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.

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

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;
}

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.