Skip to main content
enisio1
Known Participant
January 5, 2025
Answered

How can I activate (bring into focus) Illustrator after pressing a Palette button in my AIScript

  • January 5, 2025
  • 1 reply
  • 548 views

 

/*I can already use this illustrator script. My problem is that after the button works, the window title remains active. 
    After the button does its job, the window title should remain passive and focus on the illustrator page. I tried very hard but I couldn't succeed.*/
#target illustrator
#targetengine main 
 
function showPalette() {
    var win = new Window('palette', '270+142mm');
    win.orientation = 'column';
    win.alignChildren = ['left', 'top'];

    var originalSize = [107, 150]; 
    var minimizedSize = [107, 35];

    // Reduce/Enlarge button in the upper left corner
    var toggleButton = win.add('button', undefined, '▼▲', {name: 'toggle'});
    var isMinimized = false; // Initially the window is not small
    toggleButton.onClick = function() {
        if (isMinimized) {
            win.size = originalSize; // Return window to default size
            isMinimized = false;
        } else {
            win.size = minimizedSize; // Minimize the window
            isMinimized = true;
        }
    };

    // Butonları içeren alt grup
    var buttonGroup = win.add('group');
    buttonGroup.orientation = 'column';
    buttonGroup.alignChildren = ['left', 'top'];
    buttonGroup.spacing = 1;

    // 270 mm button
    var button270 = buttonGroup.add('button', undefined, '270 mm', {name: 'help'});
    button270.onClick = function() {
        runScript(270, 'width');
//~         win.close();
    };

    // 142 mm button
    var button142 = buttonGroup.add('button', undefined, '142 mm', {name: 'help'});
       button142.onClick = function() {
        runScript(142, 'height');
//~         win.close();
    };
    win.size = originalSize; 
    win.show();
}

function runScript(size, dimension) {
    var bt = new BridgeTalk();
    bt.target = "illustrator";
    bt.body = "var size = " + size + "; var dimension = '" + dimension + "'; " +
              "(" + scriptToRun.toString() + ")();";
    bt.send();
}

function scriptToRun() {
    var doc = app.activeDocument;
    var sel = doc.selection;

    if (sel.length === 0) {
        alert("select at least one object.");
        return;
    }

    var minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
    for (var i = 0; i < sel.length; i++) {
        var itemBounds = sel[i].visibleBounds;
        minX = Math.min(minX, itemBounds[0]);
        minY = Math.min(minY, itemBounds[1]);
        maxX = Math.max(maxX, itemBounds[2]);
        maxY = Math.max(maxY, itemBounds[3]);
    }

    var currentDimension = (dimension === 'width') ? (maxX - minX) : (minY - maxY);
    var newDimension = size * 2.834645669291339;

    var scaleFactor = (newDimension / currentDimension) * 100;

    var group = doc.groupItems.add();
    for (var j = 0; j < sel.length; j++) {
        sel[j].duplicate(group, ElementPlacement.PLACEATEND);
    }

    group.resize(scaleFactor, scaleFactor, true, true, true, true, scaleFactor, Transformation.CENTER);

    var groupBounds = group.visibleBounds;
    var finalDimension = (dimension === 'width') ? (groupBounds[2] - groupBounds[0]) : (groupBounds[1] - groupBounds[3]);
    var correctionFactor = newDimension / finalDimension * 100;
    group.resize(correctionFactor, correctionFactor, true, true, true, true, correctionFactor, Transformation.CENTER);

    // Adjust the position of the new group to the bottom of the original object
    group.position = [minX, maxY - (groupBounds[1] - groupBounds[3])];
    doc.selection = [group];
}
showPalette();

 

Correct answer CarlosCanto

add this line at the end of your script 

 

    :
    :
    // Adjust the position of the new group to the bottom of the original object
    group.position = [minX, maxY - (groupBounds[1] - groupBounds[3])];
    doc.selection = [group];

    BridgeTalk.bringToFront("illustrator");
}

1 reply

CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
January 6, 2025

add this line at the end of your script 

 

    :
    :
    // Adjust the position of the new group to the bottom of the original object
    group.position = [minX, maxY - (groupBounds[1] - groupBounds[3])];
    doc.selection = [group];

    BridgeTalk.bringToFront("illustrator");
}
enisio1
enisio1Author
Known Participant
January 29, 2025

It worked

CarlosCanto
Community Expert
Community Expert
January 30, 2025

@enisio1 I edited the topic title to try make it a bit clearer