Copy link to clipboard
Copied
Getting a list of all open documents without actually displaying them usually works well, although sometimes, especially after a Save As (kAISaveAsCommandPostNotifierStr), it crashes. Here's the code, which resides in a for loop. DocCount has been correctly retrieved (3) and document 0 gets handled correctly. When it comes to item 1, however, it works perfectly when just opening or closing files, but crashes with an EXC_BAD_ACCESS error in the compiler (Xcode 8 with Mac CC 2017) at Activate on item 1 after a Save As. The value of "error" returned from AI is always 0.
AIErr error = kNoErr;
ai::UnicodeString fullPathString;
AIDocumentHandle doc = NULL;
for (uint32_t i = 0; i < (docCount); i++) {
error = sAIDocumentList->GetNthDocument(&doc, i);
error = sAIDocumentList->Activate(doc, false); // error = 0 here but crashes if i>0 and just did Save As
ai::FilePath filep;
error = sAIDocument->GetDocumentFileSpecification(filep);
error = sAIFilePath->GetFullPath(filep, true, fullPathString);
std::string sFullPath = fullPathString.as_UTF8();
if (sFullPath != "/") this->vOpenFiles.push_back(sFullPath); // save in string vector
}
Last, it activates what was/is the current doc. Am I overlooking something I should have included here?
TIA -- Rick
Sounds like the sort of thing that happens when you are not in the correct plugin context, but if the code is called in response to a notifier message the context should be set correctly.
What is the value of doc when it crashes, is it NULL?
You could try the old "set a zero timer" trick so your code is called after Illustrator has finished whatever it was doing.
Copy link to clipboard
Copied
Sounds like the sort of thing that happens when you are not in the correct plugin context, but if the code is called in response to a notifier message the context should be set correctly.
What is the value of doc when it crashes, is it NULL?
You could try the old "set a zero timer" trick so your code is called after Illustrator has finished whatever it was doing.
Copy link to clipboard
Copied
Thank you (again) LeoTaro! The zero timer trick worked. Or at least I can't make it crash again. All the best, my friend -- Rick