Skip to main content
Alv14
Known Participant
March 17, 2019
Question

Script to change text layers contents in photoshop

  • March 17, 2019
  • 3 replies
  • 14532 views

Hi Everybody,

I have a photoshop file with existing text layers,
and i have a word or excel document.

I would like to know if its possible to make a script to copy from the word/excel and replace the text in each photoshop layers.

OR

i copy a paragraph from the word/excel and as i click on a text layers and run a script to change the text content.

Thanks a lot.

This topic has been closed for replies.

3 replies

Participant
April 5, 2019

Allrighty.

I made a script that loads a .txt file and split it into an array
Then the script finds all Artborads/Groups and loops thru all textlayers
and fill the text content with the array.


https://www.dropbox.com/s/lur2xo8o6mr4qr9/from-txt-to-psd.zip?dl=0https://www.dropbox.com/s/lur2xo8o6mr4qr9/from-txt-to-psd.zip?dl=0

// to get line breaks in the text file you need to insert  ( %0D%0A  ).
Im line one  %0D%0A i am line tow resoults in

Im line one
I am line tow

Participant
April 5, 2019

I have a simple js script you can use.

it runs thru all the layers in your photoshop file, if the layer is a text file

it uses the content of the array.  It is far from perfect but it works.

Save the script as yourfilename.js 
and run it in photoshop



#target photoshop;    //array of what ever you need - but as many items in the array as text layers in the photoshop file var itemsAry=['Spain','lardtub','100','grandma on fire', 'nutbag','null','1000000'];  var layers = app.activeDocument.layers;   for (var i =0; i<layers.length; i++) {        if (layers.kind == "LayerKind.TEXT") {                  layers.textItem.contents=itemsAry     }  }



js and photshop test file for download



https://www.dropbox.com/s/26lhgfha7jhoco5/fucj-year-psd-text.zip?dl=0

pixxxelschubser
Community Expert
Community Expert
March 19, 2019

Please show something (screenshots or example *.psd + *.word/excel)

Because of there are too much possibilities.

Alv14
Alv14Author
Known Participant
March 20, 2019

Hi,

Thanks for your reply,
below is a small exemple of what i want to do.

on the left is the excel file and on the right the PSD.
As you can see the psd has 4 text layers and 4 rows in the excel file.

Instead of going in the excel copy the text, then double click on the text layer in photoshop and paste to change the text,

I want to know if any script can be done to do that.

SuperMerlin
Inspiring
March 20, 2019

If you are using windows this C# program should work

N.B. There isn't much error checking though.

It prompts for the Excel file and expects the document open in Photoshop.

using System;

using System.Windows.Forms;

namespace CreateExcel

{

    class Program

    {

        [STAThread]

        static void Main(string[] args)

        {

            OpenFileDialog dlg = new OpenFileDialog();

            dlg.Filter = "Excel Worksheets|*.xl*";

            dlg.Multiselect = false;

            string name;

            if (dlg.ShowDialog() == DialogResult.OK)

            {

                name = dlg.FileName;

            }

            else

            {

                return;

            }

             var excelType = Type.GetTypeFromProgID("Excel.Application");

            if (excelType == null) return;

            dynamic excel = Activator.CreateInstance(excelType);

            excel.Visible = false;

            excel.Workbooks.Open(name);

            string Location = excel.Cells(1, 1).Value;

            string Age = excel.Cells(2, 1).Value;

            string Address = excel.Cells(3, 1).Value;

            string Name = excel.Cells(4, 1).Value;

            excel.Quit();

            dynamic app = Activator.CreateInstance(Type.GetTypeFromProgID("Photoshop.Application"));

            if (app.Documents.Count == 0) return;

            app.ActiveDocument.ActiveLayer = app.ActiveDocument.ArtLayers.GetByName("Location");

            app.ActiveDocument.ActiveLayer.TextItem.Contents = Location;

            app.ActiveDocument.ActiveLayer = app.ActiveDocument.ArtLayers.GetByName("Age");

            app.ActiveDocument.ActiveLayer.TextItem.Contents = Age;

            app.ActiveDocument.ActiveLayer = app.ActiveDocument.ArtLayers.GetByName("Address");

            app.ActiveDocument.ActiveLayer.TextItem.Contents = Address;

            app.ActiveDocument.ActiveLayer = app.ActiveDocument.ArtLayers.GetByName("Name");

            app.ActiveDocument.ActiveLayer.TextItem.Contents = Name;

        }

    }

}