gpt4 book ai didi

javascript - 使用 photoshop 脚本更改选定的文本图层语言

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:43:03 25 4
gpt4 key购买 nike

虽然我设法放置了一个功能脚本来一起更改所选图层的字典语言,但这还不是我所需要的。

我目前正在检索我选择的图层的索引,使用它们来获取图层名称——这是有问题的部分——然后​​根据在我显示的自定义对话框。

层名称的使用是一个非常不成熟的解决方案,我需要精确度,我需要能够根据层 ID 或索引更改语言。不仅我的脚本容易出现名称冲突,而且我不知道那些选定的层将位于文档的根目录还是在 5 个组堆栈中。

这是我当前的代码:

主要功能

function main() {
// Create window with language selection and retrieve selected value
var lang = createWindow();

// Retrieve selected layers
var selectedLayers = getSelectedLayersIdx();

// Look at each entry and change language when possible
for(var a in selectedLayers){
try {
setLangByIndex(Number( selectedLayers[a]), lang );
} catch(err) { }
}
}

(createWindow 返回一个 lang 值,跳过那个)

迈克得到 Idx

function  getSelectedLayersIdx(){
var selectedLayers = new Array;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
var desc = executeActionGet(ref);
if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ) {
desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
var c = desc.count
var selectedLayers = new Array();
for(var i=0;i<c;i++) {
try{
activeDocument.backgroundLayer;
selectedLayers.push( desc.getReference( i ).getIndex() );
} catch(e) {
selectedLayers.push( desc.getReference( i ).getIndex()+1 );
}
}
} else {
var ref = new ActionReference();
ref.putProperty( charIDToTypeID('Prpr') , charIDToTypeID( 'ItmI' ));
ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
try {
activeDocument.backgroundLayer;
selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( 'ItmI' ))-1);
} catch(e) {
selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( 'ItmI' )));
}
}
return selectedLayers;
}

设置语言的函数

function setLangByIndex(idx, lang){
ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Nm " ));
ref.putIndex( charIDToTypeID( "Lyr " ), idx );
var name = executeActionGet(ref).getString(charIDToTypeID( "Nm " ));

var el = activeDocument.layers.getByName( name );
// Temp quick block; MUST UPDATE IN CASE OF NEW LANGUAGES
if (lang == 'pt_br' )
el.textItem.language = Language.BRAZILLIANPORTUGUESE;
else if (lang == 'en_us' )
el.textItem.language = Language.ENGLISHUSA;
else
el.textItem.language = Language.ENGLISHUK;
}

我尝试过的低效解决方法:

  • 使用 Adob​​e 的脚本监听器通过 executeAction 更改语言,但无法让它发挥作用。语言部分在里面~4叠描述符(和一个列表!)。我迷路了,一直收到错误 8800。
  • 弄清楚如何使用 idx 获取对象引用。失败的悲惨地。

最佳答案

在我注意到一些与段落样式相关的恼人故障(您可以将其定义为 PS 上的文本的默认样式,而不是 JS 对象)之后,我放弃了顽固并停止避免通过索引数组进行新循环完成后重新选择图层。

/*
* Based in Mike Hale's "getSelectedLayersIdx" script and Adobe's Scripting Listener dump.
Based the panel on Davide Barranca's code: http://www.davidebarranca.com/2012/10/scriptui-window-in-photoshop-palette-vs-dialog/

Useful Resources:
Photoshop char list: http://www.pcpix.com/Photoshop/char.htm
ActionDescriptor methods: http://jongware.mit.edu/pscs5js_html/psjscs5/pc_ActionDescriptor.html
TextItem methods: http://jongware.mit.edu/pscs5js_html/psjscs5/pc_TextItem.html
You can fish for language codes here: http://wingp.googlecode.com/svn/photogp/photoshopapi/photoshop/PIStringTerminology.h
*/

/* ______________________________ SCRIPT ______________________________ */
var scriptAlert = "Language Change says";

// MUST UPDATE IF YOU WANT TO CHANGE THE AVAILABLE LANGUAGES
// The available languages, as { text displayed on dialog window : codename }. See notes above for more language codes
var langs = {};
langs['Português (Brasil)'] = 'brazilianPortugueseLanguage';
langs['English (UK)'] = 'ukenglishLanguage';
langs['English (USA)'] = 'englishLanguage';

if(app.documents.length>0){ // the script will be executed only if an active document exists.

var imgName = activeDocument.name;
var extreme= imgName.substr(imgName.length - 5, 5);
var dot = extreme.lastIndexOf(".");

if(dot == -1) { // Checks if the active document was saved or not.
alert("Please, save your document!", scriptAlert, false);
} else {
main(); // if the active document was saved, go ahead!
}
}


