Skip to main content
Participating Frequently
August 30, 2022
Answered

How do you creating a sub menu of actions to pick from using script

  • August 30, 2022
  • 5 replies
  • 911 views

I wish to have a button in my photoshop actions which when pressed open up a window with buttons to choce further actions from.  I use a lot of actions and this would help me find the right set more easly 

This topic has been closed for replies.
Correct answer jazz-y

try this:

 

var s2t = stringIDToTypeID,
    atn = getActionsList(),
    cur = getActionName();

if (cur && atn[cur]) {
    var cursor = (getXY()),
        w = new Window("dialog", cur);
    w.preferredSize = [200, 200];
    w.spacing = 0;
    w.location = [cursor[0], cursor[1] + 100]
    for (var i = 0; i < atn[cur].length; i++) {
        var b = w.add('button')
        b.text = atn[cur][i];
        b.onClick = function () {
            w.close()
            doAction(this.text, cur)
        }
    }
    w.show();

    function getXY() {
        var w = new Window("dialog {alignChildren: ['fill','fill'], margins: 0}"),
            g = w.add("button"),
            X, Y;
        w.preferredSize = [$.screens[0].right, $.screens[0].bottom]
        g.addEventListener('mouseover', handler)
        function handler(evt) { w.close(); X = evt.screenX, Y = evt.screenY }
        w.show();
        return [X, Y]
    }
}

function getActionsList() {
    var output = {},
        setCounter = 1
    while (true) {
        (r = new ActionReference()).putIndex(s2t('actionSet'), setCounter);
        try { desc = executeActionGet(r) } catch (e) { break; }
        var numberChildren = desc.hasKey(s2t('numberOfChildren')) ? desc.getInteger(s2t('numberOfChildren')) : 0
        if (numberChildren > 0) {
            output[desc.getString(s2t('name'))] = getActionList(setCounter, numberChildren)
        }
        setCounter++
    }
    return output;

    function getActionList(setIndex, numChildren) {
        var current = []
        for (var i = 1; i <= numChildren; i++) {
            (r = new ActionReference()).putIndex(s2t('action'), i);
            r.putIndex(s2t('actionSet'), setIndex)
            current.push(executeActionGet(r).getString(s2t('name')))
        }
        return current
    }
}

function getActionName() {
    var atn;
    try {
        (r = new ActionReference()).putEnumerated(s2t('action'), s2t('ordinal'), s2t('targetEnum'));
        var parent = executeActionGet(r).getString(s2t('parentName')),
            title = executeActionGet(r).getString(s2t('name'));
        try {
            (r = new ActionReference()).putName(s2t('actionSet'), title);
            executeActionGet(r).getInteger(s2t('itemIndex'));
            atn = title;
        } catch (e) {
            (r = new ActionReference()).putName(s2t('actionSet'), parent);
            executeActionGet(r).getInteger(s2t('itemIndex'));
            atn = parent;
        }
    }
    catch (e) { atn = null }
    return atn;
}

 

5 replies

Stephen Marsh
Community Expert
Community Expert
August 31, 2022
Participating Frequently
September 1, 2022

Thank you this looks great, I have over 150 diffrent actions and this will save alot of time. 

Legend
August 31, 2022

Perhaps you will be satisfied with this option: When launched, the script gets the name of the action from which it was launched, then searches for a set with the same name among the loaded ones and displays a window with buttons, each of which launches the action of the specified set. Not perfect, but it works.

 

var s2t = stringIDToTypeID,
    atn = getActionsList(),
    cur = null;

try {
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('name'));
    r.putEnumerated(s2t('action'), s2t('ordinal'), s2t('targetEnum'));
    cur = executeActionGet(r).getString(p);
} catch (e) { }

if (cur && atn[cur]) {
    var cursor = (getXY()),
        w = new Window("dialog", cur);
    w.preferredSize = [200, 200];
    w.spacing = 0;
    w.location = [cursor[0], cursor[1]+100]
    for (var i = 0; i < atn[cur].length; i++) {
        var b = w.add('button')
        b.text = atn[cur][i];
        b.onClick = function () {
            w.close()
            doAction(this.text, cur)
        }
    }
    w.show();

    function getXY() {
        var w = new Window("dialog {alignChildren: ['fill','fill'], margins: 0}"),
            g = w.add("button"),
            X, Y;
        w.preferredSize = [$.screens[0].right, $.screens[0].bottom]
        g.addEventListener('mouseover', handler)
        function handler(evt) { w.close(); X = evt.screenX, Y = evt.screenY }
        w.show();
        return [X, Y]
    }
}

function getActionsList() {
    var output = {},
        setCounter = 1
    while (true) {
        (r = new ActionReference()).putIndex(s2t('actionSet'), setCounter);
        try { desc = executeActionGet(r) } catch (e) { break; }
        var numberChildren = desc.hasKey(s2t('numberOfChildren')) ? desc.getInteger(s2t('numberOfChildren')) : 0
        if (numberChildren > 0) {
            output[desc.getString(s2t('name'))] = getActionList(setCounter, numberChildren)
        }
        setCounter++
    }
    return output;

    function getActionList(setIndex, numChildren) {
        var current = []
        for (var i = 1; i <= numChildren; i++) {
            (r = new ActionReference()).putIndex(s2t('action'), i);
            r.putIndex(s2t('actionSet'), setIndex)
            current.push(executeActionGet(r).getString(s2t('name')))
        }
        return current
    }
}

 

 

Participating Frequently
August 31, 2022

This looks really useful, thank you. I will try it out tomorrow.

 

 

 

 

Bojan Živković11378569
Community Expert
Community Expert
August 31, 2022

Are you aware of button mode in Photoshop? Buttons can be colorized for easier navigation or find.

Stephen Marsh
Community Expert
Community Expert
August 30, 2022

The following extension can find actions by a search filter:

 

https://exchange.adobe.com/creativecloud.details.101656.action-launcher-free.html

 

c.pfaffenbichler
Community Expert
Community Expert
August 30, 2022

Do you have any Photoshop Scripting or general JavaScript experience? 

Participating Frequently
August 31, 2022

I know enough to edit java but not to created it from scratch