简体   繁体   中英

Change the color of text by using applescript

I am writing some text in to word file i want to change the color of that text any one can help on that one plz.

I want to print the 'message' from following script in red color.

Here is the Script:

set message to "mostly these windows are popup in application"

on ResultCreationFuction(message)

try
    set text_to_save to message as text
    tell application "System Events"
        tell application "Finder"
            set sortedList to sort (get files of folder "SofTestAutomationResult" of desktop) by  modification date
            set FileCount to get count of sortedList
            set theFile to (item FileCount of sortedList) as alias
        end tell

        set file_ref to open for access theFile with write permission           
        write (text_to_save & return) to the file_ref starting at eof
        close access file_ref
        delay 2
        end tell
    end try
end ResultCreationFuction

Some Details: The file is word which is all ready present on above location having name "10.012.2014_17_4_20.doc" (the name of .doc file is not fix)

What you are attempting is the wrong way to do it.

  1. To manipulate content like that, including formatted text (not plain text), you need to work within, ideally, a well-scriptable app, like Pages (or Word, perhaps, but I don't have that on the machine I'm writing this from).
  2. Don't use System Events if you don't need to. Use the apps with the appropriate AppleEvents/dictionary, etc. If you don't know what I'm talking about, you need to take advantage of the infinite resource known as the web.
  3. "Fuction" is just bad form.

I would suggest doing a lot more reading up on how AppleScript works (or scripting in general), but to start you out, here is a script I just wrote in pages which sets the color of a specific word of the open document after putting text in there:

tell application "Pages"
    set body text of document 1 to "hello there mister fancy pants"
    set color of word 3 of body text of page 1 of document 1 to {64614, 0, 111}
end tell

If you have Pages, try this by starting with a blank page and running this script. Obviously, you could get rid of "word 3 of" in the 2nd line, and the whole body text will be red.

I hope this makes sense and is of help.

[edit]

I should mention that even TextEdit is scriptable and can open Word documents. Here's an example using TextEdit:

tell application "TextEdit"
    set text of document 1 to "hello mister fancy pants"
    set color of words 2 thru 3 of text of document 1 to {65535, 0, 0}
end tell

There is a little danger of non-Word apps losing formatting of Word files. But it just seems you are attempting something very simple, and I'm not sure if Word is really necessary here.

You can't add color using the write to eof. You should open the document in Word and then insert the line and add the color. Here's a script that should demonstrate how:

set text_to_add to "mostly these windows are popup in application"
set theFile to ((path to desktop folder) & "10.012.2014_17_4_20.doc") as string

tell application "Microsoft Word"
    set theFile to theFile as string -- assuming theFile is an alias or :: path
    open file theFile

    tell active document
        set endOfDoc to end of content of text object  -- insert the text to end of document
        set theRange to create range start (endOfDoc - 1) end endOfDoc
        insert text text_to_add at theRange

        set myRange to create range start endOfDoc end (endOfDoc + (length of text_to_add))
        set color index of font object of myRange to red

        save
    end tell
end tell

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM