Copy link to clipboard
Copied
Hi,
Using scripting, I need to know which version of Illustrator created an ai or pdf-file.
In this community I found the code below (I tweaked it a bit)...
var myPath = "/Users/me/Desktop/artwork.pdf";
var myFile = new File(myPath);
var docVersion = getDocumentVersion(myFile);
var appVersion = getApplicationVersion();
function getApplicationVersion(){
var resultArr = app.version.split(".");
return (resultArr[0]);
}
function getDocumentVersion(_myFile){
var vrs = null;
_myFile.open("r");
try{
while(_myFile.tell() < _myFile.length) {
var line = _myFile.readln();
var idxOf = line.indexOf("Creator: Adobe Illustrator(R)");
if(idxOf > 0) {
vrs = line.substring(idxOf+30, line.length);
}
}
}
catch(ex) {}
finally { _myFile.close(); }
if(vrs != null){
var vrsArr = vrs.split(".");
vrs = vrsArr[0];
}
return vrs;
}
Unfortunately, the version number does not match the application version.
The value returned is 24, but the app version is 27 (CC2023).
How can I fix this?
Thanx
Yes, that is correct. Here's a revision on @tmmls's original `getDocumentVersion()` that just reads the raw file data and extracts the correct version string from the `<xmp:CreatorTool>` tag without opening the file in Ai.
function getDocumentVersion(_myFile) {
var vrsString = null;
var vrs = null;
_myFile.open("r");
try {
while (_myFile.tell() < _myFile.length) {
var line = _myFile.readln();
if (line.indexOf("<xmp:CreatorTool>") >= 0) {
vrsString = line;
...
Copy link to clipboard
Copied
Here's a way to grab the info using the document's XMP data. I tried it with a handful of different file types and it seems to work pretty well, Take it for a spin and let me know if you have any issues. Cheers!
var ver = getIllustratorVersion();
/**
* Extract the Adobe Illustrator version from a documents XMP Data.
*/
function getIllustratorVersion() {
// Set up some variables
var xmp, xmpString, aiVersion, splitString;
// Load the XMP library as an ExtendScript ExternalObject via the docs
// https://extendscript.docsforadobe.dev/scripting-xmp/index.html
if (ExternalObject.AdobeXMPScript == undefined)
ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
//Read XMP string - You can see document XMP info in Illustrator @ File > File Info > Raw Data
var xmp = new XMPMeta(app.activeDocument.XMPString);
// Grab the CreatorTool property
try {
var xmpString = xmp.getProperty(XMPConst.NS_XMP, "xmp:CreatorTool").value;
// Should give you something like 'Adobe Illustrator 27.2 (Windows)'
// or `Adobe Illustrator CC 22.0 (Macintosh)`
} catch (e) {
alert("Creator Tool Not Found!\n" + e);
return -1;
}
// If file was created by an app other than Illustrator
if (xmpString.toLowerCase().indexOf("illustrator") < 0) {
alert("File not created by Adobe Illustrator.");
return -1;
}
// Parse out the actual version number in the string
// It's more work than it should be because of inconsistent version naming
splitString = xmpString.split(" ");
for (var i = 0; i < splitString.length; i++) {
aiVersion = Number(splitString[i]);
if (aiVersion) break;
}
// Show the results for debugging
alert("Ai Version Info\nXMP String: " + xmpString + "\nParsed Version: " + aiVersion);
return aiVersion;
}
Copy link to clipboard
Copied
However, it requires an open document to read XMP. When we process a folder of files, we need to write the opening and closing of each file =(
Copy link to clipboard
Copied
Yes, that is correct. Here's a revision on @tmmls's original `getDocumentVersion()` that just reads the raw file data and extracts the correct version string from the `<xmp:CreatorTool>` tag without opening the file in Ai.
function getDocumentVersion(_myFile) {
var vrsString = null;
var vrs = null;
_myFile.open("r");
try {
while (_myFile.tell() < _myFile.length) {
var line = _myFile.readln();
if (line.indexOf("<xmp:CreatorTool>") >= 0) {
vrsString = line;
break;
}
}
} catch (ex) {
} finally {
_myFile.close();
}
if (vrsString != null) {
var splitString = vrsString.split(" ");
for (var i = 0; i < splitString.length; i++) {
if (!isNaN(splitString[i])) vrs = splitString[i];
}
}
return vrs;
}
Copy link to clipboard
Copied
Thank you very much for the great response...