Copy link to clipboard
Copied
Is it possible to place .svg file with script inside Illustrator?
Using .. docR.placedItems.add()...
I am getting error message "'The file "FileName.svg" is in a format which cannot be placed'".
I have found on the forum and stackoverflow this solution:
https://stackoverflow.com/questions/28839109/place-svg-in-illustrator
var svgFile = File.openDialog("Select File to place...");
var doc = app.activeDocument;
doc. groupItems.createFromFile( svgFile );
If someone has something else to add welcome?
Copy link to clipboard
Copied
I have found on the forum and stackoverflow this solution:
https://stackoverflow.com/questions/28839109/place-svg-in-illustrator
var svgFile = File.openDialog("Select File to place...");
var doc = app.activeDocument;
doc. groupItems.createFromFile( svgFile );
If someone has something else to add welcome?
Copy link to clipboard
Copied
Hi @livl17017666, no you've found the answer. Illustrator doesn't seem to let us place SVGs as links. You could check out my answer here for an unusual use case, but what you've got is basically the whole answer.
- Mark
Copy link to clipboard
Copied
It isn't elegant but just wanted to share something I did to help handling "any" incoming file I want to place (but it doesnt handle a number of things like size and where to place it on the document).
var doc = app.activeDocument;
//need to check if the file is a vector or raster:
var filename = arguments[0];
var extension = filename.split('.').pop();
if (extension = 'svg')
{
var svgFile = File(filename);
doc.groupItems.createFromFile(svgFile);
//might want to add some error checking here
}
else {
var placedItem = doc.placedItems.add();
placedItem.file = new File(filename);
// var aspect = placedItem.height / placedItem.width; //alert(aspect);
// placedItem.width=doc.width * arguments[1];
// placedItem.height=placedItem.width * aspect;
// placedItem.position=[((doc.width/2)-placedItem.width/2),-((doc.height/2)-placedItem.height)] //y negative
}
This also relies on a vbs file from someone else in the forums iirc like:
Set vbsArguments = WScript.Arguments
If vbsArguments.Length = 0 Then
Else
ReDim jsxArguments(vbsArguments.length-2)
for i = 1 to vbsArguments.length - 1
jsxArguments(i-1) = vbsArguments(i)
Next
Set illustrator = CreateObject( "Illustrator.Application" )
Call illustrator.DoJavaScriptFile( vbsArguments(0), jsxArguments, 1)
End IF
Copy link to clipboard
Copied
Some slight improvements to the javascript:
var doc = app.activeDocument;
//need to check if the file is a vector or raster:
var filename = arguments[0];
var extension = filename.split('.').pop();
var maxWidthPercentage = 0.25; // 25% of the document width
if (extension = 'svg')
{
var svgFile = File(filename);
newGroup=doc.groupItems.createFromFile(svgFile);
newGroup.position = [ 0 , 0 ];
var aspectRatio = newGroup.width / newGroup.height;
if (newGroup.width > doc.width * maxWidthPercentage) {
newGroup.width = doc.width * maxWidthPercentage;
newGroup.height = newGroup.width / aspectRatio;
} else {
newGroup.height = doc.height * aspectRatio * maxWidthPercentage;
newGroup.width = newGroup.height * aspectRatio;
}
}
else {
var placedItem = doc.placedItems.add();
placedItem.file = new File(filename);
var aspectRatio = placedItem.height / placedItem.width;
if (placedItem.width > doc.width * maxWidthPercentage) {
placedItem.width = doc.width * maxWidthPercentage;
placedItem.height = placedItem.width / aspectRatio;
} else {
placedItem.height = doc.height * aspectRatio * maxWidthPercentage;
placedItem.width = placedItem.height * aspectRatio;
}
}
Copy link to clipboard
Copied
Hey @smithany, thanks for sharing! So how do you use this? Do you have a watched folder or something? Or drag files onto the VB script?
Copy link to clipboard
Copied
actually, its quite different, I am just displaying a list of logos and then clicking to send the source to the .vbs which runs the jsx with the file path as a parameter. I need to update teh json example, readme and excel but this should be the gist of it: https://github.com/hasteagag/logodropper
Copy link to clipboard
Copied
Ah, I see. Thanks.