You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无需关闭GUI,用脚本实时更新Adobe Illustrator内容的方法

Adobe Illustrator脚本实现GUI实时预览文本修改的方案

你的问题根源在于使用了模态对话框(Window("dialog")),这类对话框会阻塞Illustrator主程序的UI刷新流程,导致修改操作必须等对话框关闭后才会显示。要实现实时预览,只需做两处关键调整:

  1. 给输入控件添加实时触发的事件监听,在用户修改内容时立即执行文本更新
  2. 每次修改完成后调用app.activeDocument.redraw()强制刷新画布视图

修改后的完整代码

function startGUI() {
    // DIALOG
    // ======
    var dialog = new Window("dialog");
    dialog.text = "Dialog";
    dialog.orientation = "column";
    dialog.alignChildren = ["center","top"];
    dialog.spacing = 10;
    dialog.margins = 16;

    // GRPNAME
    // =======
    var grpName = dialog.add("group", undefined, {name: "grpName"});
    grpName.orientation = "row";
    grpName.alignChildren = ["left","center"];
    grpName.spacing = 10;
    grpName.margins = 0;

    var titleName = grpName.add("statictext", undefined, undefined, {name: "titleName"});
    titleName.text = "Name";
    titleName.preferredSize.width = 64;

    var valName = grpName.add('edittext {properties: {name: "valName"}}');
    valName.text = "Ela Ayah Kayis";
    valName.preferredSize.width = 200;

    // GRPFONTSIZE
    // ===========
    var grpFontSize = dialog.add("group", undefined, {name: "grpFontSize"});
    grpFontSize.preferredSize.width = 250;
    grpFontSize.orientation = "row";
    grpFontSize.alignChildren = ["left","top"];
    grpFontSize.spacing = 10;
    grpFontSize.margins = 0;
    grpFontSize.alignment = ["left","top"];

    var titleFontSize = grpFontSize.add("statictext", undefined, undefined, {name: "titleFontSize"});
    titleFontSize.text = "Font Size";
    titleFontSize.preferredSize.width = 64;

    var valFontSize = grpFontSize.add('edittext {properties: {name: "valFontSize"}}');
    valFontSize.text = "15";
    valFontSize.preferredSize.width = 30;
    valFontSize.alignment = ["center","center"];

    var titleFontSizeUnit = grpFontSize.add("statictext", undefined, undefined, {name: "titleFontSizeUnit"});
    titleFontSizeUnit.text = "pt";
    titleFontSizeUnit.alignment = ["left","center"];

    var sliderFontSize = grpFontSize.add("slider", undefined, undefined, undefined, undefined, {name: "sliderFontSize"});
    sliderFontSize.minvalue = 11;
    sliderFontSize.maxvalue = 15;
    sliderFontSize.value = 15;
    sliderFontSize.preferredSize.width = 135;

    // 实时更新滑块与文本框,并触发画布刷新
    sliderFontSize.onChanging = function(){
        valFontSize.text = Math.round(sliderFontSize.value);
        updateNames(valName.text, Math.round(sliderFontSize.value));
        app.activeDocument.redraw();
    }

    // 文本输入框实时监听修改,触发更新
    valName.onChanging = function(){
        updateNames(valName.text, Math.round(sliderFontSize.value));
        app.activeDocument.redraw();
    }

    // DIALOG
    // ======
    var divider1 = dialog.add("panel", undefined, undefined, {name: "divider1"});
    divider1.alignment = "fill";

    // GROUP BUTTONS
    // ============
    var group1 = dialog.add("group", undefined, {name: "group1"});
    group1.orientation = "row";
    group1.alignChildren = ["right","center"];
    group1.spacing = 10;
    group1.margins = 0;
    group1.alignment = ["center","top"];

    var btnReplaceNames = group1.add("button", undefined, undefined, {name: "btnReplaceNames"});
    btnReplaceNames.text = "Replace Names";
    btnReplaceNames.justify = "left";
    btnReplaceNames.alignment = ["right","bottom"];

    var btnClose = group1.add("button", undefined, undefined, {name: "btnClose"});
    btnClose.text = "Close";

    // Event Listener for the replace button
    btnReplaceNames.onClick = function() {
        var inputText = valName.text;
        updateNames(inputText, Math.round(sliderFontSize.value));
        app.activeDocument.redraw();
    }

    // Event listener for the quit button
    btnClose.onClick = function() {
        dialog.close();
    }

    dialog.show();
}

function updateNames(chosenName, fontSize) {
    var txtFrames = app.activeDocument.textFrames;

    if (txtFrames.length > 0) {
        for ( i = 0; i < txtFrames.length; i++ ) {
            if (txtFrames[i].name == 'Text-Name') {
                txtFrames[i].textRange.characterAttributes.size = fontSize;
                txtFrames[i].textRange.characterAttributes.leading = fontSize;
                txtFrames[i].contents = chosenName;
            }
        }
    }
}

startGUI();

关键修改说明

  • 给valName(文本输入框)添加onChanging事件,用户输入时立即更新文本内容并刷新画布
  • 修改sliderFontSize的onChanging事件,在更新字号输入框后,同步执行文本更新并刷新画布
  • 在所有触发修改的操作后(按钮点击、实时输入),调用app.activeDocument.redraw()强制Illustrator刷新视图

内容的提问来源于stack exchange,提问作者Erg10000

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 16:55:16