Information about spaces is stored in the dock settings. You can access the settings through NSUserDefaults to get the number of rows and columns, and the number of spaces is a product:
NSDictionary *dockPrefs = [[NSUserDefaults standardUserDefaults] persistentDomainForName:@"com.apple.dock"]; int rows = [[dockPrefs objectForKey:@"workspaces-rows"] intValue]; int cols = [[dockPrefs objectForKey:@"workspaces-cols"] intValue]; int nspaces = rows * cols;
user557219
source share