Setting Labels and Ratings from JavaScript

New Here ,
Feb 14, 2021 Feb 14, 2021

Copy link to clipboard

Copied

I'm wanting to write a couple of JavaScripts to automate my save process. Here is what I would like to do:

For the open document, set the label to "Approved" and SaveAs with a new name

Run an Action to resize to 2048 on the long side, add a watermark. ** This is working through normal actions **

Next, I have to decide if I like the default watermark placement or not, so I deselect it, move it if needed then flatten the image. This is pretty quick if I don't need to move it (CMD-D CMD-E)

Now is where the fun comes in.

Next, I want to change the Label to "Second" then I want to rename it to preferably "filename_web.jpg". I actually have this working in a JavaScript script but it gets renamed to "web_filename.jpg" and I can work with it. However, it's not changing either the rating or the label.

Finally, I have another Action that makes an Instagram version (1080 wide, 1350 high, pads verticals if the aspect ratio isn't 4x5) which is working quite well, but I would like to add:

change the label to "Review" and then save as "ig_filename.jpg" (well I'd like filename_ig.jpg, but...)

 

Anyway, I've got the renaming functional, but it's not changing the label. I don't really care about setting the rating since I've typically done that in Bridge when selecting the photos to post-process. Here is my script:

 

var docRef = app.activeDocument
app.activeDocument.info.label = "Second"
app.activeDocument.info.Label = "Second"
app.activeDocument.info.rating = 3
app.activeDocument.info.Rating = 4

var fileName = "web_" + docRef.name
jpgFile = new File( docRef.path + "/" + fileName )
//jpgFile.info.label = "Second"
jpgSaveOptions = new JPEGSaveOptions()
jpgSaveOptions.embedColorProfile = true
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE
jpgSaveOptions.matte = MatteType.NONE
jpgSaveOptions.quality = 10
app.activeDocument.saveAs(jpgFile, jpgSaveOptions, true,Extension.LOWERCASE)

This should work according to the couple of PDFs that I've found. The documents don't list Lable and Rating as available, but they show up in the full dump of the XMP data.

 

I'm trying both capitalized and lowercase versions of the objects since the XMP data shows it capitalized while the documentation shows similar fields as lower case.

 

Surely there has to be a way to set the Label from within Photoshop from a scriptable means.

 

Thanks

 

TOPICS
Actions and scripting

Views

221

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 14, 2021 Feb 14, 2021

Copy link to clipboard

Copied

if (!ExternalObject.AdobeXMPScript)
	ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
(metaData = new XMPMeta()).setProperty(XMPConst.NS_XMP,'Label', 'green')
activeDocument.xmpMetadata.rawData = metaData.serialize()

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 14, 2021 Feb 14, 2021

Copy link to clipboard

Copied

Nice one!

 

Adding the label colour 'green' results in a white/unrecognised label in Bridge, I have to use the label description 'Approved' and not the colour (this presumes that the standard labels are used in Bridge).

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 14, 2021 Feb 14, 2021

Copy link to clipboard

Copied

Different Bridge releases (incl. language versions) use other label names. Mine has color names.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 14, 2021 Feb 14, 2021

Copy link to clipboard

Copied

Ah, that must be it, as I just checked in Bridge 2018, 2019, 2020 & 2021 all use the name/description and not the colour in English installations (not sure if GB or US localisation).

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 14, 2021 Feb 14, 2021

Copy link to clipboard

Copied

This worked great. Thank you very much.

As noted, the text for the Label has to match the string Bridge uses or you get white. Also, I had to explicitly set the rating as well. It did not preserve the rating in the open document. But for now this is working like a charm.

 

I didn't change my action that resizes to 2048 and applies the watermark, but changed my IG action to save the web version, do the resizing for IG and then save the IG version. If I don't have to move the watermark, after the image opens from Adobe Camera Raw, it's:

 

CMD-A
Run the Watermark/Resize Script

CMD-D

CMD-E

Run the Make web versions Script

 

That's going to be a huge improvement to my workflow.

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 14, 2021 Feb 14, 2021

Copy link to clipboard

Copied

To preserve ratings:

if (documents.length &&
(aD = activeDocument).saved) {
	fle = aD.fullName; if (!ExternalObject.AdobeXMPScript)
		ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript')
	xmp = new XMPFile(fle.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE);
	(metaData = xmp.getXMP()).setProperty(XMPConst.NS_XMP,'Label', 'green')
	xmp.putXMP(metaData), xmp.closeFile(XMPConst.CLOSE_UPDATE_SAFELY)
}

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 08, 2022 Sep 08, 2022

Copy link to clipboard

Copied

Hello, I would like to know if we could Label using this code without having to save the document? 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 08, 2022 Sep 08, 2022

Copy link to clipboard

Copied

If I understand correctly (and having been a programmer for over 40 years, I'm pretty sure I am), you are not opening the image file, you're opening or creating the .xml sidecar file. The new XMPFile() has a flag saying to open an existing sidecar file in update mode. I would assume that if that XMP sidecar doesn't exist, it will create one. That is a very normal behavior for opening a file in either write or update mode.

So when xmp.closeFile() is called on the xmp object, it will update the file and close it or save it if it didn't exist.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 08, 2022 Sep 08, 2022

Copy link to clipboard

Copied

thank you so much for fast respond. By all means, I am not a programmer, I have a code below that was created by former collegue and I would like to know if I could label without having to save the document.

I tried copy paste above code but Bridge still not showing the label.

  if (addLabel.value == true) {
    if (ExternalObject.AdobeXMPScript == undefined)
      ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
    var xmp = new XMPMeta(activeDocument.xmpMetadata.rawData);
    xmp.deleteProperty(XMPConst.NS_XMP, "Label");
    xmp.setProperty(XMPConst.NS_XMP, "Label", labelList.selection.text);
    app.activeDocument.xmpMetadata.rawData = xmp.serialize();
    app.activeDocument.save();
  }

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 08, 2022 Sep 08, 2022

Copy link to clipboard

Copied

Code from @SuperMerlin 

 

This script applies to an open/active document, the modified document requires saving, it isn't writing back the metadata without saving the file.

 

It is possible to modify metadata without having to resave the file, it just requires different code.

 

/*
https://community.adobe.com/t5/photoshop-ecosystem-discussions/ps-script-to-edit-bridge-labels/m-p/8981910
*/

#target photoshop;

app.bringToFront();

if (documents.length) main();

function main() {
    var win = new Window("dialog", "Label");
    g = win.graphics;
    var myBrush = g.newBrush(g.BrushType.SOLID_COLOR, [0.2, 0.2, 0.2, 1]);
    g.backgroundColor = myBrush;
    win.p1 = win.add("panel", undefined, undefined, {
        borderStyle: "black"
    });
    win.g1 = win.p1.add('group');
    win.g1.rb1 = win.g1.add("radiobutton", undefined, "Select");
    win.g1.rb1.value = true;
    rb1 = win.g1.rb1.graphics;
    rb1.font = ScriptUI.newFont("Georgia", "BOLDITALIC", 22);
    rb1.foregroundColor = rb1.newPen(rb1.PenType.SOLID_COLOR, [1.00, 0.00, 0.00, 1], lineWidth = 1);
    rb2 = win.g1.rb1.graphics;
    win.g1.rb2 = win.g1.add("radiobutton", undefined, "Second");
    rb2 = win.g1.rb2.graphics;
    rb2.font = ScriptUI.newFont("Georgia", "BOLDITALIC", 22);
    rb2.foregroundColor = rb2.newPen(rb2.PenType.SOLID_COLOR, [1.00, 0.80, 0.00, 1], lineWidth = 1);
    win.g1.rb3 = win.g1.add("radiobutton", undefined, "Approved");
    rb3 = win.g1.rb3.graphics;
    rb3.font = ScriptUI.newFont("Georgia", "BOLDITALIC", 22);
    rb3.foregroundColor = rb3.newPen(rb3.PenType.SOLID_COLOR, [0.00, 1.00, 0.00, 1], lineWidth = 1);
    win.g1.rb4 = win.g1.add("radiobutton", undefined, "Review");
    rb4 = win.g1.rb4.graphics;
    rb4.font = ScriptUI.newFont("Georgia", "BOLDITALIC", 22);
    rb4.foregroundColor = rb4.newPen(rb4.PenType.SOLID_COLOR, [0.00, 0.00, 1.00, 1], lineWidth = 1);
    win.g1.rb5 = win.g1.add("radiobutton", undefined, "To Do");
    rb5 = win.g1.rb5.graphics;
    rb5.font = ScriptUI.newFont("Georgia", "BOLDITALIC", 22);
    rb5.foregroundColor = rb5.newPen(rb5.PenType.SOLID_COLOR, [1.00, 0.00, 1.00, 1], lineWidth = 1);
    win.g2 = win.p1.add('group');
    win.g2.bu1 = win.g2.add("button", undefined, "Label");
    win.g2.bu1.preferredSize = [200, 40];
    win.g2.bu2 = win.g2.add("button", undefined, "Cancel");
    win.g2.bu2.preferredSize = [200, 40];
    win.g2.bu1.onClick = function () {
        win.close(0);
        if (win.g1.rb1.value) label = "Select";
        if (win.g1.rb2.value) label = "Second";
        if (win.g1.rb3.value) label = "Approved";
        if (win.g1.rb4.value) label = "Review";
        if (win.g1.rb5.value) label = "To Do";
        if (ExternalObject.AdobeXMPScript == undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
        var xmp = new XMPMeta(activeDocument.xmpMetadata.rawData);
        xmp.deleteProperty(XMPConst.NS_XMP, "Label");
        xmp.setProperty(XMPConst.NS_XMP, "Label", label);
        app.activeDocument.xmpMetadata.rawData = xmp.serialize();
    }
    win.show();
};

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 09, 2022 Sep 09, 2022

Copy link to clipboard

Copied

LATEST

An XMP sidecar is only needed for RAW files that cannot be modified. Once you are in Photoshop, a RAW file has been rendered and must be saved as a raster format which can store ratings and labels internally.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines