Skip to main content
Participant
July 20, 2023
Question

Calling External API

  • July 20, 2023
  • 1 reply
  • 775 views

Is it possible to call External API in adobe illustrator scripting ?

app.preferences.setBooleanPreference("ShowExternalJSXWarning", false);

run();

function run() {

    // Creating Our XMLHttpRequest object
    const xhr = new XMLHttpRequest();
 
    // // Making our connection 
    var url = "https://jsonplaceholder.typicode.com/todos/1";
    xhr.open("GET", url, true);
 
    // // function execute after request is successful
    xhr.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            console.log(this.responseText);
        }
    }
    // // Sending our request
    xhr.send();
	

// Set the path to your AI file
var filePath = "C://Projects//IllustratorPacking//Labels//Source/AAB.ai";

// Set the text to find and replace
var searchText = "EMAIL";
var replaceText = this.responseText.title;

// Open the AI file
var doc = app.open(new File(filePath));

// Iterate through all text items in the document
for (var i = 0; i < doc.textFrames.length; i++) {
  var textFrame = doc.textFrames[i];

  // Check if the text contains the search text
  if (textFrame.contents.indexOf(searchText) !== -1) {
    // Replace the text
    textFrame.contents = textFrame.contents.replace(searchText, replaceText);
  }
}

// Save and close the modified AI file
 doc.save();
 doc.close();
}
This topic has been closed for replies.

1 reply

Community Expert
July 21, 2023

Not readily possible by just using Extendscript. However there are some ways you could try.

  • Create a CEP extension and make the call using the JS part and do the Illustrator DOM manipulation using the JSX script. https://github.com/Adobe-CEP/CEP-Resources
  • Extendscript has a socket object, you will need to use it and program it to handle HTTP requests. A complex job.
  • Create a C++ library which provide methods exposed to extendscript which can make the HTTP call.
  • Execute a batch/command file from jsx and make the call from the batch/command file. The result transfer may be done via something like a text file

-Manan

-Manan
Participant
July 21, 2023

Is this possible with other scripting languages like VB script and Apple script? 

Community Expert
July 22, 2023

Yes it is, as mentioned in my last point. Any executable file can be executed using the execute method of the File object. So write your code in the executable file like a .command file on a MAC and call the execute method on it from Extendscript. The only thing for you to figure out is how to exchange the data back and forth

-Manan

-Manan