0

我有一个脚本,它通过几个段落/行并相应地应用段落和字符样式表。虽然脚本运行完美(我希望它可以帮助那里的人) - 这个过程需要太长时间 - 我可能已经走了很长一段路......我希望有人可以帮助我减少处理时间。下面是 Applescript:以及我的几行文字(原文包含 80 行)。

Quarxpress 文本:

R45m    HOUSE. Landmark property with panoramic views, 2000m2 on over 8 200m2.  web ref: 3036011  
R35m    HOUSE. Contemporary 4 bedroomed home on view slopes of Mount Street web ref: 3137609  
R27.5m  HOUSE. Ambassadorial 6 bedroomed home on over 5000m2    web ref: 3137592
R19.95m HOUSE. Brand new Georgian 5 bedroomed home on an acre   web ref: 3057625
R19m    ESTATE. Classical home of 1200m2 on 5000m2 of landscaped gardens    web ref: 234336
R17.5m  BOUTIQUE HOTEL. 11 Luxurious suites within a majestic setting on over 5000m2    web ref:3147123
R16.95m HOUSE. Classical 5 bedroomed home on 4127m2 in Hamilton Enclosure   web ref: 3230085
R15.5m  HOUSE. Georgian residence of 1100m2 close to the Country Club   web ref: 737272
R13.5m  HOUSE. A master-built contemporary home, amazing easterly views web ref: 3096574
R9.95m  HOUSE. Renovated family home with pool and court on an acre in Bryanston West   web ref: 3193537
R8.95m  ESTATE. 43 on Eccleston. Packages in R8m’s. Contemporary home with views    web ref: 3225098
R8.25m  HOUSE. Renovated 4 bedroom home in top ave in Bryanston East on 3255m2  web ref: 3248317
R6.95m  HOUSE. Nico van der Meulen contemporary masterpiece requiring final finishing   web ref: 3212495
R5.95m  HOUSE. Renovated 3 bedroom home in boomed enclosure on 4660m2.  web ref: 3247597
R5.5m   HOUSE. Prime position in boomed enclosure in Bryanston East, excellent potential    web ref: 3201665
To view any of the above call Regan: 088 888 8888 or Erica: 088 888 8888

我的苹果脚本:

tell application "QuarkXPress Passport"
    tell document 1
        set MyStyle3 to object reference of character spec "RedCopy"
        set MyStyle1 to object reference of style spec "MainCopy"
        set MyStyle4 to object reference of character spec "BoldCopy"

        tell story 1 of current box
            set style sheet of every paragraph to null
            delay 5
            set ParagraphCounter to (get count of paragraphs)
            repeat with n from 1 to ParagraphCounter
                tell paragraph n
                    try
                        set style sheet to MyStyle1
                    end try
                    try
                        if words 1 thru 2 contains "." then
                            set character style of words 1 thru 3 to MyStyle4
                        else
                            set character style of words 1 thru 2 to MyStyle4
                        end if
                    end try
                    try
                        set character style of word 2 to MyStyle4
                    end try

                    try
                        set character style of words -3 thru -1 to MyStyle4
                    end try

                    try
                        if words 1 thru 2 starts with "To view" then
                            set character style of words 1 thru -1 to MyStyle3
                        end if
                    end try
                end tell
            end repeat



        end tell
    end tell
end tell
beep 3
4

1 回答 1

0

为什么 Applescript 很慢?

提高 Applescript 性能的最大技巧是尽可能多地删除 AppleEvents。您直接向应用程序发出的每个命令都作为 AppleEvent 执行。例如,以下代码执行两个 AppleEvent。一个获取段落中的所有单词,一个设置段落的字符样式:

tell story 1 of current box
   tell paragraph n
        if words 1 thru 2 contains "." then
            set character style of words 1 thru 3 to MyStyle4
        else
            set character style of words 1 thru 2 to MyStyle4
        end if
    end tell
end tell

分析

因此,查看您的脚本,您有 5 个 AppleEvents 在设置中,7 个在循环期间定期执行,1 个在最后一行执行(“查看”)。5 + 80 * 6 + 1 = 80 行文本的486 个 AppleEvents

通过将其中一些事件放入设置中并直接使用本机 Applescript 类型,您可以显着降低该数字。

修复

1)删除不必要的代码

您可以完全删除以下行:

set character style of word 2 to MyStyle4

MyStyle4当您检查一段时间时,您已经在申请第 2 个单词。

--word 2 is covered under both "words 1 thru 2" and "words 1 thru 3".
if words 1 thru 2 contains "." then
    set character style of words 1 thru 3 to MyStyle4
else
    set character style of words 1 thru 2 to MyStyle4
end if

2) 在循环之外应用默认值。

您将 anull style sheet应用于所有段落,然后一个接一个地重新应用于MyStyle1每个段落。所以删除这些行

set style sheet of every paragraph to null
[...]
set style sheet to MyStyle1

并在循环之前将其替换为

set style sheet of every paragraph to MyStyle1

您可以将相同的内容应用于加粗最后三个单词:

-- this is being run during every iteration of the loop
set character style of words -3 thru -1 to MyStyle4

在循环之前应用它,它只花费你一个 AppleEvent

set character style of words -3 thru -1 of every paragraph to MyStyle4

3) 不要在不需要的时候使用 AppleEvents

如前所述,每次您从 Quark 请求信息时,都会花费您一个 AppleEvent。在您的 80 行文本中,您发送 160 个 AppleEvents 以进行以下检查:

tell paragraph n
    if words 1 thru 2 contains "." then [...]
    if words 1 thru 2 starts with "To view" then [...]
end tell

要解决此问题,您可以预先获取所有文本,这将使段落成为 Applescript 原生类型,然后对其进行条件检查。所以,在循环之前:

tell story 1 of current box
    set quarkText to every paragraph
end tell

这将为您提供可以处理的字符串列表,并在不需要将 AppleEvents 发送到 Quark 的情况下运行条件。

把它们放在一起

那么,这样的脚本会是什么样子呢?

tell application "QuarkXPress Passport"
    tell document 1
        set MyStyle1 to object reference of style spec "MainCopy"
        set MyStyle3 to object reference of character spec "RedCopy"
        set MyStyle4 to object reference of character spec "BoldCopy"

        tell story 1 of current box
            -- Anything that is run during every iteration should be moved outside
            -- the loop, and targeted at every paragraph.
            set style sheet of every paragraph to MyStyle1
            set character style of words -3 thru -1 of every paragraph to MyStyle4

            delay 5

            -- Transform data once, instead of every loop iteration
            set quarkText to every paragraph
            set ParagraphCounter to (get count of quarkText)

            repeat with n from 1 to ParagraphCounter
                -- currentLine is now a string that is not dependent on Quark, which
                -- means you have cut way down on AppleEvents
                set currentLine to item n of quarkText
                tell paragraph n
                    if word 1 of currentLine contains "." then
                        set character style of words 1 thru 3 to MyStyle4 -- AppleEvent
                    else
                        set character style of words 1 thru 2 to MyStyle4 -- AppleEvent
                    end if

                    if currentLine starts with "To view" then
                        set character style of words 1 thru -1 to MyStyle3 -- AppleEvent
                    end if
                end tell
            end repeat
        end tell
    end tell
end tell
beep 3

请注意,我更改了if statement一段时间的外观。在纯 Applescript 中,如果句号周围没有空格,则显然将其视为单词的一部分。

word 1 of "R27.5m   HOUSE." --> "R27.5m"

现在,设置中有 6 个 AppleEvents,只有一个在循环期间定期执行,一个用于最后一行,使计数减少到 6 + 80 * 1 + 1 = 87 AppleEvents

警告

我的机器上没有安装 Quark,但是看你写的代码,关键字word可能和 Quark 字典有冲突。在这种情况下,您可能需要将所有字符串检查移到 Quark tell 块之外。我的第一个想法是,您可以创建一个实际应用样式的子例程,并在必要时调用该子例程:

on apply_style(characterStyle, paragraphNumber, startWord, endWord)
    tell application "QuarkXPress Passport"
        tell document 1
            tell story 1 of current box
                tell paragraph paragraphNumber
                    set character style of words startWord thru endWord to MyStyle4
                end tell
            end tell
        end tell
    end tell
end apply_style


tell application "QuarkXPress Passport"
    tell document 1
        set MyStyle1 to object reference of style spec "MainCopy"
        set MyStyle3 to object reference of character spec "RedCopy"
        set MyStyle4 to object reference of character spec "BoldCopy"

        tell story 1 of current box
            set style sheet of every paragraph to MyStyle1
            set character style of words -3 thru -1 of every paragraph to MyStyle4

            delay 5

            set quarkText to every paragraph
        end tell
    end tell
end tell

set ParagraphCounter to (get count of quarkText)
repeat with n from 1 to ParagraphCounter
    set currentLine to item n of quarkText
    if word 1 of currentLine contains "." then
        set myStyle to MyStyle4
        set startingAt to 1
        set endingAt to 3
    else if currentLine starts with "To view" then
        set myStyle to MyStyle3
        set startingAt to 1
        set endingAt to -1
    else
        set myStyle to MyStyle4
        set startingAt to 1
        set endingAt to 2
    end if

    my apply_style(myStyle, n, startingAt, endingAt)
end repeat
beep 3
于 2014-12-11T21:52:57.383 回答