Applescript和Quarkxpress Passport v 6.5

时间:2014-12-11 18:57:51

标签: textbox applescript stylesheet paragraphs

我有一个脚本,通过几个段落/行,并相应地应用段落和字符样式表。虽然脚本运行完美(我希望它可以帮助那里的人) - 这个过程需要很长时间 - 我可能已经走了很久......我希望有人可以帮我减少处理时间。下面是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

我的Applescript:

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

1 个答案:

答案 0 :(得分: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个AppleEvent,7个在循环期间定期执行,1个在最后一行执行(“查看”)。对于80行文本,5 + 80 * 6 + 1 = 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)在循环外应用默认值。

您将null 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个AppleEvent用于以下检查:

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。在纯苹果脚本中,如果周期内没有空格,则显然将其视为单词的一部分。

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