Skip to main content
Flowgun
Inspiring
September 17, 2025
Answered

Is there a language-agnostic way to select "Foreground to Transparent" gradient?

  • September 17, 2025
  • 1 reply
  • 327 views

Hello everyone,
this code retrieved with xtools from a saved action selects the gradient:

function Action1() {
  // Select
  function step1(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putName(cTID('Grdn'), "Foreground to Transparent");
    desc1.putReference(cTID('null'), ref1);
    executeAction(cTID('slct'), desc1, dialogMode);
  };

  step1();      // Select
};

but it uses the name to select the "foreground to Transparent" gradient, which I assume would be different if Photoshop is installed with another language. 

Is there a reliable way to select it?

Correct answer c.pfaffenbichler

I'm trying to change the settings of the gradient tool. for now, I set up a hotkey that gives me the gradient tool with the foreground to background gradient, but I also want to press it again to toggle to foreground to transparent.


Does this work? 

// 2025, use it at your own risk;
try {
var colorA = app.foregroundColor;
var theName = String(Math.random());
// =======================================================
    var desc232 = new ActionDescriptor();
        var ref9 = new ActionReference();
        ref9.putClass( stringIDToTypeID( "gradientClassEvent" ) );
    desc232.putReference( stringIDToTypeID( "null" ), ref9 );
        var desc233 = new ActionDescriptor();
            var desc234 = new ActionDescriptor();
            desc234.putString( stringIDToTypeID( "name" ), theName );
            desc234.putEnumerated( stringIDToTypeID( "gradientForm" ), stringIDToTypeID( "gradientForm" ), stringIDToTypeID( "customStops" ) );
            var idinterfaceIconFrameDimmed = stringIDToTypeID( "interfaceIconFrameDimmed" );
            desc234.putDouble( idinterfaceIconFrameDimmed, 4096.000000 );
                var list4 = new ActionList();
                    var desc235 = new ActionDescriptor();
                        var desc22 = new ActionDescriptor();
                        desc22.putDouble( stringIDToTypeID( "red" ), colorA.rgb.red );
                        desc22.putDouble( stringIDToTypeID( "grain" ), colorA.rgb.green );
                        desc22.putDouble( stringIDToTypeID( "blue" ), colorA.rgb.blue );
                    desc235.putObject( stringIDToTypeID( "color" ), stringIDToTypeID( "RGBColor" ), desc22 );
                    desc235.putEnumerated( stringIDToTypeID( "type" ), stringIDToTypeID( "colorStopType" ), stringIDToTypeID( "userStop" ) );
                    desc235.putInteger( stringIDToTypeID( "location" ), 0 );
                    desc235.putInteger( stringIDToTypeID( "midpoint" ), 50 );
                list4.putObject( stringIDToTypeID( "colorStop" ), desc235 );
                    var desc237 = new ActionDescriptor();
                    desc237.putObject( stringIDToTypeID( "color" ), stringIDToTypeID( "RGBColor" ), desc22 );
                    desc237.putEnumerated( stringIDToTypeID( "type" ), stringIDToTypeID( "colorStopType" ), stringIDToTypeID( "userStop" ) );
                    desc237.putInteger( stringIDToTypeID( "location" ), 3798 );
                    var idmidpoint = stringIDToTypeID( "midpoint" );
                    desc237.putInteger( idmidpoint, 50 );
                list4.putObject( stringIDToTypeID( "colorStop" ), desc237 );
            desc234.putList( stringIDToTypeID( "colors" ), list4 );
                var list5 = new ActionList();
                    var desc239 = new ActionDescriptor();
                    desc239.putUnitDouble( stringIDToTypeID( "opacity" ), stringIDToTypeID( "percentUnit" ), 100.000000 );
                    desc239.putInteger( stringIDToTypeID( "location" ), 0 );
                    desc239.putInteger( stringIDToTypeID( "midpoint" ), 50 );
                list5.putObject( stringIDToTypeID( "transferSpec" ), desc239 );
                    var desc240 = new ActionDescriptor();
                    desc240.putUnitDouble( stringIDToTypeID( "opacity" ), stringIDToTypeID( "percentUnit" ), 0.000000 );
                    desc240.putInteger( stringIDToTypeID( "location" ), 4096 );
                    desc240.putInteger( stringIDToTypeID( "midpoint" ), 50 );
                list5.putObject( stringIDToTypeID( "transferSpec" ), desc240 );
            desc234.putList( stringIDToTypeID( "transparency" ), list5 );
        desc233.putObject( stringIDToTypeID( "gradient" ), stringIDToTypeID( "gradientClassEvent" ), desc234 );
    desc232.putObject( stringIDToTypeID( "using" ), stringIDToTypeID( "gradientClassEvent" ), desc233 );
executeAction( stringIDToTypeID( "make" ), desc232, DialogModes.NO );
// =======================================================
    var desc241 = new ActionDescriptor();
        var ref10 = new ActionReference();
        ref10.putName( stringIDToTypeID( "gradientClassEvent" ), theName );
    desc241.putReference( stringIDToTypeID( "null" ), ref10 );
executeAction( stringIDToTypeID( "select" ), desc241, DialogModes.NO );
// =======================================================
    var desc242 = new ActionDescriptor();
        var ref11 = new ActionReference();
        ref11.putName(stringIDToTypeID( "gradientClassEvent" ), theName);
    desc242.putReference( stringIDToTypeID( "null" ), ref11 );
executeAction( stringIDToTypeID( "delete" ), desc242, DialogModes.NO );
} catch (e) {};

1 reply

Stephen Marsh
Community Expert
Community Expert
September 17, 2025

It looks like it is applying the gradient preset name. Forgetting regionalisation issues, even within English installations this could fail if the preset isn't loaded, even if spelt correctly.

 

I'll need to take a look later, perhaps either prompting to load the preset or creating it from scratch?

c.pfaffenbichler
Community Expert
Community Expert
September 19, 2025
quote

this could fail if the preset isn't loaded,

Good point! 

I suppose »simply« evaluating the fore- and background colors and applying a custom gradient might be the most »convenient« route. 

Flowgun
FlowgunAuthor
Inspiring
September 19, 2025

but can the background color be set to transparent??
I already have some code that sets the gradient to the foreground and background colors, but it does it simply by resetting the tool.