Highlighted

Can this script modify the path of action storage?

Explorer ,
Sep 04, 2020

Copy link to clipboard

Copied

The role of this script is to perform an action many times.
I often use to batch modify the size of many pictures, but every time I change the path, it will default to the last storage location.
If you want to save to the current, you must first open the graph in the current path and double-click to run the "save" behavior in the action, which is very troublesome.
Is there a master who can help realize:
Let the modified picture be opened and saved in the current path by default.
Thank you very much~

#target photoshop
app.bringToFront();
// 重复执行N遍选中的动作
/////////////////////////////////////////////////////////////////////
// Function: GlobalVariables
// Usage: global action items that are reused
// Input: <none>
// Return: <none>
/////////////////////////////////////////////////////////////////////
function GlobalVariables() {
    gClassActionSet = charIDToTypeID( 'ASet' );
    gClassAction = charIDToTypeID( 'Actn' );
    gKeyName = charIDToTypeID( 'Nm  ' );
    gKeyNumberOfChildren = charIDToTypeID( 'NmbC' );
}
/////////////////////////////////////////////////////////////////////
// Function: GetActionSetInfo
// Usage: walk all the items in the action palette and record the action set
//        names and all the action children
// Input: <none>
// Return: the array of all the ActionData
// Note: This will throw an error during a normal execution. There is a bug
// in Photoshop that makes it impossible to get an acurate count of the number
// of action sets.
/////////////////////////////////////////////////////////////////////
function GetActionSetInfo() {
    var actionSetInfo = new Array();
    var setCounter = 1;
      while ( true ) {
        var ref = new ActionReference();
        ref.putIndex( gClassActionSet, setCounter );
        var desc = undefined;
        try { desc = executeActionGet( ref ); }
        catch( e ) { break; }
        var actionData = new ActionData();
        if ( desc.hasKey( gKeyName ) ) {
            actionData.name = desc.getString( gKeyName );
        }
        var numberChildren = 0;
        if ( desc.hasKey( gKeyNumberOfChildren ) ) {
            numberChildren = desc.getInteger( gKeyNumberOfChildren );
        }
        if ( numberChildren ) {
            actionData.children = GetActionInfo( setCounter, numberChildren );
            actionSetInfo.push( actionData );
        }
        setCounter++;
    }
    return actionSetInfo;
}
/////////////////////////////////////////////////////////////////////
// Function: GetActionInfo
// Usage: used when walking through all the actions in the action set
// Input: action set index, number of actions in this action set
// Return: true or false, true if file or folder is to be displayed
/////////////////////////////////////////////////////////////////////
function GetActionInfo( setIndex, numChildren ) {
    var actionInfo = new Array();
    for ( var i = 1; i <= numChildren; i++ ) {
        var ref = new ActionReference();
        ref.putIndex( gClassAction, i );
        ref.putIndex( gClassActionSet, setIndex );
        var desc = undefined;
        desc = executeActionGet( ref );
        var actionData = new ActionData();
        if ( desc.hasKey( gKeyName ) ) {
            actionData.name = desc.getString( gKeyName );
        }
        var numberChildren = 0;
        if ( desc.hasKey( gKeyNumberOfChildren ) ) {
            numberChildren = desc.getInteger( gKeyNumberOfChildren );
        }
        actionInfo.push( actionData );
    }
    return actionInfo;
}
/////////////////////////////////////////////////////////////////////
// Function: ActionData
// Usage: this could be an action set or an action
// Input: <none>
// Return: a new Object of ActionData
/////////////////////////////////////////////////////////////////////
function ActionData() {
    this.name = "";
    this.children = undefined;
    this.toString = function () {
        var strTemp = this.name;
        if ( undefined != this.children ) {
            for ( var i = 0; i < this.children.length; i++ ) {
                strTemp += " " + this.children[i].toString();
            }
        }
        return strTemp;
    }
}
//////////
function CreateSnapshot(name) {
   function cTID(s) { return app.charIDToTypeID(s); };
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
        ref.putClass( cTID('SnpS') );
    desc.putReference( cTID('null'), ref );
        var ref1 = new ActionReference();
        ref1.putProperty( cTID('HstS'), cTID('CrnH') );
    desc.putReference( cTID('From'), ref1 );
    desc.putString( cTID('Nm  '), name);
    desc.putEnumerated( cTID('Usng'), cTID('HstS'), cTID('FllD') );
    executeAction( cTID('Mk  '), desc, DialogModes.NO );
}
//////////
res ="dialog { \
text:'重复执行选定动作',\
        group: Group{orientation: 'column',alignChildren:'left',\
                corrdination: Panel { orientation: 'row', \
                        text: '选择动作', \
                                cla: Group { orientation: 'row', \
                                        d: DropDownList {  alignment:'left' },\
                                        }\
                                act: Group { orientation: 'row', \
                                        d: DropDownList {  alignment:'left' },\
                                        }\
                                },  \
                num: Group { orientation: 'row', \
                                    s: StaticText { text:'执行次数:' }, \
                                    e: EditText { preferredSize: [50, 20] } ,\
                                    }, \
                Snapshot:Group{ orientation: 'row', \
                                    c: Checkbox { preferredSize: [16, 16]} ,\
                                    s: StaticText {text:'建立快照(根据动作内容适当选择)'},\
                                    }, \
                },\
        buttons: Group { orientation: 'row', alignment: 'right',\
                Btnok: Button { text:'确定', properties:{name:'ok'} }, \
                Btncancel: Button { text:'取消', properties:{name:'cancel'} } \
                } \
}";
    
