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

how to speed up a script in javascript, bridgeTalk

Explorer ,
Aug 03, 2023 Aug 03, 2023

Hello.
Sorry in advance for my english everything is translated by google translator.
I wrote a simplified script to solve this problem. The script works, but I have the impression that changes in illustrator, i.e. changing the width, work too slowly. Sure, this change could work much faster, but that's where my knowledge ends. I need help in this subject.
Thank you in advance!

 

#targetengine main;

var mm = 2.834645;
var doc = app.activeDocument;
var rec = doc.pathItems.rectangle(0,0,100*mm,100*mm);

// PAlETTE
var palette = new Window("palette");
palette.text = "rectangle";
palette.alignChildren = ["center","top"];
palette.spacing = 10;
palette.margins = 16;

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

var statictext1 = group1.add("statictext", undefined, undefined, {name: "statictext1"});
statictext1.text = "W:";

var statictext2 = group1.add("statictext", undefined, undefined, {name: "statictext2"});
statictext2.text = rec.width/mm;

var button1 = group1.add("button", undefined, undefined, {name: "button1"});
button1.text = "-";
button1.preferredSize.width = 50;

var button2 = group1.add("button", undefined, undefined, {name: "button2"});
button2.text = "+";
button2.preferredSize.width = 50;

// onClick
button1.onClick = function() {
statictext2.text = Number(statictext2.text)-1;
fun1();
};

button2.onClick = function() {
statictext2.text = Number(statictext2.text)+1;
fun2();
};

palette.show();

// FUNCTION
function fun1() {
var bt = new BridgeTalk();
bt.target = "illustrator";
var message = "rec.width = rec.width - (1*mm);";
bt.body = message;
bt.send();
};

function fun2() {
var bt = new BridgeTalk();
bt.target = "illustrator";
var message = "rec.width = rec.width + (1*mm);";
bt.body = message;
bt.send();
};
TOPICS
How-to , Performance , Scripting
593
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

Guide , Aug 03, 2023 Aug 03, 2023

Mouse events are limited in Illustrator scripting.  You could try the up and down arrow keys:

var width = 100;
var mm = new UnitValue(1 + " " + "mm").as("pt");
var doc = app.activeDocument;
var rec = doc.pathItems.rectangle(0, 0, width * mm, 100 * mm);
app.redraw();

var w = new Window("dialog");
var g = w.add("group");
var st = g.add("statictext", undefined, "W");
    st.preferredSize.width = 100;
    st.justify = "center";
var et = g.add("edittext", undefined, width);
    et.preferredSize.width
...
Translate
Adobe
Advocate ,
Aug 03, 2023 Aug 03, 2023

Bonjour,

Je ne sais pas comment aller plus vite (maintenir le bouton?)

Mais le résultat de la largeur ne me semble pas correct, vous cumulez les erreurs d'imprécision.

rec.width = rec.width + (1*mm)

115.999 donne 117.122

Je propose:

 

 

 

// JavaScript Document
#targetengine main;
var dep = 100;
// var mm = 2.834645;
var mm = new UnitValue(1 + " " + "mm").as('pt');

var doc = app.activeDocument;
var rec = doc.pathItems.rectangle(0,0,dep*mm,dep*mm);
//rec.selected = true;

// PAlETTE
var palette = new Window("palette");
palette.text = "rectangle";
palette.alignChildren = ["center","top"];
palette.spacing = 10;
palette.margins = 16;

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

var statictext1 = group1.add("statictext", undefined, undefined, {name: "statictext1"});
statictext1.text = "W:";

var statictext2 = group1.add("statictext", undefined, undefined, {name: "statictext2"});
statictext2.text = dep;

var button1 = group1.add("button", undefined, undefined, {name: "button1"});
button1.text = "-";
button1.preferredSize.width = 50;

var button2 = group1.add("button", undefined, undefined, {name: "button2"});
button2.text = "+";
button2.preferredSize.width = 50;

// onClick
button1.onClick = function() {
dep--;
statictext2.text = dep;
fun();
};

button2.onClick = function() {
dep++;
statictext2.text = dep;
fun();
};

palette.show();

// FUNCTION
function fun() {
var bt = new BridgeTalk();
bt.target = "illustrator";
var message = "rec.width = dep*mm;";
bt.body = message;
bt.send();
};

 

René

 

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
Guide ,
Aug 03, 2023 Aug 03, 2023

Mouse events are limited in Illustrator scripting.  You could try the up and down arrow keys:

var width = 100;
var mm = new UnitValue(1 + " " + "mm").as("pt");
var doc = app.activeDocument;
var rec = doc.pathItems.rectangle(0, 0, width * mm, 100 * mm);
app.redraw();

var w = new Window("dialog");
var g = w.add("group");
var st = g.add("statictext", undefined, "W");
    st.preferredSize.width = 100;
    st.justify = "center";
var et = g.add("edittext", undefined, width);
    et.preferredSize.width = 100;
    et.justify = "center";
    et.active = true;
    et.addEventListener("keydown", function (e) {
        f(e, et);
    });
var b = w.add("button", undefined, "OK");
w.show();

function f(e, et) {
    if (e.keyName == "Up") {
        et.text = Number(et.text) + 1;
        rec.width = et.text * mm;
    }
    if (e.keyName == "Down") {
        et.text = Number(et.text) - 1;
        rec.width = et.text * mm;
    }
    redraw();
}
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 ,
Aug 03, 2023 Aug 03, 2023

Thank you gentlemen for your answers.

The code on the arrows actually runs fast, as does scaling with preview in illustrator (response is immediate). Bridgetalk seems to be slowing things down. Both scripts helped me, now I need to analyze them and implement them in my project

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 ,
Aug 04, 2023 Aug 04, 2023
LATEST

In a "dialog" type window and redraw() the width changes are instantaneous, that's what I wanted. In a "palette" window, you probably have to send the code over bridgeTalk, and that slows things down.

 

var dep = 100;
var mm = new UnitValue(1 + " " + "mm").as('pt');

var doc = app.activeDocument;
var rec = doc.pathItems.rectangle(0,0,dep*mm,dep*mm);
app.redraw();

var p = new Window("dialog"); 
    p.text = "rectangle";
    p.alignChildren = ["center","top"]; 
    p.spacing = 10; 
    p.margins = 16; 

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

var st1 = g1.add("statictext", undefined, undefined, {name: "st1"}); 
    st1.text = "W:";

var st2 = g1.add("statictext", undefined, undefined, {name: "st2"}); 
    st2.text = dep;

var b1 = g1.add("button", undefined, undefined, {name: "b1"}); 
    b1.text = "-"; 
    b1.preferredSize.width = 50; 

var b2 = g1.add("button", undefined, undefined, {name: "b2"}); 
    b2.text = "+"; 
    b2.preferredSize.width = 50;

b1.onClick = function() {
    dep--;
    st2.text = dep;
    rec.width = dep*mm;
    redraw();
};

b2.onClick = function() {
    dep++;
    st2.text = dep;
    rec.width = dep*mm;
    redraw();
};

p.show();

 

 

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