Skip to content

Instantly share code, notes, and snippets.

@ChristoferK
Last active May 19, 2019 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChristoferK/87cc11bf9891273f0cbb7302f6816b95 to your computer and use it in GitHub Desktop.
Save ChristoferK/87cc11bf9891273f0cbb7302f6816b95 to your computer and use it in GitHub Desktop.
[Objective-C Classes List] Retrieves an ordered, filtered list of Objective-C NSObject subclasses and writes them to file. #JXA #JavaScript #ObjectiveC #ObjC #classes #objc_copyClassList
ObjC.import('stdlib');
ObjC.import('Foundation');
__NSString__ = $.NSString.stringWithString.bind($.NSString);
ObjC.bindFunction('objc_copyClassList', ['void**', ['int']]);
ObjC.bindFunction('class_getName', ['char *', ['void*']]);
ObjC.bindFunction('class_getSuperclass', ['void*', ['void*']]);
ObjC.bindFunction('class_isMetaClass', ['bool *', ['void*']]);
i = 0; // Counter variable
className = undefined; // An individual Objective-C class name
classNames = ["NSObject"]; // An array of Objective-C class names
re = /^[A-Z]{3}[^_]+$/; // RegEx pattern filtering class list
dir = '~/Documents/Scripts/AppleScript/data/' // Output folder
file = 'NSCLASSES'; // Filename to write out to
fp = __NSString__(dir + file).stringByStandardizingPath;
utf8 = $.NSUTF8StringEncoding;
objc_classes = $.objc_copyClassList(undefined);
while (className != 'nil') {
objc_class = objc_classes[i++];
className = $.class_getName(objc_class);
superName = $.class_getName($.class_getSuperclass(objc_class));
if (superName=="NSObject") { classNames.push(className); }
}
$.free(objc_classes);
$.free(objc_class);
__NSString__(classNames.filter(x=>x.match(re)!=null).sort().join('\n'))
.writeToFileAtomicallyEncodingError(fp,true,utf8,$());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment