Code: Select all
;-TOP Dump Object Methods
; by mk-soft, 29.12.2019 - 06.11.2022, v1.08.2
Structure ArrayOfMethods
i.i[0]
EndStructure
ImportC ""
class_copyMethodList(*Class, *p_methodCount)
; -> An array of pointers of type Method describing
; the instance methods implemented by the class
; Any instance methods implemented by superclasses are Not included
; You must free the array with free()
class_getName(*Class) ; -> UnsafePointer<Int8> -> *string
sel_getName(*Selector); -> const char *
method_getName(*Method) ; -> Selector
method_getTypeEncoding(*Method) ; -> const char *
method_getReturnType(*Method, *dst, dst_len) ; -> void
method_getNumberOfArguments(*Method) ; -> unsigned int
method_getArgumentType(*Method, index, *dst, dst_len) ; -> void
NSGetSizeAndAlignment(*StringPtr, *p_size, *p_align)
; -> const char *
; Obtains the actual size and the aligned size of an encoded type.
EndImport
; ----
Procedure.s GetArgumentType(*String)
Protected r1.s, arg.s, size.i, ofs.i
arg = PeekS(*String, -1, #PB_UTF8)
r1 + arg + " - "
If Left(arg, 1) = "^"
r1 + "A pointer to type of "
arg = Mid(arg, 2)
EndIf
Select arg
Case "c" : r1 + "A char "
Case "i" : r1 + "An int "
Case "s" : r1 + "A short "
Case "l" : r1 + "A long "
Case "q" : r1 + "A long long"
Case "C" : r1 + "An unsigned char "
Case "I" : r1 + "An unsigned int "
Case "S" : r1 + "An unsigned short "
Case "L" : r1 + "An unsigned long "
Case "Q" : r1 + "An unsigned long long "
Case "f" : r1 + "A float "
Case "d" : r1 + "A double "
Case "B" : r1 + "A C++ bool Or a C99 _Bool "
Case "v" : r1 + "A void"
Case "*" : r1 + "A character string (char *) "
Case "@" : r1 + "An object (whether statically typed Or typed id) "
Case "#" : r1 + "A class object (Class) "
Case ":" : r1 + "A method selector (SEL) "
Default:
NSGetSizeAndAlignment(*String, @size, @ofs)
r1 + "[" + Str(size) + " bytes]"
EndSelect
If Right(arg, 1) = "?"
r1 + "An unknown type (e.g. function pointer)"
EndIf
ProcedureReturn r1
EndProcedure
; ----
Procedure.s DumpObjectMethods(*Object, SuperLevel = 0, HidePrivate = #True, ShowEncoding = #False, FirstArgument = 2)
Protected r1.s, i, c, n, methodCount, Method.s
Protected *Class, *SuperClass, *Method, *Methods.ArrayOfMethods
Protected *String
*Class = object_getclass_(*Object)
If *Class
*String = AllocateMemory(1024)
r1 = PeekS(class_getName(*Class), -1, #PB_UTF8)
If SuperLevel
For i = 1 To SuperLevel
*SuperClass = class_getsuperclass_(*Class)
If *SuperClass
*Class = *SuperClass
r1 + " -> " + PeekS(class_getName(*Class), -1, #PB_UTF8)
Else
Break
EndIf
Next
EndIf
*Methods = class_copyMethodList(*Class, @methodCount)
r1 + #LF$ + #LF$ + "Count of Methods: " + methodCount + #LF$ + #LF$
For i = 0 To methodCount - 1
*Method = *Methods\i[i];
Method = PeekS(sel_getName(method_getName(*Method)), -1, #PB_UTF8)
If HidePrivate And Left(Method, 1) = "_"
Continue
EndIf
r1 + "Method " + Method + #LF$
If ShowEncoding
r1 + " * Encoding " + PeekS(method_getTypeEncoding(*Method), -1, #PB_UTF8) + #LF$
EndIf
method_getReturnType(*Method, *String, 1024)
r1 + " -- ReturnType = " + GetArgumentType(*String) + #LF$
c = method_getNumberOfArguments(*Method)
For n = FirstArgument To c - 1
method_getArgumentType(*Method, n, *String, 1024)
r1 + " -- Argument " + Str(n - FirstArgument + 1) + " = " + GetArgumentType(*String) + #LF$
Next
r1 + #LF$
Next
r1 + "End Class" + #LF$ + #LF$
If *Methods
free_(*Methods)
EndIf
FreeMemory(*String)
Else
r1 = "Object is nil" + #LF$
EndIf
ProcedureReturn r1
EndProcedure
; ****
Macro CocoaString(NSString)
PeekS(CocoaMessage(0, NSString, "UTF8String"), -1, #PB_UTF8)
EndMacro
Procedure.s AppleScript(Script.s)
Protected retVal.s, strVal, numItems, i
Protected obj_pool
Protected obj_AppleScript, obj_Script, obj_EventDescriptor, obj_Descriptor, obj_ErrorInfo
obj_pool = CocoaMessage(0, 0, "NSAutoreleasePool new")
obj_AppleScript = CocoaMessage(0, 0, "NSAppleScript new")
Debug DumpObjectMethods(obj_AppleScript, 0) ; Class
Debug DumpObjectMethods(obj_AppleScript, 1) ; SuperClass
If obj_AppleScript
obj_Script = CocoaMessage(0, obj_AppleScript, "initWithSource:$", @Script)
If obj_Script
obj_EventDescriptor = CocoaMessage(0, obj_Script, "executeAndReturnError:", @obj_ErrorInfo)
If obj_EventDescriptor
numItems = CocoaMessage(0, obj_EventDescriptor, "numberOfItems")
If numItems
For i = 1 To numItems
obj_Descriptor = CocoaMessage(0, obj_EventDescriptor, "descriptorAtIndex:", i)
strVal = CocoaMessage(0, obj_Descriptor, "stringValue")
If strVal
retVal + CocoaString(strVal)
If i <> numItems : retVal + #LF$ : EndIf
EndIf
Next
Else
strVal = CocoaMessage(0, obj_EventDescriptor, "stringValue")
If strVal
retVal = CocoaString(strVal)
EndIf
EndIf
Else
If obj_ErrorInfo
strVal = CocoaMessage(0, obj_ErrorInfo, "objectForKey:$", @"NSAppleScriptErrorMessage")
If strVal
retVal = "[ErrorInfo] " + CocoaString(strVal)
EndIf
EndIf
EndIf
EndIf
EndIf
CocoaMessage(0, obj_pool, "release")
ProcedureReturn retVal
EndProcedure
CompilerIf #PB_Compiler_IsMainFile
Define script.s, result.s
;script = "tell application " + Chr(34) + "Finder" + Chr(34) + " To get the name of every item in the desktop"
;script = ~"tell app \"Finder\" \n beep \n delay 1 \n beep \n end tell"
script = ~"tellx app \"Finder\" \n beep \n delay 1 \n beep \n end tell" ; Error
result = AppleScript(script)
If Left(result, 11) = "[ErrorInfo]"
Debug result
Else
Debug result
EndIf
CompilerEndIf