Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting
Moderators: Tom , Kukurykus
Megals
Posts: 10 Joined: Wed Jan 18, 2023 2:52 pm
Post
by Megals » Mon Mar 13, 2023 12:47 pm
Hi there,
i try to change the color of an existing rectangle without luck. The hex string is a variable. This is my code so far:
Code: Select all
var fillColor = new SolidColor();
fillColor.rgb.hexValue = hex.substring(1);
layerBaseColor.fillPath( fillColor );
fillColor.red = parseInt(hex.substring(1, 3), 16);
fillColor.green = parseInt(hex.substring(3, 5), 16);
fillColor.blue = parseInt(hex.substring(5, 7), 16);
myLayer.fillPath( fillColor );
Best regards
Kukurykus
Posts: 531 Joined: Mon Jul 25, 2016 12:36 pm
Post
by Kukurykus » Mon Mar 13, 2023 2:07 pm
Hard to guess what you want, specifically when you use not working variables:
Code: Select all
(sc = new SolidColor())
.rgb.hexValue = '808080'
activeDocument.selection.fill(sc)
Megals
Posts: 10 Joined: Wed Jan 18, 2023 2:52 pm
Post
by Megals » Tue Mar 14, 2023 9:16 am
Thank you for your reply.
I want to change the color of an existing rectangle myLayer, by a hex string e.g. #FF0000.
Best regards
Kukurykus
Posts: 531 Joined: Mon Jul 25, 2016 12:36 pm
Post
by Kukurykus » Tue Mar 14, 2023 10:50 am
Code: Select all
(function(v) {
sTT = stringIDToTypeID;
(sc = new SolidColor()).rgb.hexValue = v;
(ref = new ActionReference()).putEnumerated
(sTT('contentLayer'), sTT('ordinal'), sTT('targetEnum'));
(dsc1 = new ActionDescriptor()).putReference(sTT('null'), ref)
dsc4 = new ActionDescriptor(), rgb = [].slice.call(sc.rgb.reflect.properties)
.splice(0, 3); while(shft = rgb.shift()) dsc4.putDouble(sTT(shft), sc.rgb[shft]);
(dsc3 = new ActionDescriptor()).putObject(sTT('color'), sTT('RGBColor'), dsc4);
(dsc2 = new ActionDescriptor()).putObject(sTT('fillContents'), sTT('solidColorLayer'), dsc3);
(dsc5 = new ActionDescriptor()).putBoolean(sTT('fillEnabled'), true), dsc2.putObject(sTT('strokeStyle'),
sTT('strokeStyle'), dsc5), dsc1.putObject(sTT('to'), sTT('shapeStyle'), dsc2), executeAction(sTT('set'), dsc1)
})('FF0000')
Megals
Posts: 10 Joined: Wed Jan 18, 2023 2:52 pm
Post
by Megals » Tue Mar 14, 2023 3:07 pm
Hi,
thank you for the code, but It is not working for me. This is what i did:
- New File, added a rectangle.
- Put the code into a jsx file and drag and dropped it.
- Added a alert to the code to make sure it gets executed.
- i made sure the rectangle layer is active
- Photoshop 2022
Kukurykus
Posts: 531 Joined: Mon Jul 25, 2016 12:36 pm
Post
by Kukurykus » Tue Mar 14, 2023 6:38 pm
Zip your psd and attach to post.
Megals
Posts: 10 Joined: Wed Jan 18, 2023 2:52 pm
Post
by Megals » Tue Mar 14, 2023 10:07 pm
attached it
Attachments
rectangle_color.zip
(7.35 KiB) Downloaded 720 times
Kukurykus
Posts: 531 Joined: Mon Jul 25, 2016 12:36 pm
Post
by Kukurykus » Wed Mar 15, 2023 6:55 am
What is version of your application (which you can check from 'Help' menu > 'About Photoshop')? If that is not the latest you have to change:
to:
Megals
Posts: 10 Joined: Wed Jan 18, 2023 2:52 pm
Post
by Megals » Wed Mar 15, 2023 2:28 pm
Now it is working! Thank you so much!