New version of "Get super classes"

foundation
asobjc

#1

I fixed the miss spelling of “case sensitive” in the script listing in the post, and in the applet (Bill Kopp 2/16/2018).

“Get super classes” is an applet that take a class name and returns a list of parent classes from, the class in question, up to NSObject. This is of value in ASObj-C because the classes inherit abilities from their parent classes. For example if “NSMutableArray” is entered the applet will return “NSObject–>NSArray–>NSMutableArray”. This means that things defined in NSMutableArray, NSArray and NSObject can be used for NSMutableArray.

If a person only looks in the documentation (Xcode, Dash, ASObj-C database, …) for NSMutableArray then they are missing a lot of things that could be used while working with a NSMutableArray.

The old version only worked for the “Foundation” framework. The new version works for “Foundation,” “AppKit,” “Quartz” and Carbon." What determine what the applet works on is the “use framework” statements at the beginning of the script. To add another framework you only have to add another “use framework” statement.

I have included the newest version of the applet in this post and the AppleScript code for the applet is listed below.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

use framework "Foundation"
use framework "AppKit"
use framework "Quartz"
use framework "Carbon"


on GetStringState(TheString)
	-- Make sure TheString is really a string
	set TheString to TheString as text
	
	set LowercaseFound to false
	set UpperCaseFound to false
	
	set StringCharacter to characters of TheString
	repeat with TheCharacter in StringCharacter
		if (not LowercaseFound) then
			set LowercaseFound to ((id of "a") ≤ (id of TheCharacter)) and ((id of TheCharacter) ≤ (id of "z"))
		end if
		if (not UpperCaseFound) then
			set UpperCaseFound to ((id of "A") ≤ (id of TheCharacter)) and ((id of TheCharacter) ≤ (id of "Z"))
		end if
	end repeat
	
	if (UpperCaseFound and LowercaseFound) then set State to "MixedCase"
	if (not UpperCaseFound and not LowercaseFound) then set State to "NoLettersFound"
	if (UpperCaseFound and not LowercaseFound) then set State to "AllUpperCase"
	if (not UpperCaseFound and LowercaseFound) then set State to "AllLowerCase"
	return State
end GetStringState

on GetClassNameAsString(TheClass)
	-- Returns a string representation of the given class.
	return (current application's NSStringFromClass(TheClass's |class|())) as text
end GetClassNameAsString

on GetInheritancePath(CurrentClassName)
	script ReturnObj
		property Successful : false
		property ClassPath : ""
	end script
	
	local CurrentClass
	local ClassPath
	local NextClass
	local TheClass
	
	try
		if (CurrentClassName = "NSObject") then return CurrentClassName
		
		-- Create the class indicated by the given string.  This needs to be done first so
		-- later on the "super class" of the "current class" can be found.
		-- If the the string entered by the user does not equate to a class then 
		-- NSClassFromString returns missing value.
		set CurrentClass to current application's NSClassFromString(CurrentClassName)
		
		if (CurrentClass = missing value) then
			-- A valid class name was not entered
			if (CurrentClassName = "") then
				display dialog "No characters were entered as input." buttons {"OK"} default button "OK" with title "Error"
			else if ((length of CurrentClassName) < 4) then
				display dialog "Invalid input.  Only " & (length of CurrentClassName) & " character(s) was input" buttons {"OK"} default button "OK" with title "Error"
			else
				set StringStatus to GetStringState(CurrentClassName)
				if (StringStatus = "AllUpperCase") then
					display dialog "Invalid input.  All the text in the input, \"" & CurrentClassName & ",\" was all upper case." buttons {"OK"} default button "OK" with title "Error"
				else if (StringStatus = "AllLowerCase") then
					display dialog "Invalid input.  All the text in the input, \"" & CurrentClassName & ",\" was all lower case." buttons {"OK"} default button "OK" with title "Error"
				else if (StringStatus = "NoLettersFound") then
					display dialog "Invalid input.  There were no letters found in the input, \"" & CurrentClassName & "\"" buttons {"OK"} default button "OK" with title "Error"
				else
					display dialog "Invalid input.  The entered text \"" & CurrentClassName & ¬
						"\" does not appear to represent a valid class name." buttons {"OK"} default button "OK" with title "Error"
				end if
			end if
			set (Successful of ReturnObj) to false
			return ReturnObj
		end if
		
		set ClassPath to CurrentClassName
		
		-- This loop keeps getting the suppclass of the the current class until
		-- the current class is NSObject.  All class inherit from NSObject.
		repeat until (GetClassNameAsString(CurrentClass) = "NSObject")
			-- Get the parent class of the CurrentClass
			set NextClass to CurrentClass's superclass()
			
			-- Convert the class of NextClass to a string equivalent so
			-- the text can be can be appended to the ClassPath string and
			-- can be compared again at the top loop.
			set ClassName to GetClassNameAsString(NextClass)
			
			-- Add newest string representation of the parent class to the
			-- ClassPath.
			set ClassPath to ClassName & "-->" & ClassPath
			
			-- Start loop again with a new CurrentClass
			set CurrentClass to NextClass
		end repeat
		
		set (ClassPath of ReturnObj) to ClassPath
		set (Successful of ReturnObj) to true
		return ReturnObj
	on error errMsg number errNum
		display dialog "Error " & (errNum as string) ¬
			& " occured getting the inheritance path." & return & return & errMsg with title "Error" buttons {"OK"} default button "OK"
		set (Successful of ReturnObj) to false
		return ReturnObj
	end try
end GetInheritancePath

display dialog ¬
	"The class will be converted to an inheritance path" & return & ¬
	"The input is case sensitive." with title "Enter a class" buttons {"OK", "Cancel"} default button "OK" default answer ""

set StartingClass to text returned of result

set TheResult to GetInheritancePath(StartingClass as text)
if (Successful of TheResult) then
	display dialog ¬
		"The arrow points to the class that is inheriting from its parent." with title ¬
		"inheritance path" buttons {"OK"} default button "OK" cancel button "OK" default answer (ClassPath of TheResult)
else
	return false
end if

Get super classes 1.1.1.app.zip (65.9 KB)


"Get super classes" 1.2
(Phil Stokes) #2

Y’got a typo there, Bill.


#3

No that’s my special way of spelling sensitive :slight_smile:

Bill