Skip to main content
hakzter
Known Participant
March 15, 2020
Answered

How to select all vector smart objects in .psd file and save as separate .ai files?

  • March 15, 2020
  • 2 replies
  • 4594 views

Hi all!

I have .psd file with different layers. Some layers are vector smart objects (VSO) and some are just image layers. What I need to do is to select (or filter) only VSO and export them as separate .ai files. I know I can do it by right click on layer -> Export contents. But when I select multiply layers "Export contents" is inactive. Please assist.

This topic has been closed for replies.
Correct answer JJMack

I uploaded the script and it appears in my Scripts list. Then I reloaded Photoshop and open my .psd file and run script from File - Scripts but nothing happens. Sorry Im not goog with all this scripting stuff, can you assit more?

 

 

Many thanks to you JJMack! I realized that my question is not about I'm trying to do. Thank you for that. I created the new one How to export contents from all smart objects? that is more focused on what I expect to get.


As I wrote I do not install AI. This photoshop script should open all Vector Smart objects in your layered Photoshop document in Ai for you to be able to save Ai .svg and .ai files.

 

 

/* ==========================================================
// 2017  John J. McAssey (JJMack) 
// ======================================================= */
// This script is supplied as is. It is provided as freeware. 
// The author accepts no liability for any problems arising from its use.
// enable double-clicking from Mac Finder or Windows Explorer
#target photoshop // this command only works in Photoshop CS2 and higher
// bring application forward for double-click events
app.bringToFront();
// ensure at least one document open
if (!documents.length) alert('There are no documents open.', 'No Document');
else { app.activeDocument.suspendHistory('Some Process Name','main()');
}
///////////////////////////////////////////////////////////////////////////////
//                            main function                                  //
///////////////////////////////////////////////////////////////////////////////
function main() {
	// declare local variables
	var orig_ruler_units = app.preferences.rulerUnits;
	var orig_type_units = app.preferences.typeUnits;
	var orig_display_dialogs = app.displayDialogs;
	app.preferences.rulerUnits = Units.PIXELS;  	// Set the ruler units to PIXELS
	app.preferences.typeUnits = TypeUnits.POINTS;   // Set Type units to POINTS
	app.displayDialogs = DialogModes.NO;	    	// Set Dialogs off
	try { code(); }
	// display error message if something goes wrong
	catch(e) { alert(e + ': on line ' + e.line, 'Script Error', true); }
	app.displayDialogs = orig_display_dialogs;  	// Reset display dialogs 
	app.preferences.typeUnits  = orig_type_units;	// Reset ruler units to original settings 
	app.preferences.rulerUnits = orig_ruler_units;	// Reset units to original settings
}
///////////////////////////////////////////////////////////////////////////////
//                           main function end                               //
///////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////
// The real code is embedded into this function so that at any point it can return //
// to the main line function to let it restore users edit environment and end      //
/////////////////////////////////////////////////////////////////////////////////////
function code() {
	processArtLayers(activeDocument) 
}
function processArtLayers(obj) {  
    for( var i = obj.artLayers.length-1; 0 <= i; i--) {processLayers(obj.artLayers[i])}  
    for( var i = obj.layerSets.length-1; 0 <= i; i--) {processArtLayers(obj.layerSets[i])} // Process Layer Set Layers  
} 
function processLayers(layer) { 
	if (layer.kind == LayerKind.SMARTOBJECT) {
		if (smartobject_file_ext(layer) == "svg" | smartobject_file_ext(layer) == "ai" ) {
			app.activeDocument.activeLayer=layer;
			openSmartObject(app.activeDocument.activeLayer);
			alert("Save the ." +  smartobject_file_ext(layer) + " file for layer " + layer);
		}
	}
}    

// Thanks to  r-bin
function smartobject_file_ext(layer) {  
    try {         
        var r = new ActionReference();     
        r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("smartObject"));  
        r.putIdentifier(stringIDToTypeID("layer"), layer.id);  
        var name = executeActionGet(r).getObjectValue(stringIDToTypeID("smartObject")).getString(stringIDToTypeID("fileReference"));         
         
        var n = name.lastIndexOf(".");  
        if (n < 0) return "";  
     
        return name.substr(n+1).toLowerCase();  
        }  
    catch (e) { return "error"; }  
}    

////// open smart object //////
function openSmartObject (theLayer) {
	if (theLayer.kind == "LayerKind.SMARTOBJECT") {
		// =======================================================
		var idplacedLayerEditContents = stringIDToTypeID( "placedLayerEditContents" );
			var desc2 = new ActionDescriptor();
		executeAction( idplacedLayerEditContents, desc2, DialogModes.NO );
	};
};

 

 

2 replies

JJMack
Community Expert
Community Expert
March 15, 2020

I do not understand your question? All vector smart object layers are Placed AI files. The vector Ai files exists.  If you open a vector smart object layer to update its content it will open in Ai not Photoshop because Photoshop does not support vector files. With a script you can process all Smart object layers and open the vector files in AI. R-bin posted script code to get smart object file types.

 

 

