Skip to main content
Inspiring
May 21, 2024
Answered

extend script convert to apple script

  • May 21, 2024
  • 9 replies
  • 1189 views

Hello,  could help me to convert below extend script to apple script, thanks.

 

Convert path of PDF to artboard.

 

#target illustrator

app.preferences.setBooleanPreference("ShowExternalJSXWarning", false);
var lines = {"x", "y", "g"}; 
var oldPdfName = lines [0];
var newPdfName = lines [1];
var folderPath = lines [2];
app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS; 
var pdfOptions = app.preferences.PDFFileOptions;
pdfOptions.pageRangeToOpen = "all"; 
pdfOptions.placeAsLinks = false;
var idoc = app.open(File(folderPath + "/" + oldPdfName), DocumentColorSpace.CMYK); 
app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS;

function getColor(pathItem) {
var isRightColor;
if (pathItem.strokeColor instanceof SpotColor) { 
var strokeColorName = pathItem.strokeColor.spot.name;
if (strokeColorName === "Diecut") {
isRightColor = "macColor"; 
}
else if(strokeColorName === "PFLW"){
isRightColor = "newPcColor"; 
}
}
return isRightColor;
}

var doc = app.activeDocument;
var artb = doc.artboards;
var artLayer = doc.layers[0];
var boardCount = artb.length; 
var glen = artLayer.groupItems; 


var pathColor;
var firstTopGroup = artLayer.groupItems[glen.length-1]; 
var firstGroupPaths;
if(boardCount === 1){ 
firstGroupPaths = artLayer.pathItems;
}else{
var firstClipGroup = firstTopGroup.groupItems[0]; 
firstGroupPaths = firstClipGroup.pathItems; 
}

for (var i = firstGroupPaths.length-1; i >=0; i--) {
var firstGroupPathItem = firstGroupPaths[i];
if (getColor(firstGroupPathItem ) === "macColor"){
pathColor = "macColor";
break;
} else if (getColor(firstGroupPathItem ) === "newPcColor"){
pathColor = "newPcColor";
break;
}
}
for(var u = glen.length-1; u>=0; u--){
var topGroup = artLayer.groupItems[u]; 
var paths;
if(boardCount === 1){ 
paths = artLayer.pathItems;
}else{
var clipGroup = topGroup.groupItems[0]; 
paths = clipGroup.pathItems; 
}

var whiteRectangles = [];
for (var i = paths.length-1; i >=0; i--) {
var pathItem = paths[i];
if (pathColor === "macColor"){
if(getColor(pathItem) === "macColor"){
if(pathItem.pathPoints.length === 4){
whiteRectangles.push(pathItem);
}else{
pathItem.remove();
}
}
}
else if(pathColor === "newPcColor"){
if(getColor(pathItem) === "newPcColor"){whiteRectangles.push(pathItem);}
}
else if(typeof pathColor === 'undefined'){
if(pathItem.pathPoints.length === 4){
if(pathItem.strokeColor.cyan == 0 && pathItem.strokeColor.magenta == 0 && pathItem.strokeColor.yellow == 0 && pathItem.strokeColor.black == 0){
whiteRectangles.push(pathItem);
}
}
}
}

if(whiteRectangles.length > 0){
for (var a=0; a<whiteRectangles.length; a++) {
whiteRectangles[a].selected = true;
var pathBds = whiteRectangles[a].geometricBounds;
whiteRectangles[a].remove();
artb.add(pathBds);
}}

}

for(var j=0; j<boardCount; j++){
artb[0].remove ();
}

function saveAsPDF() {
var pdfFile = new File(folderPath + "/" + newPdfName + ".pdf");
var pdfOptions = new PDFSaveOptions();
pdfOptions.compatibility = PDFCompatibility.ACROBAT5;
pdfOptions.generateThumbnails = true; 
pdfOptions.preserveEditability = true;
pdfOptions.preset = "[High Quality Print]"; //High Quality Print, Smallest File Size
app.activeDocument.saveAs(pdfFile, pdfOptions);
}
saveAsPDF();
doc.close();

 

This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer hhas01
quote

Hello,  could help me to convert below extend script to apple script, thanks.

 

Why? You can run your existing ExtendScript from AppleScript, e.g.:

 

test.jsx

alert('Hello ' + arguments[0])

 

test.scpt

property scriptPath : "/path/to/test.jsx" as POSIX file -- your path here

tell application "Adobe Illustrator"
  activate
  do javascript scriptPath with arguments {"Bob"}
end tell

 

9 replies

hhas01Correct answer
Inspiring
May 21, 2024
quote

Hello,  could help me to convert below extend script to apple script, thanks.

 

Why? You can run your existing ExtendScript from AppleScript, e.g.:

 

test.jsx

alert('Hello ' + arguments[0])

 

test.scpt

property scriptPath : "/path/to/test.jsx" as POSIX file -- your path here

tell application "Adobe Illustrator"
  activate
  do javascript scriptPath with arguments {"Bob"}
end tell

 

rui huangAuthor
Inspiring
May 21, 2024

You've been a great help. Thank you for sharing your knowledge and solving my problem.