Photoshop脚本 - 从外部文件中获取单词

时间:2014-02-22 06:36:16

标签: javascript replace find photoshop

下面的脚本是用Photoshop的脚本列表器记录的。它在文档中搜索单词“Sun”并将其替换为单词“Moon”。

我的问题:如何从外部.txt文件中获取替换词“Moon”?

// =======================================================
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 );

1 个答案:

答案 0 :(得分:1)

查看我对这个问题的回答。 How does one load some variables at runtime in Photoshop Script?。如果你所说的单词是你文档中唯一的单词,那么变量'str'将包含你的单词。另请查看我的答案:Find and replace text in multiple Photoshop files?,它提供了一个不依赖于凌乱的脚本侦听器代码的查找/替换方法。

至少将以下内容添加到您提供的代码的顶部:

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

然后替换

desc3.putString( id12, "Moon" ); 

desc3.putString( id12, str );

如果这不起作用,您需要发布更新的代码,这样我才能看到您做错了什么。