win = new Window (res);
    
    GlobalVariables();
    var actionInfo = GetActionSetInfo();
    var ddSet=win.group.corrdination.cla.d;
    var ddAction=win.group.corrdination.act.d;
    if ( actionInfo.length > 0 ) {
        for ( var i = 0; i < actionInfo.length; i++ ) {
            ddSet.add( "item", actionInfo[i].name );
        }
        ddSet.items[0].selected = true;
        ddSet.onChange = function() { 
            ddAction.removeAll();
            for ( var i = 0; i < actionInfo[ this.selection.index ].children.length; i++ ) {
                ddAction.add( "item", actionInfo[ this.selection.index ].children[ i ].name );
            }
            if ( ddAction.items.length > 0 ) {
                ddAction.items[0].selected = true;
            }
            ddSet.helpTip = ddSet.items[ ddSet.selection.index ].toString();
        }
        ddSet.onChange();
    } else {
        ddSet.enabled = false;
        ddAction.enabled = false;
    }
    
    ddAction.onChange = function() {
        ddAction.helpTip = ddAction.items[ ddAction.selection.index ].toString();
    }
    
win.buttons.Btncancel.onClick = function () {
this.parent.parent.close();
}
win.buttons.Btnok.onClick = function () {
   g=Number(win.group.num.e.text);
    g=200;
    if(g<1){
        alert ('-_-!!! 至少得运行一次吧?')
        win.group.num.e.text='1';
        g=1
    }else {
        var b=win.group.Snapshot.c.value;        
        if(b && app.documents.length) {CreateSnapshot(g+"遍动作执行前");} //安全起见,建立快照
        for(i=0;i<g;i++){
        doAction(ddAction.selection,ddSet.selection) //执行选择的动作
        }
        this.parent.parent.close();
    }
}
win.center();
win.show();
TOPICS
Actions and scripting, Bug, How to, Problem or error, Windows

Views

43

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

Can this script modify the path of action storage?

Explorer ,
Sep 04, 2020

Copy link to clipboard

Copied

The role of this script is to perform an action many times.
I often use to batch modify the size of many pictures, but every time I change the path, it will default to the last storage location.
If you want to save to the current, you must first open the graph in the current path and double-click to run the "save" behavior in the action, which is very troublesome.
Is there a master who can help realize:
Let the modified picture be opened and saved in the current path by default.
Thank you very much~

#target photoshop
app.bringToFront();
// 重复执行N遍选中的动作
/////////////////////////////////////////////////////////////////////
// Function: GlobalVariables
// Usage: global action items that are reused
// Input: <none>
// Return: <none>
/////////////////////////////////////////////////////////////////////
function GlobalVariables() {
    gClassActionSet = charIDToTypeID( 'ASet' );
    gClassAction = charIDToTypeID( 'Actn' );
    gKeyName = charIDToTypeID( 'Nm  ' );
    gKeyNumberOfChildren = charIDToTypeID( 'NmbC' );
}
/////////////////////////////////////////////////////////////////////
// Function: GetActionSetInfo
// Usage: walk all the items in the action palette and record the action set
//        names and all the action children
// Input: <none>
// Return: the array of all the ActionData
// Note: This will throw an error during a normal execution. There is a bug
// in Photoshop that makes it impossible to get an acurate count of the number
// of action sets.
/////////////////////////////////////////////////////////////////////
function GetActionSetInfo() {
    var actionSetInfo = new Array();
    var setCounter = 1;
      while ( true ) {
        var ref = new ActionReference();
        ref.putIndex( gClassActionSet, setCounter );
        var desc = undefined;
        try { desc = executeActionGet( ref ); }
        catch( e ) { break; }
        var actionData = new ActionData();
        if ( desc.hasKey( gKeyName ) ) {
            actionData.name = desc.getString( gKeyName );
        }
        var numberChildren = 0;
        if ( desc.hasKey( gKeyNumberOfChildren ) ) {
            numberChildren = desc.getInteger( gKeyNumberOfChildren );
        }
        if ( numberChildren ) {
            actionData.children = GetActionInfo( setCounter, numberChildren );
            actionSetInfo.push( actionData );
        }
        setCounter++;
    }
    return actionSetInfo;
}
/////////////////////////////////////////////////////////////////////
// Function: GetActionInfo
// Usage: used when walking through all the actions in the action set
// Input: action set index, number of actions in this action set
// Return: true or false, true if file or folder is to be displayed
/////////////////////////////////////////////////////////////////////
function GetActionInfo( setIndex, numChildren ) {
    var actionInfo = new Array();
    for ( var i = 1; i <= numChildren; i++ ) {
        var ref = new ActionReference();
        ref.putIndex( gClassAction, i );
        ref.putIndex( gClassActionSet, setIndex );
        var desc = undefined;
        desc = executeActionGet( ref );
        var actionData = new ActionData();
        if ( desc.hasKey( gKeyName ) ) {
            actionData.name = desc.getString( gKeyName );
        }
        var numberChildren = 0;
        if ( desc.hasKey( gKeyNumberOfChildren ) ) {
            numberChildren = desc.getInteger( gKeyNumberOfChildren );
        }
        actionInfo.push( actionData );
    }
    return actionInfo;
}
/////////////////////////////////////////////////////////////////////
// Function: ActionData
// Usage: this could be an action set or an action
// Input: <none>
// Return: a new Object of ActionData
/////////////////////////////////////////////////////////////////////
function ActionData() {
    this.name = "";
    this.children = undefined;
    this.toString = function () {
        var strTemp = this.name;
        if ( undefined != this.children ) {
            for ( var i = 0; i < this.children.length; i++ ) {
                strTemp += " " + this.children[i].toString();
            }
        }
        return strTemp;
    }
}
//////////
function CreateSnapshot(name) {
   function cTID(s) { return app.charIDToTypeID(s); };
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
        ref.putClass( cTID('SnpS') );
    desc.putReference( cTID('null'), ref );
        var ref1 = new ActionReference();
        ref1.putProperty( cTID('HstS'), cTID('CrnH') );
    desc.putReference( cTID('From'), ref1 );
    desc.putString( cTID('Nm  '), name);
    desc.putEnumerated( cTID('Usng'), cTID('HstS'), cTID('FllD') );
    executeAction( cTID('Mk  '), desc, DialogModes.NO );
}
//////////
res ="dialog { \
text:'重复执行选定动作',\
        group: Group{orientation: 'column',alignChildren:'left',\
                corrdination: Panel { orientation: 'row', \
                        text: '选择动作', \
                                cla: Group { orientation: 'row', \
                                        d: DropDownList {  alignment:'left' },\
                                        }\
                                act: Group { orientation: 'row', \
                                        d: DropDownList {  alignment:'left' },\
                                        }\
                                },  \
                num: Group { orientation: 'row', \
                                    s: StaticText { text:'执行次数:' }, \
                                    e: EditText { preferredSize: [50, 20] } ,\
                                    }, \
                Snapshot:Group{ orientation: 'row', \
                                    c: Checkbox { preferredSize: [16, 16]} ,\
                                    s: StaticText {text:'建立快照(根据动作内容适当选择)'},\
                                    }, \
                },\
        buttons: Group { orientation: 'row', alignment: 'right',\
                Btnok: Button { text:'确定', properties:{name:'ok'} }, \
                Btncancel: Button { text:'取消', properties:{name:'cancel'} } \
                } \
}";
    
