如何将此代码从 VBA 转换为 AppleScript?

How do I convert this code from VBA to AppleScript?

我正在尝试将此代码从 VBA 转换为 AppleScript 2.5。

Sub testMacro()
    Application.ScreenUpdating = False
    Dim InDoc As Document, OutDoc As Document, i As Long
    Dim RngOut As Range, RngNm As Range, StrNm As String
    Set InDoc = ActiveDocument
    With InDoc
        Set RngOut = ActiveDocument.Range(0, 0)
        Dim obj As New DataObject
        For i = 1 To 3
            Set RngOut = RngOut.GoTo(What:=wdGoToPage, Name:=i)
            Set RngOut = RngOut.GoTo(What:=wdGoToBookmark, Name:="\page")
            RngOut.Copy
            Set OutDoc = Documents.Add
            With OutDoc
                .Range.Paste
                .Characters.Last.Delete
                Set RngNm = .Paragraphs.First.Range
                RngNm.End = RngNm.End - 1
                StrNm = RngNm.Text
                .Paragraphs.First.Range.Delete
                obj.SetText StrNm
                obj.PutInClipboard
                .Close
            End With
        Next i
    End With
    Set RngOut = Nothing: Set RngNm = Nothing
    Set InDoc = Nothing: Set OutDoc = Nothing
    Application.ScreenUpdating = True
End Sub

有人告诉我只能执行以下操作,但它不起作用:

tell application "Microsoft Word"
    activate
    do Visual Basic "GetPage
        End Sub

        Sub GetPage()
            Application.ScreenUpdating = False
            Dim InDoc As Document, OutDoc As Document, i As Long
            Dim RngOut As Range, RngNm As Range, StrNm As String
            Set InDoc = ActiveDocument
            With InDoc
                Set RngOut = ActiveDocument.Range(0, 0)
                Dim obj As New DataObject
                For i = 1 To 3
                    Set RngOut = RngOut.GoTo(What:=wdGoToPage, Name:=i)
                    Set RngOut = RngOut.GoTo(What:=wdGoToBookmark, Name:=\"\page\")
                    RngOut.Copy
                    Set OutDoc = Documents.Add
                    With OutDoc
                        .Range.Paste
                        .Characters.Last.Delete
                        Set RngNm = .Paragraphs.First.Range
                        RngNm.End = RngNm.End - 1
                        StrNm = RngNm.Text
                        .Paragraphs.First.Range.Delete
                        obj.SetText StrNm
                        obj.PutInClipboard
                        .Close
                    End With
                Next i
            End With
            Set RngOut = Nothing: Set RngNm = Nothing
            Set InDoc = Nothing: Set OutDoc = Nothing
            Application.ScreenUpdating = True"
end tell

我不断收到此错误:预期行尾等但找到标识符。 并突出显示以下内容(我加粗):do 视觉基本"GetPage

基本上,这段代码应该获取文档每一页的第一个词,并将循环到的最后一页上的第一个词插入剪贴板。

do Visual Basic 不是 Microsoft Word 的 AppleScript 词典中的 AppleScript 命令,因此无法使用。

(不仅如此,您示例中的 VBA 代码也是无效的。)

run VB macro Word 的 AppleScript 命令,因此如果您有一个名为 GetPage 的 VBA 宏,请在 Normal.dot 项目,你可以使用这个 AppleScript 代码来 运行 它:

tell application "Microsoft Word"
    run VB macro macro name "GetPage"
end tell

(注意:我假设您的 VBA 代码可以在 Mac 版本的 Word 中运行。我还没有尝试过并修复它,如果它没有,那就不同了重要。)

这是我迅速创建的一个 AppleScript,它应该可以满足您的要求。它抓取每一页的第一个词并将它们复制到剪贴板。请注意,这已经进行了最低限度的测试,但它确实适用于我手头的 79 页 Word 文档。

use AppleScript version "2.5"
use scripting additions
use framework "Foundation"

to trimText:inText

    set _str to current application's NSString's stringWithString:inText
    set _whitespace to current application's NSCharacterSet's whitespaceAndNewlineCharacterSet()
    set _str to _str's stringByTrimmingCharactersInSet:_whitespace
    return _str as text

end trimText:

on run

    set wordList to {}
    set lastPage to false

    tell application "Microsoft Word"

        tell active document
            set firstWord to word 1

            repeat until lastPage
                set pageWord to my trimText:(content of firstWord)
                if pageWord ≠ "" then set end of wordList to pageWord
                set nextPage to go to next firstWord what goto a page item
                if (start of content of nextPage) is not equal to (start of content of firstWord) then
                    set firstWord to word 1 of nextPage
                else
                    set lastPage to true
                end if
            end repeat
        end tell

    end tell

    if (count of wordList) is greater than 0 then
        set {oldDelims, AppleScript's text item delimiters} to {AppleScript's text item delimiters, linefeed}
        set the clipboard to (wordList as text)
        set AppleScript's text item delimiters to oldDelims
    end if

end run