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

JXA and Illustrator?

LEGEND ,
Jun 14, 2019 Jun 14, 2019

Does anyone use Javascript for Automation (JXA) with Illustrator? I'd like to try it because I can pass variables between applications using JXA. I'm especially interested in directly accessing Keyboard Maestro variables when running scripts in Illustrator.

TOPICS
Scripting
2.4K
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

Enthusiast , Jun 28, 2019 Jun 28, 2019

run is a special function for JXA. When there is a function run, do not write the process directly outside of it.

What you should write is the following code:

function run() {

  setContents('dates') ;

  setContents('Name') ;

}

function setContents(myText) {

  // get contents of variable from Keyboard Maestro

  const kmEngine = Application('Keyboard Maestro Engine') ;

  varText = kmEngine.getvariable(myText) ;

     

  // set contents of selected text frame to variable contents

  const aiApp = Application('

...
Translate
Adobe
Community Expert ,
Jun 15, 2019 Jun 15, 2019

Here is a little snippet.

var app = Application("Illustrator");

var doc = app.documents[0];

var pth = doc.pathItems[0];

var pt = nwAnchor(

  pth.pathPoints[0].anchor(),

  pth.pathPoints[0].rightDirection(),

  pth.pathPoints[1].leftDirection(),

  pth.pathPoints[1].anchor(),

  0.5);

console.log(pt);

function nwAnchor (p0,p1,p2,p3,t) {

  var spPt = new Array();

  for (var i=0;i<2;i++){

  spPt = (Math.pow ((1 - t), 3)) * p0

  + 3 * (Math.pow((1-t), 2)) * t * p1

  + 3 * (Math.pow(t, 2)) * (1-t) * p2

  + Math.pow(t, 3) * p3;

  }

  return spPt;

  }

スクリーンショット 2019-06-15 16.00.01.png

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
Enthusiast ,
Jun 15, 2019 Jun 15, 2019

To get Keyboard Maestro's variables:

function run() {

  // get contents of variable from Keyboard Maestro

  const kmEngine = Application('Keyboard Maestro Engine') ;

  const varContents = kmEngine.getvariable('your_variable') ;

 

  // set contents of selected text frame to variable contents

  const aiApp = Application('Adobe Illustrator') ;

  const doc = aiApp.documents[0] ;

  const sel = doc.selection()[0] ;

  sel.contents = varContents ;

}

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
LEGEND ,
Jun 27, 2019 Jun 27, 2019

Thanks to both of you for your help. Sorry, I went on vacation just after posting this question.

sttk3​, I've been experimenting with your code and it's working for me. I am getting an error though on line 4 ("Can't convert types"), although the script still works. The error is not displayed when run from Keyboard Maestro, only Script Editor. How can I eliminate this error?

function run(myText) {

  // get contents of variable from Keyboard Maestro

  const kmEngine = Application('Keyboard Maestro Engine') ;

  const varText = kmEngine.getvariable(myText) ;

   

  // set contents of selected text frame to variable contents

  const aiApp = Application('Adobe Illustrator') ;

  const doc = aiApp.documents[0] ;

  const theFrame = doc.textFrames[myText] ;

  theFrame.contents = varText ;

}

run("dates");

run("Name");

"Name" and "dates" are the KM variable names AND the names of the targeted AI text frames. Don't know if that's a good idea.

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
Enthusiast ,
Jun 28, 2019 Jun 28, 2019

run is a special function for JXA. When there is a function run, do not write the process directly outside of it.

What you should write is the following code:

function run() {

  setContents('dates') ;

  setContents('Name') ;

}

function setContents(myText) {

  // get contents of variable from Keyboard Maestro

  const kmEngine = Application('Keyboard Maestro Engine') ;

  varText = kmEngine.getvariable(myText) ;

     

  // set contents of selected text frame to variable contents

  const aiApp = Application('Adobe Illustrator') ;

  const doc = aiApp.documents[0] ;

  const theFrame = doc.textFrames[myText] ;

  theFrame.contents = varText ;

}

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
LEGEND ,
Jun 28, 2019 Jun 28, 2019

sttk3​, this is very helpful information. Thank you. Can you recommend a training resource for JXA?

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
Enthusiast ,
Jun 29, 2019 Jun 29, 2019

The official document is most useful.

Introduction to JavaScript for Automation Release Notes

Besides, the following web site is also convenient.

Home · JXA-Cookbook/JXA-Cookbook Wiki · GitHub

jxa@apple-dev.groups.io | Wiki

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
Engaged ,
Jul 02, 2019 Jul 02, 2019

I wouldn’t waste time on JXA. It’s flawed, buggy, and effectively unsupported (it bombed so hard, Apple disbanded the team responsible). It works up to a point, but try doing something non-trivial and it breaks, you’re stuffed. Same goes for Apple’s Scripting Bridge before it. Doubly so when dealing with older Carbon-based apps.

You’re best sticking to Adobe’s built-in JavaScript and/or AppleScript; neither is perfect, but at least they work and have documentation and user communities worth a damn. If you _really_ want to use JavaScript + Apple events then node.js + nodeautomation should work okay, but caveat emptor as I don’t provide support.

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 Beginner ,
Jul 02, 2019 Jul 02, 2019

Sounds like you’ve been around the block. Do you have experience with Keyboard Maestro? I’m using it to automat AI, PS and Chrome. Works well. Just trying to understand all the options.

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
Engaged ,
Jul 03, 2019 Jul 03, 2019
LATEST

Not something I use. AppleScript’s the obvious choice given KM’s support for it, although there are also folks doing work with Adobe’s CEP if you can tie KM into Node.

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