使用 jsfl 批量重命名 Flash 库项目
版本 flash cs5
好的,所以我知道重命名所有选定库项目的通用代码
var items = fl.getDocumentDOM().library.getSelectedItems();
for (var i=0; i<items.length; i++){
var item = items[i];
item.name = "ABC_"+item.name;
}
,但是如果库项目位于文件夹中,这还不够好...因为 item.name 返回完整路径,但 item.name 设置名称。 oO 正如其他人在这里指出的,http://forums.adobe.com/message/107718
- 因此,当我尝试将 Level1 重命名为 ABC_Level1 时,
- 如果 Level1 的文件夹路径是 LIBRARY/FolderA/FolderB/Level1,
- 我会得到这个
- ABC_FolderA-FolderB-Level1 strong>
我可能可以编写某种字符串解析器,如下所示,
item.name = "ABC_"+item.name.substr(item.name.lastIndexOf("-"), 99)
但是这真的很丑陋,如果库项目已经包含“-”,则无法工作。例如“Level-1”
,所以我想我希望的是一种不同的方式来访问名称,它只返回名称而不是而不是路径
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这很棘手,因为当您获取名称时,它是完整路径,但是当您设置名称时,它只是项目名称(而不是路径)。在连接之前必须将名称和文件夹分开。因此,没有一种“干净”的方法来做到这一点,尽管编写一个函数可能会使其更具可读性:
然后这样设置项目的名称:
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:
Then set the name of the item thusly:
如果我没记错的话,JSFL 就像大多数 JavaScript 语言实现一样,是一种基于原型的语言。这意味着,您可以向现有的内置对象添加新的属性/方法。理论上,您可以使所有库项目都具有“getShortName()”方法,其作用与@Justin Putney 的解决方案相同。
大致意思是:
这使得它成为扩展 JSFL 功能的一种简洁而方便的方法。理想情况下,您只想运行此代码片段的第一位(方法定义),因为只要您的 Flash IDE 运行,它们就会持续存在。
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:
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.
在 JSFL 中添加属性是可能的,但您永远不应该向本机原型添加属性,因为这会修改 Flash 中的所有对象,特别是在 for..in 迭代中抬起其丑陋的头。 Adobe 的 IK 工具实际上在内部使用了 for..in 循环,而它们本应使用 for 循环。一旦扩展了 Object.prototype,您就会注意到这一点...当您与舞台交互时,输出面板 (CS4) 将开始充满错误!
在我的测试中,我无法扩展 Item (也许因为它内部是一个抽象类?),但我可以扩展 SymbolItem (因此您还需要扩展所有其他 Item 变体):
使用 getter 还可以保护您免受对于我刚才提到的问题。
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):
Using getters also protects you from the for..in issue I just mentioned as well.