JSFL Rename Folder

May 26th, 2008 by Steven Sacks

JSFL does not have a method to rename a folder, which means you have to create a new folder with the new name, copy all the contents of the old folder into the new one, and then delete the old folder. It's a bit of a pain.

Here is the JSFL code to do this until Adobe sees fit to add a rename folder method. You can also use this to move a folder from one place to another.

function renameFolder(sourcePath, targetPath)
{
    FLfile.createFolder(targetPath);
    copyFolders(sourcePath, targetPath, FLfile.listFolder(sourcePath, "directories"));
    copyFiles(sourcePath, targetPath, FLfile.listFolder(sourcePath, "files"));
    FLfile.remove(sourcePath);
}
function copyFolders(sourcePath, targetPath, folderList)
{
    var i = folderList.length;
    while (i--)
    {
        FLfile.createFolder(targetPath + "/" + folderList[i]);
        copyFiles(sourcePath + "/" + folderList[i], targetPath + "/" + folderList[i], FLfile.listFolder(sourcePath + "/" + folderList[i], "files"));
        copyFolders(sourcePath + "/" + folderList[i], targetPath + "/" + folderList[i], FLfile.listFolder(sourcePath + "/" + folderList[i], "directories"));
    }
}
function copyFiles(sourcePath, targetPath, fileList)
{
    var i = fileList.length;
    while (i--)
    {
        FLfile.copy(sourcePath + "/" + fileList[i], targetPath + "/" + fileList[i]);
    }
}

Posted in JSFL, Tips/Tricks

4 Responses

  1. Timbo

    Thanks for the great JSFL help on your blog! Is it also possible to convert layers to a movieclip?

    I have about 1200 layers, on each layer 1 group.. and i like to convert each layer / group to a movieclip.

    Thanks in advance,
    Tim.

  2. Robert Penner

    Actually, there are a couple of JSAPIs that can rename folders, but the UI doesn't get updated right away.

    You can use either of these:

    fl.getDocumentDOM().library.renameItem("new name");

    var selItems = fl.getDocumentDOM().library.getSelectedItems();
    var item0 = selItems[0];
    item0.name = 'foo';
    fl.trace('item name: ' + item0.name);

    The name will be changed, but will not appear to be until you force a library refresh, e.g. by adding or renaming another item or updating use counts.

  3. Robert Penner

    Oops, I didn't really read your code before replying. I just assumed you were talking about library folders.

  4. Sven

    I'm trying to achieve, in my world a very simple task, of adding a (very large) selection of 'groups' to the library and edit them one by one (adding a layer, draw a rectangle).

    I can convert the symbols fine. I can enter edit mode just fine. I can add a layer just fine. And I can draw a rectangle just fine.
    My problem is that the added rectangle is placed in the top left corner of the stage, and not the top left corner of the symbol. And how do I specify the rectangle to the width and height of the symbol itself addNewRectangle({left:0, top:0, right: width, bottom: height}, 0)?

    Sorry, if this is not the place to ask things like this, but I've been fighting this for days, and can't seem to find anywhere else to go on Google.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

About Steven Sacks

I am a professional Flash developer with over 13 years of programming experience. I have consulted for high-profile agencies and companies in San Francisco, Los Angeles, Atlanta and New York, and developed numerous award-winning websites and rich internet applications for clients including Adobe, Fox Sports, FX Networks, Anheuser-Busch, GE, DirecTV, ESPN, The Weather Channel, Home Depot, and Coca-Cola.

I am the author of the open-source Gaia Framework for Adobe Flash, which dramatically reduces development time and makes developing Flash sites much easier.