Skip to main content
Known Participant
March 23, 2022
Answered

illustrator script to convert the base64 into image

  • March 23, 2022
  • 1 reply
  • 4683 views

Hi All,

 

Is it poosible to convert the base64 code(from json) into image(png/jpg) using script? And need to save the converted images into local desktop.

 

Thanks

This topic has been closed for replies.
Correct answer Inventsable
Please download the attached file to view this post

1 reply

Mylenium
Legend
March 23, 2022

You could probably decode it inside a CEP panel, but there it doesn't do you any good. That's probably a taks better suited to a web browser with a respective JS library that simply renders the image as a web page from where it can be saved as an image.

 

Mylenium

Rocky@Author
Known Participant
March 23, 2022

Thanks Mylenium for the update. Could you please share any code to decode in cep panel or web browser? 


 

Inventsable
Legend
March 26, 2022

Thank you so much Inventsable. Its working perfect.


No worries. Just an FYI, I'd added a few functions that I thought you might find useful, like reverse PNG > base64 conversion so you can handle the data both ways.

 

Technically you can programmatically control this from a script or JSX, but it becomes a bit more complicated. You could use what's called an "Invisible Extension" (read here) to have a CEP panel that is headless or contains no UI and is automatically run when the application starts. If this panel contains code such as:

// Must include CSInterface.js as a preloaded script in your HTML:
// https://github.com/Adobe-CEP/CEP-Resources/blob/master/CEP_11.x/CSInterface.js
// The below must be run after CSInterface.js is loaded, like in the custom logic / <body> tag
let CSI = new CSInterface();
CSI.addEventListener("myScriptMessage", (msg) => {
  if (msg.data && msg.data !== "undefined") alert(msg.data);
});

 

Then we can actually have a script send it a message directly and invoke the above listener function at any point. If this msg.data were base64 text or a JSON.stringified object containing the base64 rawdata and a filepath to write to, etc, you could trigger the function fully remotely. In JSX/scripting you'd do this in order to trigger the above in the CEP panel:

function JSXEvent(payload, eventType) {
  try {
    var xLib = new ExternalObject("lib:PlugPlugExternalObject");
  } catch (e) {}
  if (xLib) {
    var eventObj = new CSXSEvent();
    eventObj.type = eventType;
    eventObj.data = payload;
    eventObj.dispatch();
  }
  return;
}

// And then to send some data to our CEP panel:
JSXEvent("Hello world from JSX", "myScriptMessage");

 

You could make this much more robust by adding a JSON polyfill in Adobe scripting, then sending all the data you'd need in the first argument for JSXEvent stringified, then parse it in CEP and potentially have your base64 rawdata and filepath to write to all in a single call without needing any UI from CEP at all. Unfortunately I'm not clever enough to know how to convert from base64 > PNG in pure JSX, but you could still trigger it from JSX if needed, then evalScript a callback function in JSX to indicate you're done. I don't know the scope of what you're doing but presumably if you originally wanted to do it in scripting it could still be helpful to know.