Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bulk rename flash library items with jsfl

Tags:

flash

rename

jsfl

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

like image 572
SketchBookGames Avatar asked Jan 21 '23 10:01

SketchBookGames


2 Answers

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);
like image 115
Justin Putney Avatar answered Feb 01 '23 07:02

Justin Putney


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.

like image 43
chamberlainpi Avatar answered Feb 01 '23 08:02

chamberlainpi