Javascript and the Photoshop 'crop and straighten' command

DavidHoffman

New Member
Joined
Sep 12, 2018
Messages
5
Lightroom Experience
Beginner
Lightroom Version
Lightroom Version
Adobe Lightroom Classic version: 8.4.1
Operating System
macOS 10.12 Sierra
Can anyone help me with a bit of Javascript? I'm trimming big batches of 16 bpc layered tiff files in Photoshop. I've found a useful script by Jeffrey Tranberry <An photoshop batch script for crop & straighten photo from Jeffrey Tranberry> to batch Photoshop's ‘crop and straighten’ command but it only works on flat files or where the background layer is already selected and I know next to nothing of Javascript.

The background layer of my layered, 16 bit/channel tiff files is not selected when the file is opened so the ‘crop and straighten’ command fails. I could run a separate action on the batch to leave the background selected before I run the script but I'd rather add a step in the script to select the background layer before the script calls the crop and straighten command.

I can make the script work with this ‘flatten’ step:
app.activeDocument.flatten();
but I want to retain the layers when I save the final tiff so a 'flatten' step doesn't help.

I need to write a line to select the background layer in P'shop that I could use in place of the ‘flatten’ step above. The total number of layers will vary but the background will always be named 'background' and be at the bottom of the layer stack. How would I write that?

The script saves as jpg but I’d like to save as 16 bpc layered tiffs. That part of the script is:
//Save as a JPEG to the outputFolder
var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = 10;
jpegOptions.embedColorProfile = false;
app.activeDocument.saveAs( File( outputFolder + "/" + activeDocument.name + ".jpg"), jpegOptions, false);
How would I rewrite that to save as 16 bpc layered tiffs?

Thanks in advance for any suggestions!

David Hoffman
 
Joined
Oct 8, 2007
Messages
5,190
Location
London
Lightroom Experience
Power User
Have you downloaded the Photoshop scripting guide, David? I've done a bit of JS scripting with PS and would always go straight to the JS reference manual for detail, so I can see that TiffSaveOptions would replace the JPEG stuff.

John
 

DavidHoffman

New Member
Joined
Sep 12, 2018
Messages
5
Lightroom Experience
Beginner
Lightroom Version
Have you downloaded the Photoshop scripting guide, David? I've done a bit of JS scripting with PS and would always go straight to the JS reference manual for detail, so I can see that TiffSaveOptions would replace the JPEG stuff.
Thanks for that link John, it's definitely got the answer somewhere in it. I can see terms like 'activeLayer' and 'backgroundLayer' which are clearly part of my solution. I don't know the 'grammar' of Javascript so I can't yet see how to use those terms in a script line that says 'make backgroundLayer the activeLayer' but I'll read through some scripts and hopefully find the correct form.
 
Joined
Oct 8, 2007
Messages
5,190
Location
London
Lightroom Experience
Power User
There used to be lots of sample scripts in Adobe's download, David.
 

DavidHoffman

New Member
Joined
Sep 12, 2018
Messages
5
Lightroom Experience
Beginner
Lightroom Version
Hi John
I've found a few sites with sample scripts though I'm not sure if any are the Adobe site you mean. They take a lot of time to work through and read, particularly as I'm not sure exactly what I'm looking for but I've found a few clues.

The existing line that I want to replace is:
app.activeDocument.flatten();
which works fine but isn't what I want.
I need to change it to a line that instead selects the background layer. From the scripts I've seen I've tried variations of this:
docRef.activeLayer = docRef.layers["Background”];
That line seems to work when I run it on its own in a minimal test script but when I paste it into the script get an "Unterminated string constant" error. I thought that would be a missing quote mark but that doesn't seem to be the case.
The var docRef is defined earlier so I don't think that's the problem.

So currently I need to find what may be wrong with the new line or find an alternative. And while I'm at I also need to write a line to change a file from 16 bpc to 8 bpc. A little progress but a way to go yet.
 
Joined
Sep 29, 2007
Messages
22,100
Location
Southampton, UK
Lightroom Experience
Power User
Lightroom Version
Cloud Service
I haven't done any JS for years, but I couldn't help but notice a curly quote in your pasted line. Could be it?
 

DavidHoffman

New Member
Joined
Sep 12, 2018
Messages
5
Lightroom Experience
Beginner
Lightroom Version
I haven't done any JS for years, but I couldn't help but notice a curly quote in your pasted line. Could be it?
Well spotted! Thanks. My Mac does this, annoys Excel too, and I should really have learned to spot them by now. I'll test to see if that's the problem this evening and report back.
 

DavidHoffman

New Member
Joined
Sep 12, 2018
Messages
5
Lightroom Experience
Beginner
Lightroom Version
Many thanks Victoria - the curly quote was indeed the problem and the script is working nicely now. Very nice to get 100 massive files cropped while I make a coffee!
 
Top