Create symbol from layer PageItems

I'm having trouble figuring out how to create a single symbol from all the objects within a single layer.
Despite what the reference docs say, it doesn't seem that PageItems is a valid argument to pass into Symbols.add()...
Anyone have any insight or maybe some example code?
Thanks in advance!

Hmm..  Creating a group seems to be troublesome as well...
The following code gives me
undefined is not an object
on line
var group = doc.groups.add(itemArray);
var doc =app.activeDocument;
var layer = doc.activeLayer;
layer.hasSelectedArtwork = true;
var itemArray = new Array;
for (var i in selection)
    itemArray.push(i);
var group = doc.groups.add(itemArray);
Symbols.Add(group);
My goal is simply to take all the objects on a given layer and create a symbol from them...  Sounded simple when I set out to figure it out.   Turning out not to be so simple...

Similar Messages

  • Can I batch create symbols from layers?

    I have Illustrator files of some fairly complicated schematics that I will be bringing in to Flash. The layers are already well organized and named so I would like to select particular layers, hit a "create symbols" button and have all of the selected layers turned into separate symbols that have the same name as their layer name. I would even be willing to code something to do this but I can't find any information about how it can be done.
    I know that I can import an Illustrator file into Flash and select layers that I can then make into movie clips but again, I have to manually select the layer, tell it to be a movie clip and then give it an instance name. I would be happy to try to automate that process as well but haven't found any info on how it can be done either.
    Does anyone have any ideas on how I can batch create symbols from my existing layers?

    I figured this out using recursion. Pasted here in case it helps someone else:
    #target Illustrator
    var docRef = app.activeDocument;
    var layersRef = app.activeDocument.layers;
    var layersCount = layersRef.length;
    var mySymbolInstance = null;
    if ( app.documents.length > 0 ) {
        // if the file is not empty, loop through all levels
        recurseLayers(docRef.layers);
        clearEmptyLayers();
        alert("I'm done");
    function recurseLayers(objArray) {
        // loop through main layers
        for (var l = 0; l < objArray.length; l++) {
            // loop through secondary layers
            if (objArray[l].layers.length > 0) {
                recurseLayers(objArray[l].layers);
            // loop through first level groups
            if (objArray[l].groupItems.length > 0) {
                recurseGroups(objArray[l].groupItems);
                // create a group
                var layerGrp = objArray[l].groupItems.add();
                //give new group the same name as the old group
                var layerName = objArray[l].name;
                layerGrp.name = layerName;
                // get all page items from group
                var layerGrpPageItems =  objArray[l].pageItems;
                //alert("how many group page items: " + layerGrpPageItems.length);
                for (var li= layerGrpPageItems.length-1; li >0; li--) {
                    // will become the symbol name
                    var layerPageItemName = layerGrpPageItems[li].name;
                    // if it's not already a symbol, make it into one
                    if (layerGrpPageItems[li].typename == "SymbolItem") {
                        layerGrpPageItems[li].moveToBeginning(layerGrp);
                    } else {
                        // create symbols
                        createSymbol(layerGrpPageItems[li], layerGrpPageItems[li].name);
                        // add symbols to group
                        mySymbolInstance.moveToBeginning(layerGrp);
                        // remove original content
                        layerGrpPageItems[li].remove();
                // create symbols from layer
                createSymbol(layerGrp, layerName);
                // remove original layer content
                layerGrp.remove();
    function recurseGroups(objArray) {
        for (var g = 0; g < objArray.length; g++) {
            // loop through second level groups
            if (objArray[g].groupItems.length > 0) {
                recurseGroups(objArray[g].groupItems);
                // create a group
                var grp = objArray[g].groupItems.add();
               //give new group the same name as the old group
                var groupName = objArray[g].name;
                grp.name = groupName;
                //alert("which group is this? " + groupName);
                // get all page items from group
                var grpPageItems =  objArray[g].pageItems;
                //alert("how many group page items: " + grpPageItems.length);
                for (var gi= grpPageItems.length-1; gi >0; gi--) {
                    // will become the symbol name
                    var pageItemName = groupName + "_" + grpPageItems[gi].name;
                    createSymbol(grpPageItems[gi], pageItemName);
                    // add symbols to group
                    mySymbolInstance.moveToBeginning(grp);
                    // remove original content
                    grpPageItems[gi].remove();
    function createSymbol(element, elementName) {
        //alert("elementName" + elementName);
        // create symbols from all items in the group
        var symbolRef = docRef.symbols.add(element);
        //alert("element unnamed before: " + elementName);
        // if the element name is empty, give it a name
        var addElementIndex = 0;
        if(elementName == "") {
            elementName = "unnamed" + addElementIndex;
            addElementIndex++;
        // loop through all the symbols in the document
        var symbolCount = docRef.symbols.length;
            for(var s=0; s<symbolCount; s++)  {
                // existing symbols
                var symbolCheck = docRef.symbols[s];
                //alert(symbolCheck.name);
                var addIndex = 0;
                // if the name already exists, add the index number to it and increment
                if(elementName == symbolCheck.name) {
                    elementName = elementName + addIndex;
                    addIndex++;
        symbolRef.name = elementName;
        //alert("symbol name: " + symbolRef.name);
        mySymbolInstance = docRef.symbolItems.add(symbolRef);
        mySymbolInstance.left = element.left;
        mySymbolInstance.top = element.top;
    function clearEmptyLayers() {
        if (documents.length > 0 && activeDocument.pathItems.length >= 0){
            for (var ni = layersCount - 1; ni >= 0; ni-- ) {
                // get sub layers
                var topLayer = docRef.layers[ni];
                for(var ii = topLayer.layers.length - 1; ii >=0; ii--) {
                    // delete empty sub layers
                    if ( topLayer.layers[ni].pageItems.length == 0 ) {
                        topLayer.layers[ni].remove();

  • Illustrator script to create symbols from images in folder

    Time to give back to the community...
    Here is a script I recently devised to bulk create symbols from images in a folder. Tested with Illustrator CC 2014.
    // Import Folder's Files as Symbols - Illustrator CC script
    // Description: Creates symbols from images in the designated folder into current document
    // Author     : Oscar Rines (oscarrines (at) gmail.com)
    // Version    : 1.0.0 on 2014-09-21
    // Reused code from "Import Folder's Files as Layers - Illustrator CS3 script"
    // by Nathaniel V. KELSO ([email protected])
    #target illustrator
    function getFolder() {
      return Folder.selectDialog('Please select the folder to be imported:', Folder('~'));
    function symbolExists(seekInDoc, seekSymbol) {
        for (var j=0; j < seekInDoc.symbols.length; j++) {
            if (seekInDoc.symbols[j].name == seekSymbol) {
                return true;
        return false;
    function importFolderContents(selectedFolder) {
        var activeDoc = app.activeDocument;     //Active object reference
      // if a folder was selected continue with action, otherwise quit
      if (selectedFolder) {
            var newsymbol;              //Symbol object reference
            var placedart;              //PlacedItem object reference
            var fname;                  //File name
            var sname;                  //Symbol name
            var symbolcount = 0;        //Number of symbols added
            var templayer = activeDoc.layers.add(); //Create a new temporary layer
            templayer.name = "Temporary layer"
            var imageList = selectedFolder.getFiles(); //retrieve files in the folder
            // Create a palette-type window (a modeless or floating dialog),
            var win = new Window("palette", "SnpCreateProgressBar", {x:100, y:100, width:750, height:310});
            win.pnl = win.add("panel", [10, 10, 740, 255], "Progress"); //add a panel to contain the components
            win.pnl.currentTaskLabel = win.pnl.add("statictext", [10, 18, 620, 33], "Examining: -"); //label indicating current file being examined
            win.pnl.progBarLabel = win.pnl.add("statictext", [620, 18, 720, 33], "0/0"); //progress bar label
            win.pnl.progBarLabel.justify = 'right';
            win.pnl.progBar = win.pnl.add("progressbar", [10, 35, 720, 60], 0, imageList.length-1); //progress bar
            win.pnl.symbolCount = win.pnl.add("statictext", [10, 70, 710, 85], "Symbols added: 0"); //label indicating number of symbols created
            win.pnl.symbolLabel = win.pnl.add("statictext", [10, 85, 710, 100], "Last added symbol: -"); //label indicating name of the symbol created
            win.pnl.errorListLabel = win.pnl.add("statictext", [10, 110, 720, 125], "Error log:"); //progress bar label
            win.pnl.errorList = win.pnl.add ("edittext", [10, 125, 720, 225], "", {multiline: true, scrolling: true}); //errorlist
            //win.pnl.errorList.graphics.font = ScriptUI.newFont ("Arial", "REGULAR", 7);
            //win.pnl.errorList.graphics.foregroundColor = win.pnl.errorList.graphics.newPen(ScriptUIGraphics.PenType.SOLID_COLOR, [1, 0, 0, 1], 1);
            win.doneButton = win.add("button", [640, 265, 740, 295], "OK"); //button to dispose the panel
            win.doneButton.onClick = function () //define behavior for the "Done" button
                win.close();
            win.center();
            win.show();
            //Iterate images
            for (var i = 0; i < imageList.length; i++) {
                win.pnl.currentTaskLabel.text = 'Examining: ' + imageList[i].name; //update current file indicator
                win.pnl.progBarLabel.text = i+1 + '/' + imageList.length; //update file count
                win.pnl.progBar.value = i+1; //update progress bar
                if (imageList[i] instanceof File) {         
                    fname = imageList[i].name.toLowerCase(); //convert file name to lowercase to check for supported formats
                    if( (fname.indexOf('.eps') == -1) &&
                        (fname.indexOf('.png') == -1)) {
                        win.pnl.errorList.text += 'Skipping ' + imageList[i].name + '. Not a supported type.\r'; //log error
                        continue; // skip unsupported formats
                    else {
                        sname = imageList[i].name.substring(0, imageList[i].name.lastIndexOf(".") ); //discard file extension
                        // Check for duplicate symbol name;
                        if (symbolExists(activeDoc, sname)) {
                            win.pnl.errorList.text += 'Skipping ' + imageList[i].name + '. Duplicate symbol for name: ' + sname + '\r'; //log error
                        else {
                            placedart = activeDoc.placedItems.add(); //get a reference to a new placedItem object
                            placedart.file = imageList[i]; //link the object to the image on disk
                            placedart.name =  sname; //give the placed item a name
                            placedart.embed();   //make this a RasterItem
                            placedart = activeDoc.rasterItems.getByName(sname); //get a reference to the newly created raster item
                            newsymbol = activeDoc.symbols.add(placedart); //add the raster item to the symbols                 
                            newsymbol.name = sname; //name the symbol
                            symbolcount++; //update the count of symbols created
                            placedart.remove(); //remove the raster item from the canvas
                            win.pnl.symbolCount.text = 'Symbols added: ' + symbolcount; //update created number of symbols indicator
                            win.pnl.symbolLabel.text = 'Last added symbol: ' + sname; //update created symbol indicator
                else {
                    win.pnl.errorList.text += 'Skipping ' + imageList[i].name + '. Not a regular file.\r'; //log error
                win.update(); //required so pop-up window content updates are shown
            win.pnl.currentTaskLabel.text = ''; //clear current file indicator
            // Final verdict
            if (symbolcount >0) {
                win.pnl.symbolLabel.text = 'Symbol library changed. Do not forget to save your work';
            else {
                win.pnl.symbolLabel.text = 'No new symbols added to the library';
            win.update(); //update window contents
            templayer.remove(); //remove the temporary layer
        else {
            alert("Action cancelled by user");
    if ( app.documents.length > 0 ) {
        importFolderContents( getFolder() );
    else{
        Window.alert("You must open at least one document.");

    Thank you, nice job & I am looking forward to trying it out!

  • Get the ID of a dynamically created symbol from library, INSIDE another symbol.

    Hi everyone,
    I'm trying to get the id from a dynamic created symbol from library.
    When dynamically creating the symbol directly on the stage (or composition level), there's no problem.
    But I just can't get it to work when creating the symbol inside another symbol. 
    Below some examples using both "getChildSymbols()" and "aSymbolInstances" 
    // USING "getChildSymbols()" ///////////////////////////////////////////////////////////////////////// 
    // ON THE STAGE 
    var m_item = sym.createChildSymbol("m_item","Stage");
    var symbolChildren = sym.getChildSymbols(); 
    console.log(symbolChildren[0].getSymbolElement().attr('id')); // ok eid_1391853893203
    // INSIDE ANOTHER SYMBOL
    var m_item = sym.createChildSymbol("m_item", sym.getSymbol("holder").getSymbolElement()); 
    var symbolChildren = sym.getSymbol("holder").getChildSymbols(); // Am i using this wrong maybe?
    console.log(symbolChildren.length) // returns 0 so can't get no ID either
    // USING "aSymbolInstances"" ////////////////////////////////////////////////////////////////////////// 
    // ON THE STAGE
    var m_item = sym.createChildSymbol("m_item","Stage"); 
    console.log(sym.aSymbolInstances[0]); // ok (i guess) x.fn.x.init[1] 0: div#eid_1391854141436
    // INSIDE ANOTHER SYMBOL
    var m_item = sym.createChildSymbol("m_item", sym.getSymbol("holder").getSymbolElement());
    console.log(sym.getSymbol("holder").aSymbolInstances[0]); // Javascript error in event handler! Event Type = element 
    In this post http://forums.adobe.com/message/5691824 is written: "mySym.aSymbolInstances will give you an array with all "names" when you create symbols"
    Could it be this only works on the stage/ composition level only and not inside a symbol? 
    The following methods to achieve the same are indeed possible, but i simply DON'T want to use them in this case:
    1) Storing a reference of the created symbol in an array and call it later by index.
    2) Giving the items an ID manually on creation and use document.getElementById() afterwards.
    I can't believe this isn't possible. I am probably missing something here.
    Forgive me I am a newbie using Adobe Edge!
    I really hope someone can help me out here.
    Anyway, thnx in advance people!
    Kind Regards,
    Lester.

    Hi,
    Thanks for the quick response!
    True this is also a possibility. But this method is almost the same of "Giving the items an ID manually on creation and use document.getElementById() afterwards".
    In this way (correct me if i'm wrong) you have to give it an unique ID yourself. In a (very) big project this isn't the most practical way.
    Although I know it is possible.
    Now when Edge creates a symbol dynamically on the Stage (or composition level) or inside another symbol it always gives the symbol an ID like "eid_1391853893203".
    I want to reuse this (unique) ID given by Edge after creation.
    If created on the stage directly you can get this ID very easy. Like this;
    var m_item = sym.createChildSymbol("m_item","Stage");
    var symbolChildren = sym.getChildSymbols(); 
    console.log(symbolChildren[0].getSymbolElement().attr('id')); // ok eid_1391853893203
    I want to do exactly the same when created INSIDE another symbol.
    var m_item = sym.createChildSymbol("m_item", sym.getSymbol("holder").getSymbolElement());
    Now how can I accomplish this? How can I get the Id of a dynamically created symbol INSIDE another symbol instead of created directly on the stage?
    This is what i'm after.
    Thnx in advance!

  • Create symbol from "scratch"

    Hello all!
    I've been busy and haven't checked in here for awhile - I guess for me that's good :-)
    I've got a question that I know I'm not the only one and was wondering what others do...
    I have a stage with everything on it I want, including 6 buttons.  Now I want to create "symbols" that each button will call.  Is there a "correct" way to do this?
    I know I could add a small rectangle off stage, create a symbol from it and then remove it from the stage and create what I want.
    Is there a better way?
    Just curious :-)
    James

    Hey James,
    You can also create symbols and then export them and reuse them in other compositions.
    I usually create all my symbol elements, right-click on all the divs and then convert to symbol. then, if it is something I think I could reuse somewhere else, I export it.
    But different people have different ways of doing things.

  • Creating symbol from animated layer

    Hi all. Newbie post here.
    I created an animation of a stick figure in my flash project,
    then decided that I wanted to make it a symbol. Is there some way
    to make the animation/set of frames part of the symbol? When I
    select the layer and its frames, "convert to symbol" becomes dimmed
    (because it's a layer maybe). But other attempts have resulted in
    my just getting the original stick figure over and not the
    sequence.
    I am probably overlooking something obvious, but I can't
    figure it out!
    Thanks for any help.
    Karen

    Actually it's ridiculously complicated ;-)
    Easier to explain visually:
    http://www.stevenlyons.org/uploaded_files/tools2/frames-symbol.jpg

  • Creating symbol removes some layers

    I am having some layers (without content) removed that have sublayers with content collected into them when creating symbols from a set of layers. Why does this occur and what is the exact expected behavior?
    I am presuming layers without content on them directly are stripped? Has this been changed in CS6?

    What Monika means is that you cannot have a layer without content if that layer has sublayers. Show a screen capture of the Layers panel with all the layers open of the original and what happens after you make the symbol.

  • Create a symbol from placedItem/rasterItem does not work

    I need to create symbols in Illustrator CC programmatically from files on on disk. The following code gives an error when adding the symbol and I have no clue why. Any help will be appreciated. According to Adobe Illustrator CC Scripting Reference, a symbol can be added from a source art of type:
    CompoundPathItems
    GroupItems
    MeshItems
    NonNativeItems
    PageItems
    PathItems
    RasterItems
    SymbolItems
    TextFrameItems
    Here is the code:
    #target illustrator
    var activeDoc = app.activeDocument;
    var tempLayer = activeDoc.layers.add(); //Layer object reference
    var newSymbol; //Symbol object reference
    var srcArt; //PlacedItem object reference
    var fName; //File name
    // create document list from files in selected folder
    var selectedFolder = Folder.selectDialog('Please select the folder to be imported:', Folder('~'));
    var imageList = selectedFolder.getFiles();
    for (var i = 0; i < 1; i++) {
        if (imageList[i] instanceof File) {
            fName = imageList[i].name.toLowerCase();
            if( (fName.indexOf('.eps') == -1) && (fName.indexOf('.png') == -1)) {
                continue; // skip unsupported formats
            else {
                srcArt = activeDoc.placedItems.add(); //Place the image on the artboard
                srcArt.file = imageList[i];
                srcArt.embed();                                       
                activeDoc.symbols.add(srcArt);  --> Execution stops here with the message "an Illustrator error occurred: 556099904 ('@i%!')"
                newSymbol.name = fName.substring(0, fName.lastIndexOf(".") );

    srcArt is a "PlacedItem", after rasterizing, it disappears and a new "RasterItem" object is created, so you need to get a reference to this newly created object and continue
    srcArt.embed();     
    var embedded = activeDoc.rasterItems[0];
    activeDoc.symbols.add(embedded);

  • How do I use "Trim Paths" after "Create Shapes from Vector Layer" on a strokes only layer?

    Hello,
    Adobe After Effects CC
    2014.0.2
    Version 13.0.2.3
    I have imported an illustrator file that was created in Illustrator CC 18.0.0. The file contains the outline of a logo. It is the logo with the fill set to none and the stroke set to 1px.
    I drag the logo_strokes.ai file onto the composition, right click, and click Create Shapes from Vector Layer.
    A new layer is created and in the Contents "drop down", a Fill 1 layer is created.
    When I select the Contents "drop down" and click Add / Trim Paths, a Trim Paths 1 layer is created.
    When I animate Trim Paths 1, it appears to animate the Fill layer, not the stroke.
    How do I get it to animate just the stroke, and not show a fill at all?
    Thank you.

    Thanks,
    I am reading and watching tutorials. Still stuck on that one point.
    Here are the images of the After Effects layers. The illustrator layer is the top layer.

  • Script that create Layerset with name from layer?

    Hello!
    This should be easy for someone who know whet is doing so not me:P
    I want script to automatically create layerset from each selected layer, name it as that layer and place that layer in it
    I found old script that do that but only normal layer not linked or embeded, and I want to do it for all selected layers not only to one
    Can someone modify it a little to work little better? I have no idea how to do it and probably would take me like thousands hours or more
    thx
    #target photoshop
    main();
    function main(){
    if(!documents.length) return;
    var doc = activeDocument;
    if(doc.activeLayer.kind != LayerKind.NORMAL) return;
    var layerRef = doc.activeLayer;
    var layerSetRef = doc.layerSets.add();
    layerSetRef.name = layerRef.name;
    layerRef.move(layerSetRef, ElementPlacement.PLACEATEND);

    You could remove the line
    if(doc.activeLayer.kind != LayerKind.NORMAL) return;
    But it would only work with one selected Layer (and also on selected LayerSets).
    To handle more than one selected Layer (or all Layers) some changes would be necessary.

  • How do I stop Photoshop CS4 from creating a new layer each time I use the line tool?

    How do I stop Photoshop CS4 from creating a new layer each time I use the line tool?
    Each time I use the line tool, a new layer is created. How can I stop this? No settings were changed.
    Thanks in advance.

    Look at the three options in the top bar.

  • How create new draw layer in Spreadsheet View Multisim Symbol Editor ?

    Hello,
    I would like to know how create new draw layer in Spreadsheet View Multisim Symbol Editor ?
    I am interested in this to create a contact or switch with 3 positions (one in and three out).
    So ... ??
    Best regards

    Hi,
    I am not really sure what you are trying to do with a new layer but the spreadsheet was hard coded and you cannot add a new layer.  In the Symbol Editor, there is a rectangle with dash lines, this is the symbol boundary and you can select Edit>>Resize Boundary Box to make the boundary larger/smaller.  Whatever you draw must be inside the boundary, you can only place pins outside the boundary.  The switch you created will not toggle in simulation, only the switches in the Master database can do this, R&D have to program them to toggle.
    Tien P.
    National Instruments

  • How can I automatically create a rectangular layer mask from an irregularly shaped selection?

    I am trying to automatically crop images. The subject is on a white background and extends from the left edge of the image to the right. The top and bottom of the subject are within the frame. I need to crop a certain distance from the top and bottom of the subject and a predefined width that would be calculated from the height of the subject.
    My current idea is to create a duplicate layer and run Threshold on it, which allows me to select the subject clearly. Once I have the subject selected I can use Image -> Crop to create the desired rectangle, but I can't figure out a way to create a mask from the irregularly selected subject. Does anyone know a way to take a selection and convert it to a rectangle? I hope this makes sense, I can try to explain better if not. Thanks.
    --Chris

    // make selection rectangular;
    // 2013, use it at your own risk;
    #target photoshop
    if (app.documents.length > 0) {
    var myDocument = app.activeDocument;
    try {
    var theBounds = myDocument.selection.bounds;
    var theArray = [[theBounds[0], theBounds[1]], [theBounds[2], theBounds[1]], [theBounds[2], theBounds[3]], [theBounds[0], theBounds[3]]];
    myDocument.selection.select(theArray, SelectionType.REPLACE, 0, false);
    catch (e) {}

  • How do I make a symbol from selection?

    I've got the below script, but it's not working. I'm trying to select all the art in the document on different layers, and create a symbol. So far I have the first part down, but can't figure out how to reference what is selected. actDoc.selection comes out as undefined. I've also tried to use "actDoc.PageItem.selected = true" and a few other variants, but I'm coming up empty. Anyone have recommendations for this?
    actDoc = app.activeDocument
    var layerCount = actDoc.layers.length;
    if(layerCount > 1) {
        for(i=0;i<layerCount;i++) {
        actDoc.layers[i].hasSelectedArtwork = true // selects all layers
       actDoc.symbols.add(actDoc.selection); // <---------- Here's where the error is thrown

    Thanks! That makes sense! Now I'd like to put a layer with sublayers into a symbol to maintain any effects that are on the sub layers. I have an action that runs, that does this with nested layers like this. I used to just let it group everything, but then found some effects were falling off. I tried the below, but it didn't work, and states that it's an illegal argument. Is there any way to acheive this?
                            layer = actDoc.layers.getByName('Editorial'); //get a reference to the newly created raster item 
                            newsymbol = actDoc.symbols.add(layer); //add the raster item to the symbols  

  • How can I create a new layer that is a gradient?

    Hi, i'm very new to photoshop scripting and am having some trouble.
    I'm looking for a way to take an image i have and set it to have a gradient opacity as it approaches the middle, my thought on how to do that was to just create a layer that is a gradient from top left to bottom right and then attach that as a vector mask.
    Any ideas on how I could create this gradient layer in script, or a better method of doing this opacity gradient?
    Thanks in advance,
    Levianth

    You could try this:
    // 2012, use it at your own risk;
    #target photoshop
    if (app.documents.length > 0) {
    var myDocument = app.activeDocument;
    var theLayer = myDocument.activeLayer;
    if (theLayer.isBackgroundLayer == true) {theLayer.isBackgroundLayer = false};
    // create gradient layer;
    // =======================================================
    var idMk = charIDToTypeID( "Mk  " );
        var desc15 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref3 = new ActionReference();
            var idcontentLayer = stringIDToTypeID( "contentLayer" );
            ref3.putClass( idcontentLayer );
        desc15.putReference( idnull, ref3 );
        var idUsng = charIDToTypeID( "Usng" );
            var desc16 = new ActionDescriptor();
            var idType = charIDToTypeID( "Type" );
                var desc17 = new ActionDescriptor();
                var idType = charIDToTypeID( "Type" );
                var idGrdT = charIDToTypeID( "GrdT" );
                var idLnr = charIDToTypeID( "Lnr " );
                desc17.putEnumerated( idType, idGrdT, idLnr );
                var idGrad = charIDToTypeID( "Grad" );
                    var desc18 = new ActionDescriptor();
                    var idNm = charIDToTypeID( "Nm  " );
                    desc18.putString( idNm, "Custom" );
                    var idGrdF = charIDToTypeID( "GrdF" );
                    var idGrdF = charIDToTypeID( "GrdF" );
                    var idCstS = charIDToTypeID( "CstS" );
                    desc18.putEnumerated( idGrdF, idGrdF, idCstS );
                    var idIntr = charIDToTypeID( "Intr" );
                    desc18.putDouble( idIntr, 4096.000000 );
                    var idClrs = charIDToTypeID( "Clrs" );
                        var list3 = new ActionList();
                            var desc19 = new ActionDescriptor();
                            var idClr = charIDToTypeID( "Clr " );
                                var desc20 = new ActionDescriptor();
                                var idRd = charIDToTypeID( "Rd  " );
                                desc20.putDouble( idRd, 0.000000 );
                                var idGrn = charIDToTypeID( "Grn " );
                                desc20.putDouble( idGrn, 0.000000 );
                                var idBl = charIDToTypeID( "Bl  " );
                                desc20.putDouble( idBl, 0.000000 );
                            var idRGBC = charIDToTypeID( "RGBC" );
                            desc19.putObject( idClr, idRGBC, desc20 );
                            var idType = charIDToTypeID( "Type" );
                            var idClry = charIDToTypeID( "Clry" );
                            var idUsrS = charIDToTypeID( "UsrS" );
                            desc19.putEnumerated( idType, idClry, idUsrS );
                            var idLctn = charIDToTypeID( "Lctn" );
                            desc19.putInteger( idLctn, 0 );
                            var idMdpn = charIDToTypeID( "Mdpn" );
                            desc19.putInteger( idMdpn, 50 );
                        var idClrt = charIDToTypeID( "Clrt" );
                        list3.putObject( idClrt, desc19 );
                            var desc21 = new ActionDescriptor();
                            var idClr = charIDToTypeID( "Clr " );
                                var desc22 = new ActionDescriptor();
                                var idRd = charIDToTypeID( "Rd  " );
                                desc22.putDouble( idRd, 0.000000 );
                                var idGrn = charIDToTypeID( "Grn " );
                                desc22.putDouble( idGrn, 0.000000 );
                                var idBl = charIDToTypeID( "Bl  " );
                                desc22.putDouble( idBl, 0.000000 );
                            var idRGBC = charIDToTypeID( "RGBC" );
                            desc21.putObject( idClr, idRGBC, desc22 );
                            var idType = charIDToTypeID( "Type" );
                            var idClry = charIDToTypeID( "Clry" );
                            var idUsrS = charIDToTypeID( "UsrS" );
                            desc21.putEnumerated( idType, idClry, idUsrS );
                            var idLctn = charIDToTypeID( "Lctn" );
                            desc21.putInteger( idLctn, 4096 );
                            var idMdpn = charIDToTypeID( "Mdpn" );
                            desc21.putInteger( idMdpn, 50 );
                        var idClrt = charIDToTypeID( "Clrt" );
                        list3.putObject( idClrt, desc21 );
                    desc18.putList( idClrs, list3 );
                    var idTrns = charIDToTypeID( "Trns" );
                        var list4 = new ActionList();
                            var desc23 = new ActionDescriptor();
                            var idOpct = charIDToTypeID( "Opct" );
                            var idPrc = charIDToTypeID( "#Prc" );
                            desc23.putUnitDouble( idOpct, idPrc, 0.000000 );
                            var idLctn = charIDToTypeID( "Lctn" );
                            desc23.putInteger( idLctn, 0 );
                            var idMdpn = charIDToTypeID( "Mdpn" );
                            desc23.putInteger( idMdpn, 50 );
                        var idTrnS = charIDToTypeID( "TrnS" );
                        list4.putObject( idTrnS, desc23 );
                            var desc24 = new ActionDescriptor();
                            var idOpct = charIDToTypeID( "Opct" );
                            var idPrc = charIDToTypeID( "#Prc" );
                            desc24.putUnitDouble( idOpct, idPrc, 100.000000 );
                            var idLctn = charIDToTypeID( "Lctn" );
                            desc24.putInteger( idLctn, 2048 );
                            var idMdpn = charIDToTypeID( "Mdpn" );
                            desc24.putInteger( idMdpn, 50 );
                        var idTrnS = charIDToTypeID( "TrnS" );
                        list4.putObject( idTrnS, desc24 );
                            var desc25 = new ActionDescriptor();
                            var idOpct = charIDToTypeID( "Opct" );
                            var idPrc = charIDToTypeID( "#Prc" );
                            desc25.putUnitDouble( idOpct, idPrc, 0.000000 );
                            var idLctn = charIDToTypeID( "Lctn" );
                            desc25.putInteger( idLctn, 4096 );
                            var idMdpn = charIDToTypeID( "Mdpn" );
                            desc25.putInteger( idMdpn, 50 );
                        var idTrnS = charIDToTypeID( "TrnS" );
                        list4.putObject( idTrnS, desc25 );
                    desc18.putList( idTrns, list4 );
                var idGrdn = charIDToTypeID( "Grdn" );
                desc17.putObject( idGrad, idGrdn, desc18 );
            var idgradientLayer = stringIDToTypeID( "gradientLayer" );
            desc16.putObject( idType, idgradientLayer, desc17 );
        var idcontentLayer = stringIDToTypeID( "contentLayer" );
        desc15.putObject( idUsng, idcontentLayer, desc16 );
    executeAction( idMk, desc15, DialogModes.NO );
    // move layer below;
    var theGradient = myDocument.activeLayer;
    theGradient.move(theLayer, ElementPlacement.PLACEAFTER);
    // clipping mask;
    theLayer.grouped = true

Maybe you are looking for