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
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
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With