Benches macOS

11

How to create a simple text color script?

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

Hi, I need a script for school that will type all consonants in black and vowels in red using Office Word. I'm sure there is an easy way to do this but I know little about scripting. Does anyone know a script that will do this. Your help is much appreciated.

11 replies

Sign in or join Beachball Help to reply.

  1. 0

    Camelot

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    It's not that hard to do - you just need an iterator to look at each character. I don't have Word, but this TextEdit-based example should give you an idea:

    property vowels : "aeiouAEIOU"

    tell application "TextEdit"

    tell document 1

    set i to count characters

    repeat with j from 1 to i

    if character j is in vowels then

    set color of character j to {65535, 0, 0}

    else

    set color of character j to {0, 0, 0}

    end if

    end repeat

    end tell

    end tell

  2. 0

    jpimbert

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    In France, 'y' is also a vowel.

  3. 0

    Camelot

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    In France, 'y' is also a vowel.

    Of course... but the use of a property to list the characters makes it easy to accommodate. You can also include accented characters ( é, â, etc.) in the list to include them if required.

  4. 0

    Matthew Jackson

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Thanks a lot for your input. Do you think the text written with this script will import into Word? Also, how do I get your script into Text Edit? Do I copy /paste it somewhere? I know nothing about text edit. Thanks for your great help.

  5. 0

    Camelot

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Do you think the text written with this script will import into Word?

    By this do you mean "can I copy the colored text from TextEdit and paste it into Word'? Probably - I don't see why not. It might even work directly in Word if you change the 'tell application' line to use Word rather than TextEdit, but I can't confirm that.

    Also, how do I get your script into Text Edit?

    Umm.. copy/paste, but that's not going to do any good. You don't want this script in TextEdit at all - it's an AppleScript and should be pasted into a new script document in Script Editor (or AppleScript Editor, depending on your OS version). When you click Run in the Script Editor it will drive TextEdit (or, potentially, Word) to make the changes as discussed.

  6. 0

    Noaks

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Maybe it's clear but it isn't to me. Can we clarify exactly what you want to accomplish?

    1) My assumption is that you have one or more existing Word documents, with existing text, on a MAC computer (running system 10.4 or later) where you want the vowels (however those are defined in your situation) to show in red. Is that correct?

    2) Is there any need to display the modified (red text for vowels) Word document(s) on a windows computer?

    Message was edited by: Noaks

  7. 0

    Matthew Jackson

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Camelot, Thanks again for your assistance. I tried changing the tell application line to Word and get the error "Can’t make character 1 of document 1 of application "Microsoft Word" into type Unicode text." number -1700 from character 1 of document 1 to Unicode text So there seems to be a problem I can't fix. Even so, I was able to type a text in word, copy it into Text Edit and run the script. I could then copy paste into a program called Notebook which is used on an interactive whiteboard - my intended goal. A lot of steps, but it worked. Many thanks. I'll keep looking for a script that will work directly in Word to save some steps. Thanks for all your help.

  8. 0

    Matthew Jackson

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hi Noaks, Well, I need a script that I can use directly in Word that will type all vowels in red and consonants in black - it will be used as a phonics program for elementary school. I then will import the text into a program called Notebook that is used on a Smartboard (interactive whiteboard). Yes, the program is used on a PC as well as Mac through the Smartboard. Hope that helps.

  9. 0

    Hiroto

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hello

    Also you may try something like the code below.

    It will edit the front document in Word. Not tested, though.


    set voyelles to "aeiouy"
    ignoring diacriticals and case
    tell application "Microsoft Word"
    tell text object of active document
    set its font object's color index to black
    repeat with i from 1 to (count its characters)
    if (get its character i's content) is in voyelles then
    set its character i's font object's color index to red
    end if
    end repeat
    end tell
    end tell
    end ignoring

    cf.

    Word2004AppleScriptRef.pdf

    http://www.microsoft.com/mac/developers/default.mspx

    Cheers,

    H

  10. 0

    Noaks

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    From your response to Camelot it would seem you have the issue pretty much solved. But you should realize:

    • You are only going to be able to write/run AppleScripts on a MAC computer

    • While there are Microsoft Word programs for MAC, there's no TextEdit on pc's as far as I know. But you might try e-mailing your TextEdit result (as an attachment) to a pc. It will probably open as a Word Document and, who knows, it might even have the red text you created on your MAC. Don't know if that would make your effort any easier.

    • If you have MS Word on your MAC, I'm reasonably certain Camelot's code can be adapted to that, but it's
    not simply a matter of substituting "MS Word" for "TextEdit". If the e-mail trick works, I don't know how rewriting the script for Word would further simplify things.

    I see that Hiroto has supplied possible code for working directly in Word. Message was edited by: Noaks

  11. 0

    Matthew Jackson

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hi, and many thanks for your help Hiroto. Your script worked in Word. I only changed the voyelles to vowels and deleted the y. This is exactly what I was looking for. My sincere thanks to everyone who helped me on this. I am so thankful for the Apple community. You guys rock. Happy New Year.