function smartobject_file_ext(layer) {  
    try {         
        var r = new ActionReference();     
        r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("smartObject"));  
        r.putIdentifier(stringIDToTypeID("layer"), layer.id);  
        var name = executeActionGet(r).getObjectValue(stringIDToTypeID("smartObject")).getString(stringIDToTypeID("fileReference"));         
         
        var n = name.lastIndexOf(".");  
        if (n < 0) return "";  
     
        return name.substr(n+1).toLowerCase();  
        }  
    catch (e) { return "error"; }  
}    

 

JJMack
hakzter
hakzterAuthor
Known Participant
March 15, 2020

You are right.

I struggle with issue when I must double-click every VSO in Photoshop to open it Illustrator and than save as .ai file beacuse I dont have source files. As far as I understood your script can open all VSO in Illustrator tabs and I can save all opened tabs in Illustrator as separate files, right?

hakzter
hakzterAuthor
Known Participant
March 15, 2020

The thing is a Photoshop script will lose control if the script open a smart object layer that is a AI vector file.  For Adobe Illustrator will be started by your Operating systems file association for the temp vector file Photoshop will create for the object file either Linked or embedded in system temp space.   You would need to saved that files from Illustrator.  When you do that if you do not save the system temp work document created by Photoshop. Photoshop will not update the documents smart object and the script would have no idea if you save to file somewhere else from Illustrator. This script could most likely access the temp vector file created by Photoshop and use JavaScript do a file copy to some location that you programmed into the script. If it exist either replace or add a sequence  number to the file name. You can not export the Ai file from Photoshop for Photoshop does not support AI vector file formats.  Photoshop may be able to export shape layers as a .svg file and the .svg file may well contain vector graphics.  You can open the .svg file in a text editor to see if any Paths are recorded in the text .svg file.

 

Photoshop can rasterize AI files and process the raster layer. Photoshop can not process AI files.

 

Here is a Shape layer I exported as a .svg file. Included here in text format.

<svg xmlns="http://www.w3.org/2000/svg" width="3200" height="3200" viewBox="0 0 3200 3200">
  <defs>
    <style>
      .cls-1 {
        fill-rule: evenodd;
      }
    </style>
  </defs>
  <path id="Impossible_Star_1" data-name="Impossible Star 1" class="cls-1" d="M1862.17,400h259.68l351.56,993.39H3500.12L3600,1634.35l-799,672.12,383.52,1086.4L2960.8,3600l-910.86-684.81L1218.98,3600l-239.7-177.54,327.594-1086.4L400,1642.8l95.88-249.41H1566.54Zm4,16.909L1438.7,1858.39l219.73,156.41L2105.87,416.909h-239.7Zm247.69,4.227L1666.42,2019.02l159.8,109.91,195.75-735.54h435.46Zm-83.9,989.164-55.93,211.36H3584.02l-87.89-207.13Zm-1498.125,4.23L1906.12,2429.06l-227.72,186-359.55-270.54-319.6,1061.03L2309.61,2374.11l-67.91-211.36L2045.94,2310.7l-623.22-452.31,135.83-443.86H531.835Zm-23.97,0L423.97,1634.35,1678.4,2593.92l203.75-164.86ZM1970.04,1634.35l-55.93,224.04H2641.2l155.8,435.4,787.02-659.44H1970.04Zm-59.93,236.72-71.91,266.31,207.74,152.18,191.76-143.72-83.89-274.77h-243.7Zm259.68,0,231.71,760.9-339.58,274.77,890.89,676.35L2365.54,1871.07H2169.79Zm211.73,0,583.27,1712.02,207.74-194.45L2633.21,1871.07H2381.52Zm-67.91,515.72L999.251,3418.23l219.729,164.86,1170.53-955.35Z" transform="translate(-400 -400)"/>
</svg>

Here is how Chrome displays thet .svg


Many thanks for your time JJMack!
It seems this script doesn't work for me since when I create .psd files I copie and paste objects from Illustrator to Photoshop and now I have only .psd file with a lot of AI vector smart objects. The thing is when I right-click any VSO and click "Export Contents" I get what I need "Vector Smart Object.ai" file from selected VSO in Photoshop. The only problem is I have near 150 VSObjects in my .psd file and I extremely need "Export Contents" for all of them instead of manual clicking one by one.. Maybe you have any Ideas how can I get .ai files from all my VSOs in .psd?


Another way I can accept is 'proper' (resizable) SVG files that I "save as" from .ai. Thats why I export VSOs to .ai and then save as SVG because there in no other way to get (resizable) SVG from Photoshop.
I appreciate your help.

 

 

If I export VSO layer as SVGs I get the following code:

And as a result I can't use it as SVG =(

c.pfaffenbichler
Community Expert
Community Expert
March 15, 2020

That would in all likelihood need a Script. 

How familiar are you with JavaScript? 

hakzter
hakzterAuthor
Known Participant
March 15, 2020

Basics only and I have no idea how to implement JS into Photoshop =( Can you assist?

c.pfaffenbichler
Community Expert
Community Expert
March 15, 2020

Not at current. 

Maybe it would help you do it manually if you hid all non-SOs with the Layers Panel’s Filter. 

 

And a warning: If you created the Vector SOs by pasting content copied in Illustrator that will export contents not as ai but as pdf.