0

The script below is recorded with Photoshop's script listner. It searches the document for the word "Sun" and replaces it with the word "Moon".

My question: how to get the replacement word, "Moon", from an external .txt file?

// =======================================================
var id1 = charIDToTypeID( "Opn " );
var desc1 = new ActionDescriptor();
var id2 = charIDToTypeID( "null" );
desc1.putPath( id2, new File( "C:\\document.psd" ) );
executeAction( id1, desc1, DialogModes.NO );

// =======================================================
var id3 = stringIDToTypeID( "replace" );
var desc2 = new ActionDescriptor();
var id4 = charIDToTypeID( "null" );
    var ref1 = new ActionReference();
    var id5 = charIDToTypeID( "Prpr" );
    var id6 = stringIDToTypeID( "replace" );
    ref1.putProperty( id5, id6 );
    var id7 = charIDToTypeID( "TxLr" );
    var id8 = charIDToTypeID( "Ordn" );
    var id9 = charIDToTypeID( "Al  " );
    ref1.putEnumerated( id7, id8, id9 );
desc2.putReference( id4, ref1 );
var id10 = charIDToTypeID( "Usng" );
    var desc3 = new ActionDescriptor();
    var id11 = stringIDToTypeID( "find" );
    desc3.putString( id11, "Sun" );
    var id12 = stringIDToTypeID( "replace" );
    desc3.putString( id12, "Moon" );
    var id13 = stringIDToTypeID( "checkAll" );
    desc3.putBoolean( id13, true );
    var id14 = charIDToTypeID( "Fwd " );
    desc3.putBoolean( id14, false );
    var id15 = stringIDToTypeID( "caseSensitive" );
    desc3.putBoolean( id15, true );
    var id16 = stringIDToTypeID( "wholeWord" );
    desc3.putBoolean( id16, false );
   var id17 = stringIDToTypeID( "findReplace" );
   desc2.putObject( id10, id17, desc3 );
    executeAction( id3, desc2, DialogModes.NO );
Malasorte
  • 1,153
  • 7
  • 21
  • 45

1 Answers1

1

Check out my answer on this question. How does one load some variables at runtime in Photoshop Script?. If the word you are after is the only word in your document then, the variable 'str' will contain your word. Also check out my answer here: Find and replace text in multiple Photoshop files? which provides a find/replace method that doesn't rely on the messy script listener code.

At minimum add the following to the top of the code you've provided:

var file = new File( /c/folder/yourfile.txt );       
file.open("r");
var str = file.read();

Then replace the line

desc3.putString( id12, "Moon" ); 

with

desc3.putString( id12, str );

If that doesn't work you'll need to post your updated code so I can see what you are doing wrong.

Community
  • 1
  • 1
Anna Forrest
  • 1,711
  • 14
  • 21
  • Thank you for your reply and your time! Unfortunatley, I just don't have the skills to make it work. After much guesswork, no result. It's clear for me that I should work on this part: desc3.putString( id12, "Moon" );. And add some file.open("r"); and file.read();. Tried diffrent variations, no luck... – Malasorte Feb 22 '14 at 16:13
  • Thank you! It worked great! One more question: if the external text file contains several paragraphs, not just one word, the script gets all the text but completely ignores paragraph breaks (the blank line between paragraphs). I realize that because the script uses Photoshop's built in search/replace function it may have limitations. However, is there any way to make it import the paragraph breaks? – Malasorte Feb 23 '14 at 04:47