Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Using the UXP code from a Photoshop Action and calling it from an Applescript

Explorer ,
May 11, 2022 May 11, 2022

Hi all,

 

Here's the UXP code taken from the Photoshop Action (enabling debug mode allows you to extract it from a recorded action), it's simply adding a drop shadow to the currently selected layer:

 

async function action() {

    let result;

    let psAction = require(\"photoshop\").action;

    let command = [

        // Set Layer Styles of current layer

        {'_obj':'set','_target':[{'_property':'layerEffects','_ref':'property'},{'_enum':'ordinal','_ref':'layer','_value':'targetEnum'}],'to':{'_obj':'layerEffects','dropShadow':{'_obj':'dropShadow','antiAlias':false,'blur':{'_unit':'pixelsUnit','_value':35.0},'chokeMatte':{'_unit':'pixelsUnit','_value':0.0},'color':{'_obj':'RGBColor','blue':0.0,'grain':0.0,'red':0.0},'distance':{'_unit':'pixelsUnit','_value':0.0},'enabled':true,'layerConceals':true,'localLightingAngle':{'_unit':'angleUnit','_value':90.0},'mode':{'_enum':'blendMode','_value':'multiply'},'noise':{'_unit':'percentUnit','_value':0.0},'opacity':{'_unit':'percentUnit','_value':100.0},'present':true,'showInDialog':true,'transferSpec':{'_obj':'shapeCurveType','name':'Linear'},'useGlobalAngle':true},'scale':{'_unit':'percentUnit','_value':100.0}}}

    ];

    result = await psAction.batchPlay(command, {});

}

async function runModalFunction() {

    await require('photoshop').core.executeAsModal(action, {'commandName': 'Action Commands'});

}

await runModalFunction();

 

As I haven't jumped into the world of UXP coding yet, I'm not sure how to extract the correct information above and place into an Applescript. I was hoping the above could be wrapped into a Javascript, which in turn could be called from an Applescript.

 

The aim is to then apply the same rules to other actions when the focus is on the current layer,

 

Thanks in advance!

TOPICS
Actions and scripting , macOS
2.2K
Translate
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

correct answers 2 Correct answers

LEGEND , May 11, 2022 May 11, 2022

Clean Listener is not bundled with Photoshop, while SL still works.

Look for actions to script converter somewhere in xbytor2's xtools...

Translate
Explorer , May 11, 2022 May 11, 2022

So, after taking your advice I downloaded xbytor2's xtools and it seemed to do the job!

 

• Download the xtools zip from the xtools website

• Extract and locate the 'ActionToJavascript.jsx' file in the xtools/Apps/ folder

• Copy 'ActionToJavascript.jsx' into Adobe Photoshop 2022/Preset/Scripts/ folder

• Restart Photoshop and locate the new 'ActionToJavascript' script via [File] then [Scripts]

• Choose the required Action and where you want to save the translated Javascript file...

• Open the newly crea

...
Translate
Adobe
LEGEND ,
May 11, 2022 May 11, 2022

Ugly Javascript version:

sTT = stringIDToTypeID;

(ref1 = new ActionReference()).putProperty(sTT('property'), sTT('layerEffects'))
ref1.putEnumerated(sTT('layer'), sTT('ordinal'), sTT('targetEnum'));
(dsc1 = new ActionDescriptor()).putReference(sTT('null'), ref1);
(dsc2 = new ActionDescriptor()).putUnitDouble(sTT('scale'), sTT('percentUnit'), 100);
(dsc3 = new ActionDescriptor()).putBoolean(sTT('enabled'), true)
dsc3.putBoolean(sTT('present'), true)
dsc3.putBoolean(sTT('showInDialog'), true)
dsc3.putEnumerated(sTT('mode'), sTT('blendMode'), sTT('multiply'));
(dsc4 = new ActionDescriptor()).putDouble(sTT('red'), 0)
dsc4.putDouble(sTT('grain'), 0)
dsc4.putDouble(sTT('blue'), 0)
dsc3.putObject(sTT('color'), sTT('RGBColor'), dsc4)
dsc3.putUnitDouble(sTT('opacity'), sTT('percentUnit'), 15)
dsc3.putBoolean(sTT('useGlobalAngle'), true)
dsc3.putUnitDouble(sTT('localLightingAngle'), sTT('angleUnit'), 90)
dsc3.putUnitDouble(sTT('distance'), sTT('pixelsUnit'), 0)
dsc3.putUnitDouble(sTT('chokeMatte'), sTT('pixelsUnit'), 0)
dsc3.putUnitDouble(sTT('blur'), sTT('pixelsUnit'), 35)
dsc3.putUnitDouble(sTT('noise'), sTT('percentUnit'), 0)
dsc3.putBoolean(sTT('antiAlias'), false);
(dsc5 = new ActionDescriptor()).putString(sTT('name'), 'Linear')
dsc3.putObject(sTT('transferSpec'), sTT('shapeCurveType'), dsc5)
dsc3.putBoolean(sTT('layerConceals'), true)
dsc2.putObject(sTT('dropShadow'), sTT('dropShadow'), dsc3)
dsc1.putObject(sTT('to'), sTT('layerEffects'), dsc2)
executeAction(sTT('set'), dsc1, DialogModes.NO);
Translate
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
Explorer ,
May 11, 2022 May 11, 2022

Thanks for taking the time out to respond...

I was hoping there would be a way to directly translate the main bit of UXP code into Javascript. This way I could then extract other, more complex functions from Photoshop Actions, which are not supported by the applescript dictionary. Your answer does indeed apply a drop shadow to the current layer, but I'm looking for a much broader answer which could translate to other single step actions within Photsohop.

I have Alchemist installed but it looks like it's spitting out UXP code also... I also tried the Clean Listener script now bundled with Photoshop (as the ScriptListener is no longer supported), but it displays 'No handler found for event' as follows, on every line it returns:

 

// No handler found for event: 3487::invokeCommand

 

Translate
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 ,
May 11, 2022 May 11, 2022

Clean Listener is not bundled with Photoshop, while SL still works.

Look for actions to script converter somewhere in xbytor2's xtools...

Translate
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
Explorer ,
May 11, 2022 May 11, 2022

Thanks for all your help... When I get to the bottom of this I'll be sure to put the answer here! 🙂

Translate
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
Explorer ,
May 11, 2022 May 11, 2022

So, after taking your advice I downloaded xbytor2's xtools and it seemed to do the job!

 

• Download the xtools zip from the xtools website

• Extract and locate the 'ActionToJavascript.jsx' file in the xtools/Apps/ folder

• Copy 'ActionToJavascript.jsx' into Adobe Photoshop 2022/Preset/Scripts/ folder

• Restart Photoshop and locate the new 'ActionToJavascript' script via [File] then [Scripts]

• Choose the required Action and where you want to save the translated Javascript file...

• Open the newly created Javascript file and trigger the Find/Replace function

• Using Find/Replace - Convert all double quotes to single quotes, select all, and copy selection

• Paste the updated javascript into Applescript...

 

 

tell application "Adobe Photoshop 2022"
activate

do javascript "<paste javascript from clipboard here>"

end tell

 

 

This now gives us the ability to store a Photoshop Action (as Javascript) in an Applescript, with the ability to tweak the variables where necessary! Thanks for your help Kukurykus and pointing me in the direction of xbytor2

Translate
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 17, 2024 Feb 17, 2024

Thanks for posting this, I have an answer here on how can I use the script from the xtools 

to create a photoshop panel using UXP ? I'm not a coder but want to learn it with my current project.

Translate
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
Enthusiast ,
Feb 18, 2024 Feb 18, 2024

yesserk4577066 I didn't understand much from what you wrote, from what I read are you creating a uxp panel using xtool?

Translate
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 18, 2024 Feb 18, 2024
LATEST

@yesserk4577066 

 

xtools is for legacy ExtendScript, not for UXP panels or standalone UXP scripts.

 

In later versions of Photoshop one can enable developer mode or use Alchemist to extract batchPlay code for use with UXP.

Translate
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