我正在写一些文字到word文件我想改变那个文本的颜色,任何人都可以帮助那个plz。
我想从以下脚本中以红色打印“消息”。
以下是脚本:
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
一些细节: 该文件是在名称为“10.012.2014_17_4_20.doc”(.doc文件的名称未修复)的上述位置上已准备就绪的文件
答案 0 :(得分:1)
你所尝试的是错误的做法。
我建议你更多地阅读AppleScript的工作原理(或者一般的脚本),但是为了让你开始,这里是我刚刚在页面中编写的脚本,用于设置打开文档中特定单词的颜色把文字放在那里后:
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
如果您有Pages,请从空白页面开始并运行此脚本来尝试此操作。显然,你可以摆脱第二行中的“第3个字”,整个文本将是红色的。
我希望这是有道理的并且有所帮助。
<强> [编辑] 强>
我应该提一下,即使是TextEdit也是可编写脚本的,可以打开Word文档。以下是使用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
非Word应用程序丢失Word文件格式有一点危险。但它似乎只是在尝试一些非常简单的事情,而且我不确定这里是否真的需要Word。
答案 1 :(得分:0)
使用写入eof无法添加颜色。您应该在Word中打开文档,然后插入该行并添加颜色。这是一个应该演示如何的脚本:
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