title: Getting updated availableFonts from NSFontManager while activating fonts in Cocoa
body: <p>I posted a similar question about using Python to do the same thing, but I rewrote this little program using objective-c and Cocoa and got the same result. Here's the other Stack Overflow question of mine: <a href="https://stackoverflow.com/questions/8980677/polling-nsfontmanager-sharedfontmanager-in-python">https://stackoverflow.com/questions/8980677/polling-nsfontmanager-sharedfontmanager-in-python</a> The purpose of submitting the question again is to reach out to the Objective-C and Cocoa experts, and who knows, maybe the answer is different. So here it goes.</p>
  
  <p>I'm trying to write a small little terminal program that will get a list of fonts that OS X sees as active, and report how many are active, over a period of time. For example, while the program is running, if I have 120 fonts active, it should report 120 fonts active, then if I activate 10 more fonts  while the program is running, it should report 130 fonts active. The issue I'm running into is that it doesn't seem like the sharedFontManager instance is updating in my for loop. Here's the code I'm working with:</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  #import &lt;AppKit/AppKit.h&gt;
  
  int main (int argc, const char * argv[])
  {
  
      @autoreleasepool {
          for(int y=0; y &lt; 5; y++){
  
          NSArray *sysFonts;
          NSFontManager *manager = [NSFontManager sharedFontManager];
          sysFonts = [manager availableFonts];
  
          NSLog(@"Number of active fonts: %lu", [sysFonts count]);
          sleep(5);
          sysFonts = nil;
          }
  
      }
  return 0;
  }
  </code></pre>
  
  <p>So if you'd like to test this you'll need a couple hundred fonts to activate and deactivate. Start with your fonts deactivated (select the fonts, right-click, select "Disable Fonts"). Run the program. When you see the terminal say "Number of active fonts: " pop back into Font Book, right-click on the selected fonts and click "Enable Fonts" (feel free to change sleep(5) to something longer or shorter. The number is in seconds), it will repeat the loop 5 times (again, you can change this number too).</p>
  
  <p>The results I keep ending up with are the same number of activated fonts, but I know this is inaccurate because if you have activated a large number of fonts (somewhere around 500 fonts), you have enough time to re-run the program and see that the number has decreased or increased from the previous time you ran the program, depending on if you activated or deactivate the large number of fonts. In other words, I know font activation is occurring, and I expect to see the number of fonts changing.</p>
  
  <p>Any help or guidance would be much appreciated.</p>