在Applescript中,在TextEdit中只找到粗体字会导致应用程序挂起



只是想找到一种普通的appescapet方法,将变量设置为文档中所有粗体字。我一直在寻找在Word,Pages&TextEdit和TextEdit似乎是唯一的一个(可能是错误的)。

好消息是下面的脚本有效,但坏消息是,如果文档超过2页,比如说有大约50个粗体字,TextEdit就会挂起。

有其他方法可以使用Applescript获得粗体字吗?

tell application "TextEdit"
    return words of text of document 1 where font contains "Bold"
end tell

感谢

可能是TextEdit的速度太慢了。我刚刚用下面的代码编写了一份3600字的文档,其中穿插了一些随机的粗体字。花了太长时间。我会考虑使用不同的可编写脚本的应用程序,比如Tex Edit Plus(仍然很强大,仍然很棒)。我同时使用"黑色"one_answers"粗体"的原因是,当你将文本设置为粗体时(至少在TextEdit中),有些字体使用"黑色的"变体而不是"粗体"。需要明确的是,下面的代码工作,但速度非常慢。如果等待足够长的时间,可能您的代码也能工作。:-(但继续阅读:-)

tell application "TextEdit"
    tell document 1
        set thisManyRuns to count of attribute run of text of it
        repeat with r from 1 to thisManyRuns
            if font of attribute run r of text of it contains "Black" then
                --do stuff
                set color of attribute run r of text of it to {35555, 0, 0}
            end if
            if font of attribute run r of text of it contains "Bold" then
                --do stuff
                set color of attribute run r of text of it to {35555, 0, 0}
            end if
        end repeat
    end tell
end tell

我刚刚在Tex Edit Plus中运行了这篇文章,大约两秒钟就完成了:

tell application "Tex-Edit Plus"
    tell document 1
        set thisManyRuns to count of style run of text of it
        repeat with r from 1 to thisManyRuns
            set thisStyle to style of style run r of text of it
            if on styles of thisStyle contains bold then
                --do stuff
                set color of style run r of text of it to {0, 35555, 0}
            end if
        end repeat
    end tell
end tell

所以你可能想换成那样。只是需要注意的是,在查询on styles属性之前,我必须style放入thisStyle变量中——如果我尝试在一行中这样做,它将不起作用。我正在处理一个rtf文件。

最新更新