• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Is it possible to place .svg file with script inside Illustrator?

Explorer ,
Nov 26, 2022 Nov 26, 2022

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'".

TOPICS
Scripting

Views

831

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Nov 26, 2022 Nov 26, 2022

I have found on the forum and stackoverflow this solution:

https://community.adobe.com/t5/illustrator-discussions/svg-files-into-one-layered-ai-file/m-p/4733761

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?

 

Votes

Translate

Translate
Adobe
Explorer ,
Nov 26, 2022 Nov 26, 2022

Copy link to clipboard

Copied

I have found on the forum and stackoverflow this solution:

https://community.adobe.com/t5/illustrator-discussions/svg-files-into-one-layered-ai-file/m-p/473376...

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?

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 26, 2022 Nov 26, 2022

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 13, 2024 Jan 13, 2024

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 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 13, 2024 Jan 13, 2024

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;
    }
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 13, 2024 Jan 13, 2024

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 13, 2024 Jan 13, 2024

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 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 13, 2024 Jan 13, 2024

Copy link to clipboard

Copied

LATEST

Ah, I see. Thanks.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines