Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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:
Copy link to clipboard
Copied
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:
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Hi Tom, thanks for sharing your script, it's really cool. I don't have much to say about your code, I would have written the script in the same style as you did.
I have a question though, why? did you need to have your code in Illustrator? I looks really nice.
Copy link to clipboard
Copied
Hi CarlosCanto!
You`re welcome
Why illustrator? Because it`s useful for making documentation and illustrations for a programming language, like the one i`m making.
Later on i can then export it to pdf.
Also, you can make scripts inside illustrator and with a little execute or eval you can test them inside illustrator itself!
I mean how cool is that
Thanks for the feedback
Copy link to clipboard
Copied
Ok I can see how it can be used for documentation.
How do you run it though? Do you open the file and read its contents from an outside script?
It's a really cool project, I love it. But that's not how you normally write and run your scripts, is it?
Copy link to clipboard
Copied
I used to write stuff inside Notepad++ and then copy the code with syntax highlighting as rtf text and everything (because that's an option in N++) and paste it in Illustrator so this project makes things easier for me
"How do you run it though?"
That's totally up to the user of my code, for the moment i write the code inside illustrator on the fly and press a hotkey that triggers an action that runs the syntax highlight script.
it would be easy to implement some sort of code to select a file or something, even to apply the correct syntax for more then one file type if you like
"But that's not how you normally write and run your scripts, is it?"
No, if i have a big project that i'm scripting i use an editor like the one from Adobe itself, vbsedit (with object browser!) or notepad++ or something.
Copy link to clipboard
Copied
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..
Copy link to clipboard
Copied
Can't seem to get this to work, do you mind posting a .js version? Would love to use this!