Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Explorer ,
Jan 05, 2025 Jan 05, 2025

 

/*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();

 

TOPICS
Scripting
509
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 05, 2025 Jan 05, 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");
}
Translate
Adobe
Community Expert ,
Jan 05, 2025 Jan 05, 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");
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 29, 2025 Jan 29, 2025

It worked

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 29, 2025 Jan 29, 2025
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines