简单的javascript循环

时间:2011-11-02 13:04:10

标签: javascript loops input extendscript

这是Illustrator的extendscript,它基本上是javascript。我希望第二个弹出窗口只显示一次,我试图告诉它变量是否已经存在但不是这样做,但是如果它不要求输入。知道我做错了什么吗?

#target illustrator
if ( app.documents.length > 0 ) {
    var replaceThis = prompt('What font do you want to replace?','')
    for ( i = 0; i< app.activeDocument.textFrames.length; i++) { //loop through the layers
        var textArtRange = app.activeDocument.textFrames[i].textRange;
        var fontSize = textArtRange.characterAttributes.size;
        //var replaceThis = "10";
        //alert("replace this:" + replaceThis);
       // alert("current font size" + fontSize);
        if (fontSize == replaceThis) {
             Replacefont();
         }
        function Replacefont () {
                //var newSize = "90";
                if (!newSize) {
                    var newSize = prompt('Replace '+ replaceThis +'pt with:','')
                }
                textArtRange.characterAttributes.size = newSize;
                alert("yay");
        }
    }
}

1 个答案:

答案 0 :(得分:1)

newSize仅在ReplaceFont内可用,并且每次函数结束时都会被丢弃。你需要让它像这样持久(没有真正的理由去做一个单独的函数)。另请注意,var循环不要忘记for

#target illustrator
if ( app.documents.length > 0 ) {
    var replaceThis = prompt('What font do you want to replace?','');
    var newSize = prompt('Replace '+ replaceThis +'pt with:','');

    for ( var i = 0; i < app.activeDocument.textFrames.length; i++) {
        var textArtRange = app.activeDocument.textFrames[i].textRange;
        var fontSize = textArtRange.characterAttributes.size;

        if (fontSize == replaceThis) {
            textArtRange.characterAttributes.size = newSize;
            alert("yay");
        }
    }
}