bulk rename flash library items with jsfl

4.4k views Asked by At

version flash cs5

ok so i know the general code to rename all selected library items

var items = fl.getDocumentDOM().library.getSelectedItems();
for (var i=0; i<items.length; i++){
var item = items[i];
item.name = "ABC_"+item.name;
}

but this isn't good enough if the library items are in folders... because item.name returns the full path, but item.name sets the name. o.O as someone else points out here, http://forums.adobe.com/message/107718

  • so when i try to rename Level1 to be ABC_Level1
  • if Level1's folder path is LIBRARY/FolderA/FolderB/Level1
  • i get this instead
  • ABC_FolderA-FolderB-Level1

i could probably code some sort of string parser something like this,

item.name = "ABC_"+item.name.substr(item.name.lastIndexOf("-"), 99)

but that is really ugly and would not work if library items contained "-" already. "Level-1" for example

so i guess what I'm hoping for is a different way to access the name that returns just the name and not the path

3

There are 3 answers

2
Justin Putney On BEST ANSWER

It's tricky because when you get the name it's the full path, but when you set the name, it's just the item name (and not the path). You have to separate the name and the folder before concatenating. So, there isn't a "clean" way to do it, though writing a function might make it more readable:

function getItemName(item) {
   return item.name.split("/").pop();
}

Then set the name of the item thusly:

 item.name = "ABC_" + getItemName(item);
2
chamberlainpi On

If I'm not mistaking - JSFL, just like most JavaScript language implementations, is a prototype-based language. That means, you can add new properties / methods to existing built-in objects. You could in theory make all Library items have a "getShortName()" method doing the same as @Justin Putney's solution.

Something along the lines of:

Object.prototype.addMethod = function(name, pMethod) {
  this.prototype[name] = pMethod;
}

Function.prototype.addMethod = function(name, pMethod) {
  this.prototype[name] = pMethod;
}

Object.addMethod( "getShortName", function() {
  return this.name.split("/").pop();
});

/*
  NOTE: SymbolItem.addMethod was causing a bug, so Object, although it's generic,
  seems like the best choice.
*/

fl.trace( fl.getDocumentDOM().library.items[0].getShortName() );

This makes it a neat convenient way to extend functionality in JSFL in general. Ideally you just want to run the first bit of this snippet ONCE (the method definitions) since they will persist for as long as your Flash IDE is running.

0
Dave Stewart On

Adding properties in JSFL is possible, but you should never add properties to the native prototypes, as that will modify ALL objects within Flash, specifically rearing its ugly head in for..in iteration. Adobe's IK Tools actually use a for..in loop internally, where they should have been using a for loop. You'll notice this as soon as you extend Object.prototype... the output panel (CS4) will start filling up with errors when you interact with the stage!

It my tests, I couldn't extend Item (perhaps because it is an abstract class internally?) but I could extend SymbolItem (so you'd need to extend all the other Item variants as well):

SymbolItem.prototype.__defineGetter__('shortName', function(element){ return this.name.split('/').pop(); })
SymbolItem.prototype.__defineGetter__('path', function(element){ return this.name; })

trace(fl.getDocumentDOM().library.items[0].shortName);

Using getters also protects you from the for..in issue I just mentioned as well.