function createWindow() { // Creates a panel asking for input
var isDone, win, windowResource, radio_group, cancel, rb, i;

//sentinel variable
isDone = false;
cancel = false;

// Build dialog radio buttons
i = 0;
for( var k in langs ) {
if( i==0 )
rb = "rb"+i+": RadioButton { text: '"+k+"', value: 1 }, \ ";
else
rb += "rb"+i+": RadioButton { text: '"+k+"' }, \ ";
i++;
}

windowResource = "palette { \
orientation: 'column', \
alignChildren: ['fill', 'top'], \
preferredSize:[300, 130], \
text: 'Change Spell Check Language', \
margins:15, \
\
optPanel: Panel { \
orientation: 'row', \
alignChildren: 'left', \
margins:15, \
text: ' Select the desired dictionary language for the select text ', \ "+ rb + " \
} \
\
bottomGroup: Group{ \
cancelButton: Button { text: 'Cancel', properties:{name:'cancel'}, size: [120,24], alignment:['right', 'center'] }, \
applyButton: Button { text: 'Ok', properties:{name:'ok'}, size: [120,24], alignment:['right', 'center'], active: true }, \
}\
}";
win = new Window(windowResource);

radio_group = win.optPanel; // set group

// Match options with language
function selected_rbutton (rbuttons)
{
for (var i = 0; i < rbuttons.children.length; i++)
if (rbuttons.children[i].value == true)
return langs[rbuttons.children[i].text];
}

win.bottomGroup.cancelButton.onClick = function() {
win.close();
return cancel = true;
};
// Button listeners
win.bottomGroup.applyButton.onClick = function() {
return isDone = true;
};
// don't forget this one!
win.onClose = function() {
return cancel = true;
};

win.addEventListener("keydown", function (k)
{
if (k.keyName == 'Enter')
return isDone = true;
});

win.show();

while (isDone === false) {
try {
app.refresh();
} catch(err) { // Esc cancels without errors
return false;
}
}

return cancel ? false : selected_rbutton (radio_group); // return value, or false if cancelled
}


function getSelectedLayersIdx(){ // Gets array of indexes of selected layers
var selectedLayers = new Array;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
var desc = executeActionGet(ref);
if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ) {
desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
var c = desc.count
var selectedLayers = new Array();
for(var i=0;i<c;i++) {
try{
activeDocument.backgroundLayer;
selectedLayers.push( desc.getReference( i ).getIndex() );
} catch(e) {
selectedLayers.push( desc.getReference( i ).getIndex()+1 );
}
}
} else {
var ref = new ActionReference();
ref.putProperty( charIDToTypeID('Prpr') , charIDToTypeID( 'ItmI' ));
ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
try {
activeDocument.backgroundLayer;
selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( 'ItmI' ))-1);
} catch(e) {
selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( 'ItmI' )));
}
}
return selectedLayers;
}


function selectLayerByIndex( selectedLayers, add ){ // Selects layers by index. If fed an array + true add, selects them, otherwise provide a single value so it selects a single layer
if (typeof(add)==='undefined') add = false;

if ( add ) // If Add it's an array
for(var a in selectedLayers){
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), Number(selectedLayers[a]) );
desc.putReference( charIDToTypeID( "null" ), ref );
desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
var idMkVs = charIDToTypeID( "MkVs" );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );

executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
}
else // Else I just need to make this layer active, it's a number
{
var desc = new ActionDescriptor();
var ref = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
ref.putIndex( charIDToTypeID( "Lyr " ), selectedLayers );
desc.putReference( charIDToTypeID( "null" ), ref );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
}
}


function setLangByIndex( idx, lang ){ // Sets the dictionary language for a text layer based on its index
// Make layer active
selectLayerByIndex( idx );

// And do tiny override
var textObj = new ActionDescriptor();
var target = new ActionReference();
target.putProperty( charIDToTypeID( "Prpr" ), charIDToTypeID( "TxtS" ) ); // desired property: text style
target.putEnumerated( charIDToTypeID( "TxLr" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) ); // the active layer
textObj.putReference( charIDToTypeID( "null" ), target );
var textStyle = new ActionDescriptor();
textStyle.putInteger( stringIDToTypeID( "textOverrideFeatureName" ), 808466225 );
textStyle.putInteger( stringIDToTypeID( "typeStyleOperationType" ), 3 );
textStyle.putEnumerated( stringIDToTypeID( "textLanguage" ), stringIDToTypeID( "textLanguage" ), stringIDToTypeID( lang ) );

textObj.putObject( charIDToTypeID( "T " ), charIDToTypeID( "TxtS" ), textStyle ); // to (end); text style, desc.
executeAction( charIDToTypeID( "setd" ), textObj, DialogModes.NO ); // execute
}


////////////////////////////////////
// Main function
function main() {
// Create window with language selection and retrieve selected value
var lang = createWindow();

if ( !lang ) // If cancelled please stop
return;

// Retrieve selected layers
var selectedLayers = getSelectedLayersIdx();

// Look at each entry and change language when possible; otherwise skip to next item
for(var k in selectedLayers){
try {
setLangByIndex(Number(selectedLayers[k]), lang);
} catch(err) {
continue;
}
}
selectLayerByIndex( selectedLayers, true );
}

关于javascript - 使用 photoshop 脚本更改选定的文本图层语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30632255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com