Skip to content

Instantly share code, notes, and snippets.

@diamantidis
Last active December 8, 2023 22:46
Show Gist options
  • Select an option

  • Save diamantidis/7dfa8de52aa2f36a3c64ff30a16dd22a to your computer and use it in GitHub Desktop.

Select an option

Save diamantidis/7dfa8de52aa2f36a3c64ff30a16dd22a to your computer and use it in GitHub Desktop.
How to use AppleScript to generate an enum for the SF Symbols
activate application "SF Symbols"
tell application "System Events"
tell process "SF Symbols"
-- Click the “list” radio button.
click radio button 2 of radio group 1 of group 3 of toolbar 1 of window 0
tell outline 1 of scroll area 1 of splitter group 1 of window 0
--- select (row 1 where value of static text 1 of UI element 1 starts with "All")
select (row 1 where value of static text 1 of UI element 1 starts with "Devices")
end tell
set enumCases to ""
repeat with sfSymbolRow in rows of table 1 of scroll area 1 of group 1 of splitter group 1 of window 0
set sfSymbolName to value of static text 1 of UI element 2 of sfSymbolRow
set caseIdentifier to my toCamelCase(my handleNameRestrictions(sfSymbolName))
set enumCases to enumCases & " case " & caseIdentifier & " = \"" & sfSymbolName & "\"
"
end repeat
set startOfEnum to "public enum SFSymbol: String, CaseIterable {
"
set endOfEnum to "}"
set enum to startOfEnum & enumCases & endOfEnum
set the clipboard to {text:(enum as string), Unicode text:enum}
enum
end tell
end tell
on handleNameRestrictions(theText)
set theNewText to theText
set firstCharacter to text 1 of theText
if isNumber(firstCharacter) then
set theNewText to "number" & theText
end if
return convertReservedKeywords(theNewText)
end handleNameRestrictions
on isNumber(theString)
try
set theString to theString as number
return true
on error
return false
end try
end isNumber
on convertReservedKeywords(theText)
set keywords to {"repeat", "return", "case"}
set theNewText to theText
if keywords contains (theText as string) then
set theNewText to "`" & theText & "`"
end if
return theNewText
end convertReservedKeywords
on toCamelCase(theText)
set theNewText to ""
set dotIsFound to false
repeat with aCharacter in theText
if (aCharacter as string) is equal to "." then
set dotIsFound to true
else
if dotIsFound then
set theNewText to (theNewText & toCapitalized(aCharacter)) as string
else
set theNewText to theNewText & aCharacter
end if
set dotIsFound to false
end if
end repeat
return theNewText
end toCamelCase
on toCapitalized(theText)
set theNewText to ""
set theComparisonCharacters to "abcdefghijklmnopqrstuvwxyz"
set theSourceCharacters to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set firstCharacter to text 1 of theText
set stringLength to the length of theText
set restOfText to ""
if stringLength is greater than 1 then
set restOfText to text 2 thru -1 of theText
end if
set theOffset to offset of firstCharacter in theComparisonCharacters
if theOffset is not 0 then
set theNewText to (theNewText & character theOffset of theSourceCharacters & restOfText) as string
else
set theNewText to (theNewText & firstCharacter & restOfText) as string
end if
return theNewText
end toCapitalized
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment