簡體   English   中英

Photoshop腳本:更改文本圖層的文本

[英]Photoshop scripting: changing text of a text layer

因為我沒有足夠的時間來學習有關PS-Scripting的所有知識,所以我想知道,如果你能幫助我的話。

這很簡單。 我想要一個JS-Script,它可以改變頂層的文本。 例如:Text為“#005”,腳本應加1,因此顯示“#006”。 之后,它應該使用當前編號(006)導出(另存為Web和設備,透明度為@ 1280x720)文件。

這是一個層的屏幕( omg在德國!11 ): imageshack.us/photo/my-images/706/helpal.png

為downvoters編輯:

為了幫助社區並避免誤導/錯誤信息(如果我在這種情況下做出任何信息),從而使StackOverflow成為一個更好的地方,請在下面添加一條評論,說明是什么讓您認為代碼或方向值得投降。 如果有任何錯誤或誤導,我將再學習一件事,我會感激不盡。

首先,您需要創建一個動作。

  1. 使用.jsx擴展名保存以下代碼。
  2. 打開其中一張圖片
  3. 創建一個新操作並按下面板下方的記錄按鈕(如果它尚未激活)
  4. 轉到File > Scripts > Browse然后選擇該腳本
  5. 停止動作錄制
  6. 轉到創建文件的文件夾並刪除該新文件

然后你需要自動完成所有這些。 沒有打開的文件

  1. 轉到File > Automate > Batch
  2. 從選項中選擇必要的“設置”和“操作”名稱
  3. 對於“Source”,選擇“Folder”,然后通過單擊“Choose ...”按鈕選擇包含分層文件的文件夾
  4. 這可能沒有必要(取決於您的顏色設置),但您可以選擇第3和第4個選項: Suppress File Open Options DialogsSuppress Color Profile Warnings 由於在錄制時您未包括打開文件的操作, 因此請取消選擇第一個選項“ Override Action Open Commands 否則它將不會打開任何文件,但仍然會嘗試執行文件的腳本*編號。 如有必要,請選擇第二個選項Include All Subfolders
  5. 單擊“確定”按鈕。

使用CS6的人的另一點: Adobe Developer Connection表明......

Adobe Photoshop CS6不安裝Scripting文件夾。 請使用以下鏈接手動安裝Samples,Documentation和Scripting Listener插件。

function getTextLayer(target) {
// this basically loops all the layers to find the
// upmost text layer with the content #nn... and returns it
    if (target == null) return false;
    var layers      = target.layers,
        layerLen    = layers.length;
    for (var i = 0; i < layerLen; i++) {
        var layer       = layers[i],
            isLayerSet  = layer.typename == 'LayerSet',
            isValid     = layer.kind == LayerKind.TEXT &&
                          /^\s*#\d+\s*$/.test(layer.textItem.contents);
            // we're allowing spaces around the text just in case
        if (!isLayerSet && !isValid) continue;
        if (isLayerSet) {
            var found = getTextLayer(layer);
            if (found) return found;
        } else return layer;
    }
    return false;
}

var doc;
try {
    doc = app.activeDocument;
    // the front document
} catch(e) {}
var txtLayer = getTextLayer(doc);
// obviously, the text layer if found

if (txtLayer) {
    var num = txtLayer.textItem.contents.match(/\d+/)[0],
    // find the numeric part
        len = num.length,
    // find the length of the numeric part
        num = (parseInt(num,10)+1).toString();
    // add 1 to that number
    while (num.length < len) num = '0' + num;
    // and adjust length if necessary so that e.g.
    // 032 will not become 33 but it will become 033
    txtLayer.textItem.contents = '#' + num;
    // update layer content
    var ext = '.png',
        dir = decodeURI(doc.path) + '/png24',
        // to use the same directory where the layered file exists
        // just keep it as decodeURI(doc.path)
        // I added a folder here, which actually does not exist
        // but it doesn't matter because I'm making it create it
        // below in case there's no such directory.
        fileName = dir + '/' + num + ext,
        i = 0;
    if (!Folder(dir).exists) Folder(dir).create();
    // create the directory if it doesn't exist
    while (File(fileName).exists)
        fileName = dir + '/' + num + '-' + (++i) + ext;
    // if file with that name exists, add -n to the end of the name
    var file = new File(fileName),
        opts = new ExportOptionsSaveForWeb();
    with (opts) {
        format = SaveDocumentType.PNG;
        PNG8 = false;
    }
    doc.exportDocument(file, ExportType.SAVEFORWEB, opts);
    // save for web
}
if (doc) {
    doc.close(SaveOptions.DONOTSAVECHANGES);
    // close the original layered document without saving
}
doc = null;
// remove reference

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM