-
1. Re: Fireworks: How to find document index for scripting?
groove25 Jan 23, 2012 6:12 PM (in response to groove25)In case anyone's interested, here's the workaround I'm now using to find the position of an active Fireworks document within the array of open documents (a.k.a., the document index). I'd still love to hear from anyone who can suggest a simpler method.
The basic idea is to first look at two properties of the active document—docTitleWithoutExtension and filePathForRevert—and, where possible, compare those values to that of the same properties within each open document. On their own, each property has loopholes, which is why I'm combining them. (For example, it's possible to have two documents with the same title—if they have different extensions or file paths. Likewise, it's possible to open multiple copies of an unsaved document, all with the same filePathForRevert value.)
New or untitled documents demand an entirely different criteria. I'm not crazy about this approach, but it seems like the best option at this point: When you can't find a DOM property to reliably distinguish one document from another, you temporarily write a distinctive value into a property of the document you want to find. Think of it like tagging a wild animal. In this case, when the active document lacks both a docTitle and file path, I write a crazy piece of "alt text" gibberish into the defaultAltText property and use that to identify my active document. (Even though I'm taking care to restore the original "alt text" afterwards, I don't love this method... but it seems to work.)
var dom = fw.getDocumentDOM();
function documentIndex() {
if (fw.documents.length == 1) {
return 0;
}
else if (fw.documents.length > 1) {
var docTitle = dom.docTitleWithoutExtension;
var filePath = dom.filePathForRevert;
if ((docTitle != "") && (filePath != null)) {
var i = 0;
for (i = 0; i < fw.documents.length; i++) {
if ((fw.documents[i].docTitleWithoutExtension == docTitle) && (fw.documents[i].filePathForRevert == filePath)) {
return i;
}
}
}
else {
var originalDefaultAlt = dom.defaultAltText;
dom.defaultAltText = "toRtoiSeOfThEsLowAcoRnsRejoiCe";
var i = 0;
for (i = 0; i < fw.documents.length; i++) {
if (fw.documents[i].defaultAltText == dom.defaultAltText) {
dom.defaultAltText = originalDefaultAlt;
return i;
}
}
}
}
}