EndNote CWYW for Word
Create a native EndNote library, convert static citations into dynamic CWYW fields, and verify that Word can reformat them through EndNote styles.
Non-negotiable rules
- Use
officeclifor every DOCX read, edit, render, and validation operation. - Do not use Word COM automation,
python-docx, LibreOffice, or direct ZIP/OOXML writes. - Use EndNote itself to create/import/save the
.enllibrary. An EndNote library consists of bothname.enlandname.Data/. - Back up the DOCX before mutation. Put working files in the workspace
Codex_temp/directory. - Treat reference numbers as library record numbers, not merely displayed bibliography numbers.
- Abort on ambiguous paragraph matches, missing records, malformed fields, or a non-empty target that already contains EndNote fields.
Workflow
1. Inventory and back up
- Confirm the target DOCX, desired
.enlpath, reference source, citation markers, and bibliography boundaries. - Run:
officecli view manuscript.docx text
officecli view manuscript.docx issues
- Identify each citation paragraph by
paraIdfrom the text view or by a unique text fragment. - Keep the source manuscript unchanged until the citation map and reference-record mapping are complete.
2. Create the native EndNote library
- Open EndNote and create a new library at the requested
.enlpath. - Import the RIS or EndNote XML source. For RIS, select
Reference Manager (RIS)as the import option. - Prefer importing into an empty library in final reference order. Confirm the imported count and save.
- Export or retain an EndNote XML file whose
<rec-number>values match the native library record numbers. - Never move or deliver the
.enlfile without its same-named.Datafolder.
Use EndNote UI control only for the library operations. Use the bundled scripts for database-ID extraction and DOCX field insertion.
3. Prepare the citation map
Read citation-map.md and create a JSON file in Codex_temp/. Each item must identify one visible marker and the corresponding EndNote record number(s). Use paragraphId when available; otherwise use a unique paragraphContains value.
The bibliography configuration must identify the exact heading, the next section heading, and the pattern matching the existing static reference paragraphs.
4. Insert dynamic fields atomically
Run:
$skill = Join-Path $env:USERPROFILE '.codex\skills\endnote-cwyw-docx'
& "$skill\scripts\insert_endnote_fields.ps1" `
-DocxPath '.\manuscript.docx' `
-EndNoteXmlPath '.\Codex_temp\references.xml' `
-CitationMapPath '.\Codex_temp\citation-map.json' `
-EndNoteLibraryPath '.\Codex_temp\Ref\library.enl'
The script:
- reads the DOCX through
officecli raw; - extracts the library database ID from
library.Data\sdb\sdb.eni; - embeds full Traveling Library records in
ADDIN EN.CITEinstructions; - wraps the cached reference list in one
ADDIN EN.REFLISTfield; - applies all mutations through one atomic
officecli batch; - preserves the visible citation and bibliography text;
- rolls back automatically if any batch operation fails.
Pass -DatabaseId only when automatic extraction is impossible. Pass -BackupPath to control the backup destination.
5. Validate before handoff
Run:
& "$skill\scripts\validate_endnote_fields.ps1" `
-DocxPath '.\manuscript.docx' `
-ExpectedCitationFields 14 `
-ExpectedUniqueRecords 15
officecli view '.\manuscript.docx' issues
officecli view '.\manuscript.docx' html --out '.\Codex_temp\endnote-preview.html'
Require all of the following:
- expected
ADDIN EN.CITEcount; - exactly one
ADDIN EN.REFLIST; - balanced field begin/separate/end counts;
- all expected record numbers present in Traveling Library XML;
- one consistent EndNote
db-id; - no temporary
{Author, Year #Record}citations; - no generated
dNpN:spaceXML namespace prefixes; - zero
officeclistructural issues; - unchanged visible citation markers and bibliography entries.
Finally, instruct the user to open the library, select another style in Word's EndNote tab, and choose Update Citations and Bibliography. This is the user-visible confirmation that the fields are editable CWYW objects.
Failure handling
- If
sdb.eniis locked, use the bundled extractor; it opens the file with read/write sharing. - If database-ID extraction returns zero or multiple candidates, stop and inspect the library rather than guessing.
- If a marker is split across multiple Word runs, normalize that marker with
officeclior choose a narrower paragraph edit before retrying. - If the DOCX already contains
EN.CITEorEN.REFLIST, validate or update it; do not wrap it a second time. - If EndNote record numbers differ from bibliography numbering, rebuild the citation map from the actual library records.
- If Word displays cached text correctly but EndNote cannot update it, check the
db-id,<RecNum>, Traveling Library record payload, and.Datafolder first.