Copy link to clipboard
Copied
Hi
I have a folder with hundreds of images...some in vertical (portrait) and some in horizontal orientation (landscape). I want to standardize all to vertical !!!
So after opening manually the image (or by using the Batch command) I´d need a script that does it:
1) Look at the current active image and if the image is in horizontal orientation (landscape) then rotate it 90º to become vertical (portrait)
2) If the image is already vertical then do not do anything. Leave as it.
End.
It´s just a script to identify which images are horizontal and rotate it.
The process of opening images and saving I can do manually or by using the batch Photoshop command (so I attach the script with an normal action). These taks do not need to be in the script.
Could anyone help me to write this script?
Thank you a lot
Gustavo.
// 2011, use at your own risk;
#target photoshop
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
if (myDocument.width > myDocument.height) {
myDocument.rotateCanvas(90)
};
};
Copy link to clipboard
Copied
I would like have a script that works when the image is printed in Photoshop and roatates the landscape images 90 degrees before printing and does nothing to the portrait images.
#target photoshop
app.bringToFront();
if (activeDocument.layers.length === 1 && /\.(jpg|jpeg)$/i.test(activeDocument.name)) {
var doc = activeDocument;
if (doc.width.value > doc.height.value) {
activeDocument.rotateCanvas(90);
}
}
Copy link to clipboard
Copied
Doesn't it rotate only landscape images?
Copy link to clipboard
Copied
@otsphotos wrote:
Thank you for finding my mistake, but the script still doesn't work with
the added
if (/jpg$/i.test(activeDocument.name))
You could also use .match() instead of .test() – which are both essentially doing the same thing in different ways:
Edit: Code corrected/updated –
if (activeDocument.name.match(/\.jpg|\.jpeg$/i)) {
var doc = activeDocument;
if (doc.width.value > doc.height.value) { //Landscape
doAction("Landscape", "OTS Photos");
} else { //Portrait
doAction("Portrait", "OTS Photos");
}
}
Copy link to clipboard
Copied
.test returns boolean while .match an array.
Copy link to clipboard
Copied
This is true... However, In the context of an if conditional check on the filename – it would appear to me that .match() and .test() are interchangeable.
I didn't state that they were exactly the same, just that they were essentially doing the same thing in different ways. This is what your post and the following pages also indicate:
https://www.w3schools.com/jsref/jsref_regexp_test.asp
https://www.w3schools.com/jsref/jsref_match.asp
Am I missing something?
Copy link to clipboard
Copied
If you don't do anything with matched items then .test should be used as faster method.
Copy link to clipboard
Copied
@Kukurykus wrote:
If you don't do anything with matched items then .test should be used as faster method.
You are of course correct that one is faster than the other, not that a human would ever notice the difference in this use case:
if (activeDocument.name.match(/\.jpg|\.jpeg$/i)) {
// Run Time: 0.003 seconds
}
if (/\.jpg|\.jpeg$/i.test(activeDocument.name)) {
// Run Time: 0.001 seconds
}
As the OP was having problems with the .test() method, I suggested .match() as a viable alternative to try, just in case there was a different result (there shouldn't be though).
Copy link to clipboard
Copied
OP did not have problems with .test method.
Copy link to clipboard
Copied
@Kukurykus wrote:
OP did not have problems with .test method.
That's not what I'm reading:
@otsphotos wrote:
Thank you for finding my mistake, but the script still doesn't work with
the added
if (/jpg$/i.test(activeDocument.name))
To be clear, I can't reproduce this issue. Both .test() and .match() produce the same correct result in my tests. The script processes only files with a .jpg or .jpeg extension, ignoring others such as .psd
Copy link to clipboard
Copied
Replacing test to match can't help. That suggestion was redundant. User simply does some other thing wrong we don't see or refers to ScriptEventsManager functionality, where maybe old instance of script still is enabled.
Copy link to clipboard
Copied
Your script triggered by Events Script Manager can't be run on psds. It doesn't fulfil condition.
Copy link to clipboard
Copied
If that works then yes. Next time when posting codes use </> icon.
Copy link to clipboard
Copied
If the script manager can't determine between file extensions, the other thing that is unquie between the files, is the first script runs an action that places a layer on the image. Maybe there's a script that won't run the action if there's already a layer on the image.
Copy link to clipboard
Copied
I don't understand what other first script, and what script manager you are talking about now.
Copy link to clipboard
Copied
@otsphotos wrote:
Maybe there's a script that won't run the action if there's already a layer on the image.
Original post edited: If you wanted to test that the active layer was a Background layer and that the doc only had one layer (which meets the criteria for a JPEG without explicitly testing for the filename extension, so is not foolproof):
#target photoshop
app.bringToFront();
if (activeDocument.activeLayer.isBackgroundLayer && activeDocument.layers.length === 1) {
var doc = activeDocument;
if (doc.width.value > doc.height.value) {
doAction("Landscape", "OTS Photos");
} else {
doAction("Portrait", "OTS Photos");
}
}
Note: I have removed the original second example as although technically working, it was testing for a redundant impossible condition.
Copy link to clipboard
Copied
@otsphotos wrote:
If the script manager can't determine between file extensions, the other thing that is unquie between the files, is the first script runs an action that places a layer on the image. Maybe there's a script that won't run the action if there's already a layer on the image.
Just to be clear on your wording and perhaps understanding of how things work. The Script Events Manager simply runs a script or action if an Event is triggered, it has no built-in logic.
It is up to the conditional or try/catch code used in the triggered script to determine the file extension or other required criteria.
Copy link to clipboard
Copied
I need to do something similar, but for all images in a directory. I just want to either renal the image with 'portrait or landscape' in the title (p or l would do or better still save the image with it's dimentions in the name to a directory for landscape and a directory for portrait.
Any pointers would be very helpful as I've never written a script for Photoshop
Copy link to clipboard
Copied
@pete24761500hl6l – There are different methods, however, I think that the easiest way is to use Adobe Bridge's Filter panel > Orientation. Select untick landscape and or square and only have portrait ticked, then select all and use Tools > Batch Rename. Then repeat for landscape and or square.
Another method would be ExifTool, for me, this is much faster than creating a script for Photoshop. The following is a conditional to rename to Portrait, Landscape or Square:
exiftool -if '$imagewidth > $imageheight' '-filename=%f_Landscape.%e' -execute -if '$imagewidth < $imageheight' '-filename=%f_Portrait.%e' -execute -if '$imagewidth == $imageheight' '-filename=%f_Square.%e' -common_args 'mac os system path/to/files or folder'
This example is for the Mac, Windows would swap the single straight quotes ' for double straight quotes " and obviously use a valid Windows path to the files or directory.
You can also use Bridge's Batch Rename to rename with the width and height metadata:
Copy link to clipboard
Copied
@pete24761500hl6l – Oops did you mean title as in metadata, for some reason I thought filename when I read title?!
Here is the Bridge method using the filter panel and the Metadata panel for all selected/filtered files:
And here is the ExifTool code:
exiftool -if '$imagewidth > $imageheight' -XMP-dc:Title='Landscape' -IPTC:ObjectName='Landscape' -execute -if '$imagewidth < $imageheight' -XMP-dc:Title='Portrait' -IPTC:ObjectName='Portrait' -execute -if '$imagewidth == $imageheight' -XMP-dc:Title='Square' -IPTC:ObjectName='Square' -common_args -overwrite_original 'mac os system path/to/files or folder'
Copy link to clipboard
Copied
The following ExifTool code will move files to new folders inside the parent folder based on their orientation (portrait, landscape or square):
exiftool -if "$imagewidth > $imageheight" -directory=%d"Landscape Images" -execute -if "$imagewidth < $imageheight" -directory=%d"Portrait Images" -execute -if "$imagewidth == $imageheight" -directory=%d"Square Images" -common_args "C:\Users\username\Desktop\my folder full of images"
This example is for Windows, for the Mac, simply swap the double straight quote marks " for single straight quotes ' and use the Mac path to the root folder containing the mix of images.
___________
Here is a script from @SuperMerlin for Adobe Bridge to do the same: