Skip navigation
jtollberg-cat
Currently Being Moderated

Script for text layer content replace w/ user input dialog

Aug 17, 2012 11:32 AM

I've been messing around with building out a script that will allow users to replace content in a text layer.

Unfortunately, the content is going to always be different, formatted differently, and not placed in any sort of XL or CSV file.

 

I'd like to be able to run a script that will select the appropriate named text layers and then prompt the user to input the various values.  The only script I've been able to find along those lines is on the ps-scripts site here:

prompt the user for textual input - Script

 

Anyone come up or across something that falls within those interaction parameters?

 

 

Thanks!

 
Replies
  • Currently Being Moderated
    Aug 17, 2012 1:20 PM   in reply to jtollberg-cat

    Something like this? ...

     

     

    #target Photoshop
    app.bringToFront();
    main();
    function main(){
    if(!documents.length) return;
    var txtArray = new Array();
    txtArray = getTxtInfo();
    var win = new Window( 'dialog', '' ); 
    g = win.graphics;
    var myBrush = g.newBrush(g.BrushType.SOLID_COLOR, [0.99, 0.99, 0.99, 1]);
    g.backgroundColor = myBrush;
    win.orientation='stack';
    win.p1= win.add("panel", undefined, undefined, {borderStyle:"black"}); 
    win.g1 = win.p1.add('group');
    win.g1.orientation = "row";
    win.title = win.g1.add('statictext',undefined,'Text Replacement');
    win.title.alignment="fill";
    var g = win.title.graphics;
    g.font = ScriptUI.newFont("Georgia","BOLDITALIC",22);
    win.g5 =win.p1.add('group');
    win.g5.orientation = "row";
    win.g5.alignment='left';
    win.g5.spacing=10;
    win.g5.st1 = win.g5.add('statictext',undefined,'Layer Name');
    win.g5.st1.preferredSize=[110,20];
    win.g5.dd1 = win.g5.add('dropdownlist');
    win.g5.dd1.preferredSize=[300,20];
    win.g10 =win.p1.add('group');
    win.g10.orientation = "row";
    win.g10.alignment='left';
    win.g10.st1 = win.g10.add('statictext',undefined,'Layer Contents');
    win.g10.st1.preferredSize=[110,20];
    win.g10.et1 = win.g10.add('edittext',undefined,'');
    win.g10.et1.preferredSize=[300,20];
    win.g10.et1.enabled=false;
    for(var a in txtArray){
        win.g5.dd1.add('item',txtArray[a][1].toString());
        }
    win.g5.dd1.selection=0;
    win.g10.et1.text=txtArray[0][2].toString();
    win.g5.dd1.onChange=function(){
    win.g10.et1.text=txtArray[this.selection.index][2].toString();
    }
    win.g15 =win.p1.add('group');
    win.g15.orientation = "row";
    win.g15.alignment='fill';
    win.g15.st1 = win.g15.add('statictext',undefined,'Replace text with:- ');
    win.g15.st1.preferredSize[150,20];
    win.g15.et1 = win.g15.add('edittext',undefined,'');
    win.g15.et1.preferredSize=[350,20];
    win.g20 =win.p1.add('group');
    win.g20.orientation = "row";
    win.g20.alignChildren='fill';
    win.g20.bu1 = win.g20.add('button',undefined,'Update Text');
    win.g20.bu1.preferredSize=[200,35];
    win.g20.bu2 = win.g20.add('button',undefined,'Cancel');
    win.g20.bu2.preferredSize=[200,35];
    win.g20.bu1.onClick=function(){
    if(win.g15.et1.text == ''){
        alert("No replacement text has been entered!");
        return;
        }
    win.close(0);
    selectLayerById(Number(txtArray[win.g5.dd1.selection.index][0]));
    activeDocument.activeLayer.textItem.contents=win.g15.et1.text;
        }
    win.center();
    win.show();
    }
    function getTxtInfo(){ 
       var ref = new ActionReference(); 
       ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') ); 
       var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1; 
       var Names=[];
    try{
        activeDocument.backgroundLayer;
    var i = 0; }catch(e){ var i = 1; };
       for(i;i<count;i++){ 
           if(i == 0) continue;
            ref = new ActionReference(); 
            ref.putIndex( charIDToTypeID( 'Lyr ' ), i );
            var desc = executeActionGet(ref);
            var layerName = desc.getString(charIDToTypeID( 'Nm  ' ));
            var Id = desc.getInteger(stringIDToTypeID( 'layerID' ));
            if(layerName.match(/^<\/Layer group/) ) continue;
            var layerType = typeIDToStringID(desc.getEnumerationValue( stringIDToTypeID( 'layerSection' )));
            var Vis = desc.getBoolean(stringIDToTypeID( 'visible' )); 
                if( desc.hasKey( stringIDToTypeID( 'textKey' ) )) {
                    var contents = desc.getObjectValue(stringIDToTypeID('textKey')).getString( stringIDToTypeID('textKey'));
                if(!Vis) continue; //do not select hidden layers
                    Names.push([[Id],[layerName],[contents]]);
                    }
       } 
    return Names;
    };
    function selectLayerById(ID, add) {
        add = (add == undefined)  ? add = false : add;
     var ref = new ActionReference();
     ref.putIdentifier(charIDToTypeID('Lyr '), ID);
     var desc = new ActionDescriptor();
     desc.putReference(charIDToTypeID('null'), ref);
     if (add) {
      desc.putEnumerated(stringIDToTypeID('selectionModifier'), stringIDToTypeID('selectionModifierType'), stringIDToTypeID('addToSelection'));
     }
     desc.putBoolean(charIDToTypeID('MkVs'), false);
     executeAction(charIDToTypeID('slct'), desc, DialogModes.NO);
    }
    
     
    
     
    |
    Mark as:
  • Currently Being Moderated
    Aug 20, 2012 9:00 AM   in reply to jtollberg-cat

    It should work for point text or paragraph as it's just getting the textItem.contents, then replacing it with the user input.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points