win = new Window (res);
    
    GlobalVariables();
    var actionInfo = GetActionSetInfo();
    var ddSet=win.group.corrdination.cla.d;
    var ddAction=win.group.corrdination.act.d;
    if ( actionInfo.length > 0 ) {
        for ( var i = 0; i < actionInfo.length; i++ ) {
            ddSet.add( "item", actionInfo[i].name );
        }
        ddSet.items[0].selected = true;
        ddSet.onChange = function() { 
            ddAction.removeAll();
            for ( var i = 0; i < actionInfo[ this.selection.index ].children.length; i++ ) {
                ddAction.add( "item", actionInfo[ this.selection.index ].children[ i ].name );
            }
            if ( ddAction.items.length > 0 ) {
                ddAction.items[0].selected = true;
            }
            ddSet.helpTip = ddSet.items[ ddSet.selection.index ].toString();
        }
        ddSet.onChange();
    } else {
        ddSet.enabled = false;
        ddAction.enabled = false;
    }
    
    ddAction.onChange = function() {
        ddAction.helpTip = ddAction.items[ ddAction.selection.index ].toString();
    }
    
win.buttons.Btncancel.onClick = function () {
this.parent.parent.close();
}
win.buttons.Btnok.onClick = function () {
   g=Number(win.group.num.e.text);
    g=200;
    if(g<1){
        alert ('-_-!!! 至少得运行一次吧?')
        win.group.num.e.text='1';
        g=1
    }else {
        var b=win.group.Snapshot.c.value;        
        if(b && app.documents.length) {CreateSnapshot(g+"遍动作执行前");} //安全起见,建立快照
        for(i=0;i<g;i++){
        doAction(ddAction.selection,ddSet.selection) //执行选择的动作
        }
        this.parent.parent.close();
    }
}
win.center();
win.show();
TOPICS
Actions and scripting, Bug, How to, Problem or error, Windows

Views

44

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
Sep 04, 2020 0
JJMack LATEST
Most Valuable Participant ,
Sep 04, 2020

Copy link to clipboard

Copied

First I must apologise for I do not understand what you wrote:

"The role of this script is to perform an action many times. I often use to batch modify the size of many pictures, but every time I change the path, it will default to the last storage location. If you want to save to the current, you must first open the graph in the current path and double-click to run the "save" behavior in the action, which is very troublesome."

 

I have no concept of  graph in the current path so I'lm lost I'm not on your Path..

 

However.  I would like to ask why you not using a Plug-in like "Image Processor Pro" it seems like it would be a perfect fit for what you want   to do "batch modify the size of many pictures"  the Plug-In can be recorded in actions so you do not always use its Dialog.  You can also save and load dialog setups.  The Plug-in can batch you actions in its processing and can also re-size documents processed.

 

Here is one  I use.  For each Image File process in the batch the Plug-in will save 10 Jpeg images they nay have  different Aspect ratios and sizes.  In this case the Plug-in not changing the document Aspect Ratio and Sizes 10 actions I recorded do. The Plug-in opens the image in  the batch and process then and includes one of ms actions foe each output file. The Plug-in  does and the files saving and closing of the documents batched. The Plug-in is very flexible and can save versions. It has good file saving options.

image.png

 

JJMack

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
Reply
Loading...
Sep 04, 2020 0