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

Problem with an Illustrator convert script when source files have a "." in their name

New Here ,
Aug 26, 2020 Aug 26, 2020

Copy link to clipboard

Copied

Hello everyone,

i often use a script to convert AI files to SVG files

This works really like a charm, but the script gets garbled if the source filenames contain dots in their names (e.g. "File1.2.ai").

I think that some of the code needs to be adapted, but despite a lot of reading and trying I can't get it right...

Here is the complete script code:

 

/**********************************************************

ADOBE SYSTEMS INCORPORATED 
Copyright 2005-2010 Adobe Systems Incorporated 
All Rights Reserved 

NOTICE:  Adobe permits you to use, modify, and 
distribute this file in accordance with the terms
of the Adobe license agreement accompanying it.  
If you have received this file from a source 
other than Adobe, then your use, modification,
or distribution of it requires the prior 
written permission of Adobe. 

*********************************************************/

/**********************************************************
 
Save as SVGs.jsx

DESCRIPTION

This sample gets files specified by the user from the 
selected folder and batch processes them and saves them 
as SVGs in the user desired destination with the same 
file name.

Based on Adobe's "Save as PDFs" sample script, intended for Illustrator CS6
 
**********************************************************/

// Main Code [Execution of script begins here]

// uncomment to suppress Illustrator warning dialogs
// app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, svgSaveOpts;

// Select the source folder.
sourceFolder = Folder.selectDialog( 'Quell-Verzeichnis mit AI-Dateien auswählen', '~' );

// If a valid folder is selected
if ( sourceFolder != null )
{
  files = new Array();
	fileType = prompt( 'Select type of Illustrator files to you want to process. Eg: *.ai', ' ' );
	
	// Get all files matching the pattern
	files = sourceFolder.getFiles( fileType );
	
	if ( files.length > 0 )
	{
		// Get the destination to save the files
		destFolder = Folder.selectDialog( 'Ziel-Verzeichnis für die SVGs auswählen.', '~' );
		for ( i = 0; i < files.length; i++ )
		{
			sourceDoc = app.open(files[i]); // returns the document object
									
			// Call function getNewName to get the name and file to save the SVG
			targetFile = getNewName();
			
			// Call function getSVGOptions get the SVGSaveOptions for the files
			svgSaveOpts = getSVGOptions( );
			
			// Save as svg
			sourceDoc.exportFile(targetFile, ExportType.SVG, svgSaveOpts );

			sourceDoc.close();
		}
		alert( 'Files are saved as SVG in ' + destFolder );
	}
	else
	{
		alert( 'No matching files found' );
	}
}


/*********************************************************

getNewName: Function to get the new file name. The primary
name is the same as the source file.

**********************************************************/

function getNewName()
{
	var ext, docName, newName, saveInFile, docName;
	docName = sourceDoc.name;
	ext = '.svg'; // new extension for svg file
	newName = "";
		
	for ( var i = 0 ; docName[i] != "." ; i++ )
	{
		newName += docName[i];
	}
	newName += ext; // full svg name of the file
	
	// Create a file object to save the svg
	saveInFile = new File( destFolder + '/' + newName );
	

	return saveInFile;
}


function getSVGOptions()
{
	var svgSaveOpts = new ExportOptionsSVG();
							svgSaveOpts.DTD = SVGDTDVersion.SVG1_1;
							svgSaveOpts.embedRasterImages = false;
							svgSaveOpts.cssProperties = SVGCSSPropertyLocation.STYLEATTRIBUTES;
							svgSaveOpts.coordinatePrecision = 3;
							svgSaveOpts.fontType = SVGFontType.SVGFONT;
							svgSaveOpts.documentEncoding = SVGDocumentEncoding.UTF8;
							svgSaveOpts.fontSubsetting = SVGFontSubsetting.None;
							svgSaveOpts.response = false;
							svgSaveOpts.sVGTextOnPath = true;
							svgSaveOpts.slices = false;
							svgSaveOpts.preserveEditability = false;
							svgSaveOpts.sVGAutoKerning = false;
						return svgSaveOpts; }

 

 

 Here is an example what happen, when the source files have a . in the name:

 

image.png
Output  file then:


image.png

 

It should create 3x SVG files (900.10.svg for example), but unfortunately only the one file with the wrong name is created.

Can anyone help me with this?

 

I think the code need some adjustment, but I am not capable of that...

 

Thanks a lot and best regards,
Sebastian

TOPICS
Scripting

Views

376

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

Valorous Hero , Aug 27, 2020 Aug 27, 2020

Change

 

	newName = "";
		
	for ( var i = 0 ; docName[i] != "." ; i++ )
	{
		newName += docName[i];
	}

 

To:

 

newName = docName.replace(/\.[^\.]+$/, "");

 

Votes

Translate

Translate
Adobe
Valorous Hero ,
Aug 27, 2020 Aug 27, 2020

Copy link to clipboard

Copied

Change

 

	newName = "";
		
	for ( var i = 0 ; docName[i] != "." ; i++ )
	{
		newName += docName[i];
	}

 

To:

 

newName = docName.replace(/\.[^\.]+$/, "");

 

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
New Here ,
Aug 28, 2020 Aug 28, 2020

Copy link to clipboard

Copied

LATEST

Thank you Silly-V,

It works like a charm! Thank you very much!

Kind regards
Sebastian

 

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