Skip to main content
Participant
December 12, 2017
Answered

Automatic instance names when importing from Photoshop

  • December 12, 2017
  • 2 replies
  • 857 views

Hello

I have a question - is it possible to automatically give mc names (from name layers for example) when importing from Photoshop? I just want the same instance names as object names... Scripts/extensions or other solution?

thanks, Remigiusz Szwed

    This topic has been closed for replies.
    Correct answer kglad

    yeah, I know:-) I wash hoping that anyone know extension/script that could make something like that...


    you can use this to assing instance names to all main timeline instances (that don't have name):

    var tl = fl.getDocumentDOM().getTimeline();

    var instanceObj = {};

    for(var i=0;i<tl.layers.length;i++){

    var framesA = tl.layers.frames;

    for(var j=0;j<framesA.length;j++){

    if(j==framesA.startFrame){

    var elementsA = framesA.elements;

    for(var k=0;k<elementsA.length;k++){

    if(elementsA.name==""){

    if(elementsA.instanceType=="symbol"){

    if(!instanceObj[elementsA.libraryItem.name]){

    instanceObj[elementsA.libraryItem.name] = true;

    elementsA.name = charF(elementsA.libraryItem.name);

    } else {

    var ii = 0;

    while(instanceObj[elementsA.libraryItem.name+ii]){

    ii++;

    }

    instanceObj[elementsA.libraryItem.name+ii] = true;

    elementsA.name = charF(elementsA.libraryItem.name+ii);

    }

    }

    }

    }

    }

    }

    }

    function charF(s){

    return s.split(" ").join("_");

    }

    2 replies

    JoãoCésar17023019
    Community Expert
    Community Expert
    December 12, 2017

    Actually it is possible.

    Because luckily Animate sets the layer name of each imported asset to the original PSD layer name and also keep the imported objects selected. So all we have to do is to save the selection and set each selected item name to its corresponding layer name.

    So here is the code:

    function renameInstances(importToLibrary, showDialog, showImporterUI)

    {

        if (importToLibrary === undefined) importToLibrary = false;   

        if (showDialog === undefined) showDialog = true;   

        if (showImporterUI === undefined) showImporterUI = false;

       

        var doc = fl.getDocumentDOM();

        var selection;

        var uri = fl.browseForFileURL("select", "Import File");

        doc.importFile(uri, importToLibrary, showDialog, showImporterUI);

        selection = doc.selection;

        doc.selectNone();

        for (var i = 0; i < selection.length; i++)

        {

            var name;

            var validName = /^[$A-Z_][0-9A-Z_$]*$/i;

           

            selection.selected = true;

           

            name = selection.layer.name;   

           

            if (selection.instanceType != "symbol")

            {

                doc.convertToSymbol("movie clip", name, "center");

               

                if (validName.test(name))

                    doc.selection[0].name = name;

               

                doc.selectNone();

               

                continue;

            }

           

            if (validName.test(name))

                selection.name = name;

           

            selection.selected = false;

        }

    }

    renameInstances(false, true, false);

    Please notice some things:

    - The script makes a basic name validation. So make sure to not name your PSD layers to invalid names for AS3, like names starting with number, that have blank space, special characters or reserved words like "var" or "Number". If the name is invalid, the script will leave the instance name empty.

    [OPTIONAL]

    - The last line (43) is where the function is called: renameInstances(importToLibrary, showDialog, showImporterUI). The three arguments you can pass are:

    • importToLibrary: if the imported assets will be added to stage or not. To add them to stage, set to false;

    • showDialog: wheter or not to call the Import dialog box. Set to true to show it.

    • showImporterUI: specifies whether to display errors visually (for example, using the Library Conflict dialog box). The default is false.

    Download the script here: rename_instances_from_psd_layers.jsfl. To use the script, drag and drop over the Animate IDE or go to Commands > Run Command....

    I hope it helps.

    Regards,

    JC

    kglad
    Community Expert
    Community Expert
    December 12, 2017

    you can use jsfl to assign instance names to eligible objects, but what kind of eligible objects are you importing from photoshop?

    rene76Author
    Participant
    December 12, 2017

    hello

    when I import .psd file I get movieclips with names created from psd layers' names. I just want to get something like that: .psd layer name -> adobe animate mc name -> aa instance name

    kglad
    Community Expert
    Community Expert
    December 12, 2017

    i see that, but don't know any way for the import dialog to automatically assign instance names.  jsfl is the only way i see to do that.