Skip to main content
TomVbk
Participating Frequently
December 2, 2018
Question

making a code syntax highlighter for illustrator

  • December 2, 2018
  • 3 replies
  • 3074 views

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

3 replies

Participant
September 13, 2019

Can't seem to get this to work, do you mind posting a .js version? Would love to use this!

TomVbk
TomVbkAuthor
Participating Frequently
December 5, 2018

PROBLEM WITH MY CODE:

if i run the script and the font of my text field has no character style set already the text just keeps it's main color after running?

that's weird..

pixxxelschubser
Community Expert
Community Expert
December 2, 2018

Hi TomVbk​,

I do not use VB or VBS or VBA. But I see no reason why VB should not work in newer Adobe applications. Please explain in detail what happens. (I did not test your code yet)

Here in the forums only a 'handful' of programmers write some of her codes with VBS. One of them is CarlosCanto

If I understand your code right: You want formatting a text like a 'code format'. Is that what you want? Please give us an example (plain) text and an screenshot before and after running your code (and perhaps a [JS]-variant of your VB-code).

Does your code work correctly with older AI versions?

AI and scripting isn't IMO the best choice to do what you want. A few years ago I made an example in InDesign. See this older thread:

Formatting Javascript code

TomVbk
TomVbkAuthor
Participating Frequently
December 2, 2018

cc 2019: when i double click a vbs file it works, when i run i with the scripts menu in illustrator it says "Internal Script Execution error"

have a look art this: illustrator2019 vbs script error

older versions are no problem.

code format: yes i want to make a script that colorizes and makes words bold or not when i select a text frame.

i'm not very familiar with indesign but illustrator would do just fine for this kinda thing in my opinion

not sure if i use the right approach with my script tho? i first wanted to just select the tokens inside the textframe one by wone with a selection range and apply a rgbcolor to it.

what the code does:

  • get content of the selected textframe
  • removes the content from the textframe
  • apply's a regexp on the content and cuts it into tokens
  • put every token back into the textframe with the right characterstyle using doc.CharacterStyles("stylename").applyTo selection[0].characters.add("current token")

pixxxelschubser
Community Expert
Community Expert
December 2, 2018

Thank you for clarification.

You are right. The problem was already posted on Illustrator USER Voice. Give a vote to this thread (Bugs and feature request) VBS Scripts not able to run without error. – Adobe Illustrator Feedback

It seems to be better, meantime to work with your script only in CC2018 - or rewrite/translate your VBS-formatting.vbs into a VBS-formatting.JS

----------------------------------

(Only further information)

In this thread we did the opposite: change the contents and remain the formatting. But maybe there are useful informations for you within. ( [JS] only ) RegExp search and replace, keep original text formatting