making a code syntax highlighter for illustrator
i'm posting my script here for other people who might need this and to ask for your opinion.
are there people who have suggestions or comments about code that can be written better?
i use vb but i can convert to js if needed, it seems vbscript is no longer supported in ai 2019?
you need to make the character styles and use the style and color for them as you wish in your illustrator document,
then select your text frame and open the script ![]()
Set app = CreateObject("illustrator.application")
Set doc = app.ActiveDocument
sel = app.Selection
'##################################################################################################################################
Set reg = New RegExp
With reg
.Global = True
.IgnoreCase = True
.Pattern = "'([^\r\n']|'')*'|#[^\r\n]*|[a-z_][a-z_0-9]*|[0-9]+|[+\-*/&>=!<(),\[\]]|\s+|\S+"
End With
Set regex = CreateObject("scripting.dictionary")
With regex
.Add "comment", "^#[^\r\n]*$"
.Add "string", "^'([^\r\n']|'')*'$"
.Add "number", "^(-?[0-9]+)$"
.Add "symbol", "^([+\-*/&>=!<(),\[\]])$"
.Add "keyword", "^(And|Or|Not|Show|Hide|Write|Clear|Message|Move|Beep|Click|Send|Wait|Sub|Call|If|Else(If)?|For|To|End|Abort)$"
.Add "macro", "^(Input|YesNo|True|False|Random)$"
End With
Function match(text, pattern)
reg.Pattern = regex(pattern)
match = reg.Test(text)
End Function
Sub append(w, c)
doc.CharacterStyles(c).applyTo sel(0).characters.add(w)
End Sub
'##################################################################################################################################
Set code = reg.Execute(sel(0).contents)
sel(0).contents = ""
doc.characterStyles.getByName("default").applyTo sel(0).textrange
For Each w In code
change = "default"
For Each c In regex
If match(w.value, c) Then
change = c
Exit For
End if
Next
append w.value ,change
Next
