7 Replies Latest reply on Dec 16, 2017 1:38 AM by Kukurykus

    Variable Scripting

    roberth74866517

      I am trying to replace text elements feeding from a web API for generating cards from photoshop files.  Is it possible to create a script which either replaces text and image elements by layer or loads a dataset and exports those datasets as photoshop or jpeg files?

        • 1. Re: Variable Scripting
          Kukurykus Adobe Community Professional

          I must understand process you describe as it's not clear for me:

           

          1) what are those cards - photoshop documents or cards you use to play game?

           

          2) you connect to photoshop by some web api?

           

          3) you want to send images and texts to photoshop?

           

          4) doing above you specify info where exactly images have to be located and which text layers changed in .psd document?

           

           

          If there is .psd template and each document / card has specified positions of all elements (layers) that you do is:

           

          send (in some form, even .txt file) array of all new texts and images (both with related positions in a document). They are automatically save this .txt file together with new images folder on the computer / server where Photoshop is present. At the same time started process checks is all already loaded to hard disk to execute .jsx script which does that you described. When all is ready new cards are saved in processed folder), you can upload / export them to the site back (manually?)

           

           

          If it is how that has to be done, photoshop script is the smalles problem to write. Just attach in next post .psd template of your card, give some examplary set of images and texts to replace. Finally specify which have to be changed in document.

           

          Ps. it's also important that someone writing script for this process should design how informations for replacement have to stored in a .txt file, a script could read them with no problem.

          • 2. Re: Variable Scripting
            roberth74866517 Level 1

            Kukurykus  wrote

             

            I must understand process you describe as it's not clear for me:

             

            1) what are those cards - photoshop documents or cards you use to play game?

             

            2) you connect to photoshop by some web api?

             

            3) you want to send images and texts to photoshop?

             

            4) doing above you specify info where exactly images have to be located and which text layers changed in .psd document?

             

             

            If there is .psd template and each document / card has specified positions of all elements (layers) that you do is:

             

            send (in some form, even .txt file) array of all new texts and images (both with related positions in a document).

            then for example save this .txt file together with new images folder on the computer / server where Photoshop is present.

            at the same time started process checks is all already loaded to hard disk to execute .jsx script which does you described.

            When all is ready new cards are saved 9in processed folder), you can upload / export them to the site back (manually?)

             

             

            If it is how that has to be done, photoshop script is the smalles problem to write. Just attach .psd template of your card, give some set of images and texts to replace. Finally specify which have to be changed in document.

             

            Ps. it's also important that someone writing script for this process should design how informations for replacement have to stored in a .txt file, a script could read them with no problem.

             

             

            1) They're essentially photoshop documents.

            2) The web API isn't relevant - I can feed that info into this script ideally.

            3) I want to replace certain layers' text values or replace certain image layers.  My understanding is that you would usually use photoshop variables to achieve this but I am aiming to automate this - I.E. have no user interaction. 

            4) Yes, I would specify the replacement content (usually this is done via some sort of CSV or text file for photoshop to parse)

             

            My question is twofold: first, is it possible to have a script specify a photoshop dataset file (see the menu Image->Variables->Data Sets).  Second: if this is impossible, what are the functions for selecting a layer and replacing its text content or selecting a layer and replacing its image contents.  For the second part, where would the image be replaced (would it be placed on the top left of the previous image? Can you define that behavior?).

             

            The input could be a text file specifying the layer names to replace as well as the text content or a file path to replace it with, sure. 

            • 3. Re: Variable Scripting
              roberth74866517 Level 1

              I'll attach a sample photoshop file, if you can make a proof of concept script I can easily modify this.

               

               

              Adobe_Customer_Support.rar - Google Drive

               

               

              Above is a compressed container which has a sample .PSD as well as a .CSV that I would usually use for variable replacement.  Ideally when the script is ran I am left with three files, one for each of the values specified in the .CSV.  This can be done with photoshop datasets but I am aiming to do this through a photoshop VB or JS script.

              • 4. Re: Variable Scripting
                Kukurykus Adobe Community Professional

                It's one of the easiest thing you can script in photoshop to do this all. There won't be problem with it. I'm at work now, so I'll write it in a free moment or when I'm back home in 3 hours. Here are many experienced scripters. Possibly someone will do it before me, and perhaps even better. If not, then only I can focus on this, I do it and answer to your questions 

                • 6. Re: Variable Scripting
                  roberth74866517 Level 1

                  Like I described above using datasets is not possible as I would like to automate this action entirely (and not have to click to import a dataset and export the files). 

                  • 7. Re: Variable Scripting
                    Kukurykus Adobe Community Professional

                    So here is how it is working:

                     

                    - there is a /Replacement/Data/ folder located on desktop

                    - the content of this folder is: text_replacement.csv, Excluded.txt, Sender.bat, File.psd, Replacement.jsx

                    - I could store content of these all files in last one Replacement.jsx, but that was too much work, so there are 5 files

                     

                    How does it work:

                     

                    You run Replacement.jsx. It check is it in 'Adobe Scripts' folder located in Documents (default by Adobe during Photoshop installation). At the first time it sees it isn't so it creates Path.txt in /Replacement/Data/ on desktop, and then run Sender.bat. Sender.bat checks what is your Windows Documents path (as it may differ depending on settings during instalation or later changes by user) and write this information in path.txt. Then Sender.bat using Excluded.txt file checks which files from /Replacement/Data/ on desktop are allowed to be sent to 'Adobe Scripts' in Documents folder. The only file (you my later set other for your needs) which will be sent is Replacement.jsx. Finally there is created Replacement.lnk shortcut in /Replacement/Data/ folder on desktop to Replacement.jsx in 'Adobe Scripts' folder. At the end Replacement.jsx from /Replacement/Data/ folder on desktop executes Replacement.jsx in 'Adobe Scripts' and it starts that part of work the script had to be written for.

                     

                    Every next time you want to use script running it from outside of Photoshop by other process (as I understand you want to have everything automated), it simply must run Replacement.lnk shortcut created in /Replacement/Data/ which is linked to new Replacement.jsx in 'Adobe Scripts'

                     

                    Explanation why that first part was important:

                     

                    Unfortunately as far as you don't run scripts directly from photoshop there pops up security question where you must click yes / no. Expection is that 'Adobe Scripts' folder located in Documents. That part could be done of course manually, (I mean placing Replacement.jsx to 'Adobe Scripts' folder and creating shortcut to it, but for exercise I did it by script).

                     

                    I understand you have some skills to write scripts (not in Photoshop of course). The only job you have to do is to figure out how other script from web api has to send images, .psd and .csv files into /Replacement/Data/ folder on desktop, and then run shortcut to the script.

                     

                     

                    The main part of script you asked is opening template (File.psd) from /Replacement/Data/ folder, then reading new names from text_replacement.csv, and then making a loop to replace each of them with existing one in opened File.psd. When it's done for each name, it saves it in upper Replacement folder with the name of label it put to the text area of new image. And when loop is finished it closes last (same for all) file and you have all 3 new files in Replacement folder.

                     

                     

                    If you need help with images, I mean placing them into document, resizing and setting position (that you asked is that possible) let me know, and rather attach some image samples and certain .psd file where it has to be put, with specified positions...

                     

                    Replacement.jsx:

                    #target photoshop
                    function rw(v1, v2, v3) {v2.open(v1), v1 < 'w' ? v3 = v2.read() : v2.write(v3), v2.close(); return v3}
                    
                    function algn(v) {// ALIGNMENT:
                         function cTT(v) {return charIDToTypeID(v)}
                         (ref = new ActionReference()).putEnumerated(cTT('Lyr '),  cTT('Ordn'), cTT('Trgt'));
                         (dsc = new ActionDescriptor()).putReference(cTT('null'), ref)
                         dsc.putEnumerated(cTT('Usng'), cTT('ADSt'), cTT('Ad' + v))
                         executeAction(cTT('Algn'), dsc, DialogModes.NO)
                    }
                    
                    if (!/A[\w ]+s$/.test(decodeURI(fNp = (fN = File($.fileName)).parent))) {
                         rw('w', pth = File(fNp + '/Path.txt'), '')
                         File(fNp + '/Sender.bat').execute()
                         while(!rw('r', pth)) refresh();
                         (jsx = File(eval(rw('r', pth).replace(/\n/, '')
                         .replace(/\\/g, '//')) + '/' + fN.name)), pth.remove()
                         File(String(fN).slice(0, -4)).createAlias(jsx), jsx.execute()
                    }
                    
                    else {
                         open(fle = File((rep = Folder.desktop + '/Replacement/Data/') + 'File.psd')),
                         (aD = activeDocument).selection.selectAll(), (aL = aD.activeLayer)
                         .textItem.justification = Justification.CENTER, algn('CH')
                    
                         arr = rw('r', File(rep + '/text_replacement.csv')).split(/\n/)
                    
                         for(i = 1; i < arr.length; i++) {
                              aL.textItem.contents = arr[i], aD.saveAs(File(File(rep).parent + '/' + arr[i]))
                         }
                         aD.close(SaveOptions.DONOTSAVECHANGES)
                    }
                    

                     

                    Sender.bat:

                    set "dat=%HOMEPATH%\desktop\Replacement\Data"
                    cd %dat%
                    for /f "skip=2 tokens=2*" %%a in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do @set "docs=%%b"
                    xcopy /f /y /exclude:Excluded.txt %dat% "%docs%\Adobe Scripts"
                    echo "%docs%\Adobe Scripts" > %dat%\Path.txt
                    

                     

                    Excluded.txt:

                    text_replacement.csv
                    Excluded.txt
                    Sender.bat
                    File.psd
                    Path.txt
                    

                     

                    text_replacement.csv: (that you attached to your post)

                    text_replacement
                    Sample 1
                    Sample 2
                    Sample 3
                    

                     

                    and File.psd (you attached to your post with other name)