macosms-wordapplescriptfieldcodes

Get text of selected field code in Microsoft Word using AppleScript


I'm making a Automator to jump from citation in Word to the reference software(Zotero). But I can't find a AppleScript to extract text of selected field code (the first step).

The field code in Word is

ADDIN ZOTERO_ITEM CSL_CITATION {"citationID":"AFUiwuqi","properties":{"formattedCitation":"[1]","plainCitation":"[1]","noteIndex":0},"citationItems":[{"id":9752,"uris":["http://zotero.org/users/6410528/items/YYTRWPHH"],"itemData":{"id":9752,"type":"article-journal","container-title":"Nature","DOI":"10.1038/s41586-019-1737-7","ISSN":"0028-0836, 1476-4687","issue":"7782","page":"324-329","title":"Controlled flight of a microrobot powered by soft artificial muscles","volume":"575","author":[{"family":"Chen","given":"Yufeng"},{"family":"Zhao","given":"Huichan"},{"family":"Mao","given":"Jie"},{"family":"Chirarattananon","given":"Pakpong"},{"family":"Helbling","given":"E. Farrell"},{"family":"Hyun","given":"Nak-seung Patrick"},{"family":"Clarke","given":"David R."},{"family":"Wood","given":"Robert J."}],"issued":{"date-parts":[["2019",11,14]]}}}],"schema":"https://github.com/citation-style-language/schema/raw/master/csl-citation.json"} 

Here is the script process:

  1. Extract text from selected field code in Word (This is the question)
  2. Get the uris text(http://zotero.org/users/6410528/items/YYTRWPHH)
  3. Get the item-codes (YYTRWPHH).
  4. Open url (zotero://select/library/items?itemKey=YYTRWPHH)

Now I use VBA to extract field code text, see below. But in this way, the file will be changed. So I want to do this via AppleScript.

Sub GetFiledsCodes()
    Dim myRange As Range, myCodes As String
    Set myRange = Selection.Range
    With myRange
        If .Fields.Count = 0 Then
            MsgBox "No Code!", vbInformation
            Exit Sub
        Else
            .Fields.Update
            .TextRetrievalMode.IncludeFieldCodes = True
            .TextRetrievalMode.IncludeHiddenText = True
            myCodes = .Text
            myCodes = VBA.Replace(myCodes, Chr(19), "{")
            myCodes = VBA.Replace(myCodes, Chr(21), "}")
            .SetRange .End, .End
            .InsertAfter myCodes
            .Font.Name = "Times New Roman"
            .Font.Size = 12
            .Cut
        End If
    End With
End Sub

PS:

Here is my process in Automator(it can work but using VBA):

  1. Run AppleScript
    on run {input, parameters}
        tell application "Microsoft Word" to activate
        tell application "Microsoft Word"
            run VB macro macro name "GetFiledsCodes"
            delay 0.5
        end tell
        return input
    end run
    
  2. Get contents from clipboard
  3. Extract URLs from Text
  4. Filter Paragraphs begin with http://zotero.org/users/
  5. Copy to Clipboard
  6. Run AppleScript
    set myStr to do shell script "pbpaste"
    tell application "Zotero" to activate
    set AppleScript's text item delimiters to "
    "
    set myList to every text item of myStr
    set zoterocode to ""
    set codes to ""
    repeat with j from 1 to the length of myList
        set itemValue to item j of myList
        set zoterocode to (do shell script "sed -E 's#http://zotero.org/users/[0-9]+/items/##g' <<< " & itemValue)
        if j = 1 then
            set codes to zoterocode
        else
            set codes to codes & "," & zoterocode
        end if
    end repeat
    tell application "System Events"
        key code 18 using {command down, control down, option down}
        delay 0.5
        set collectionKey to do shell script "pbpaste"
        if collectionKey = myStr then
            set theurl to "zotero://select/library/items?itemKey=" & codes
        else
            set theurl to collectionKey & "/items?itemKey=" & codes
        end if
        open location theurl
    end tell
    

Solution

  • Thanks to ideas from @Mockman.

    Combining with the selection, here is the way to extract text from selected field code via AppleScript:

    tell application "Microsoft Word"
        tell selection
            set fcs to content of field code of field of text object
        end tell
    end tell
    fcs