Bulk renaming flash elements using jsfl

flash version cs5

ok, so I know the general code for renaming 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 is not good enough if library items are in folders ... because item.name returns the full path, but item.name sets the name. oO, as someone else points out, http://forums.adobe.com/message/107718

  • so when I try to rename Level1 as ABC_Level1
  • if the path to the Level1 folder is LIBRARY / FolderA / FolderB / Level1
  • I get it instead
  • ABC_FolderA-FolderB-Level1

Maybe I could code some kind of parser like this,

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

but it is really ugly and will not work if the elements of the library contain a "-" already. "Level 1" for example

so I guess I hope this is a different way to access the name that returns only the name and not the path

+4
source share
3 answers

This is complicated because when you get the name, this is the full path, but when you set the name, it's just the name of the element (not the path). Before concatenation, you need to separate the name and folder. Thus, there is no β€œclean” way to do this, although writing a function can make it more readable:

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

Then set the element name like this:

  item.name = "ABC_" + getItemName(item); 
+8
source

If I'm not mistaken - JSFL, like most JavaScript implementations, is a prototype language. This means that you can add new properties / methods to existing built-in objects. Theoretically, it would be possible for all library elements to have a getShortName () method that does the same as the @Justin Putney 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 generic, seems like the best choice. */ fl.trace( fl.getDocumentDOM().library.items[0].getShortName() ); 

This makes it a convenient convenient way to extend functionality in JSFL as a whole. Ideally, you just want to run the first bit of this ONCE (method definition) snippet, as they will persist until the Flash IDE runs.

+1
source

Adding properties to JSFL is possible, but you should never add properties to native prototypes, as this will change ALL objects in Flash, in particular, to expand your ugly head for ... iteration. Adobe IK Tools actually uses the for..in loop inside where they were supposed to use the for loop. You will notice this as soon as you expand Object.prototype ... the results pane (CS4) begins to fill up with errors when interacting with the scene!

These are my tests, I could not extend Item (perhaps because it is an abstract class inside?), But I could extend SymbolItem (so you would also need to extend all other options for Item):

 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 problem that I just mentioned.

0
source

Source: https://habr.com/ru/post/1340672/


All Articles