基于Excel映射的AppleScript实现Word/Excel查找替换及问题解决
1. Enable Wildcard Search
To avoid listing every tense, plural, or variation of your terms, turn on wildcard matching in the Word find/replace setup. Tweak the properties of myfind line to set match wildcards:true, then format your Column A entries with wildcards (e.g., employe* will match "employee", "employees", "employing").
Updated Word find properties:
set properties of myfind to {match case:false, match whole word:false, match wildcards:true}
Note: Toggle match whole word if you want to avoid partial matches (like "employ" showing up in "employment").
2. Recursively Remove Consecutive Double Spaces
Add a loop that keeps replacing double spaces with single ones until none are left.
For Word, insert this after your main replacement loop:
set doubleSpaceFound to true repeat while doubleSpaceFound execute find myfind find text " " replace with " " replace replace all set doubleSpaceFound to found of myfind end repeat
For Excel, use this logic:
set doubleSpaceFound to true repeat while doubleSpaceFound set findResult to find (used range of active sheet) what " " look at xl part look in xl values match case false match byte false if findResult is not missing value then replace findResult what " " replacement " " look at xl part look in xl values match case false match byte false replace all else set doubleSpaceFound to false end if end repeat
3. Fix Excel Syntax Error
That error pops up because Excel’s AppleScript API uses completely different terminology than Word. Here’s the corrected Excel script section:
tell application "Microsoft Excel" activate open theXLSX -- Excel uses "open" instead of "open file" set targetSheet to active sheet of active workbook -- Use "active workbook" not "document 1" set arange to value of used range of targetSheet -- Run term-to-abbreviation replacements repeat with arow in arange set findText to item 1 of arow set replaceText to item 2 of arow if findText is not missing value and replaceText is not missing value then find (used range of targetSheet) what findText look at xl part look in xl values match case false match byte false replace (result) what findText replacement replaceText look at xl part look in xl values match case false match byte false replace all end if end repeat -- Add the double-space removal loop here if needed end tell
Key fixes:
- Ditch
open file—Excel just usesopen - Target
active workbookinstead ofdocument 1(Excel works with workbooks, not documents) - Use Excel’s specific
find/replaceparameters likexl partandxl values
Complete Combined Script (Word + Excel)
Here’s a full script that lets you load your master term-abbreviation Excel, then choose either a Word or Excel file to apply replacements:
use scripting additions -- Pick master Excel with term-abbreviation pairs set masterXLSX to (choose file of type {"org.openxmlformats.spreadsheetml.sheet"} with prompt "Select Master Term-Abbreviation Excel File") -- Load term pairs from master Excel tell application "Microsoft Excel" activate open masterXLSX set masterSheet to active sheet of active workbook set termPairs to value of used range of masterSheet close active workbook saving no -- Close master file after loading data end tell -- Pick target file (Word or Excel) set targetFile to (choose file of type {"org.openxmlformats.wordprocessingml.document", "org.openxmlformats.spreadsheetml.sheet"} with prompt "Select Target File to Replace Terms") set targetFileType to type identifier of targetFile -- Process Word file if targetFileType is "org.openxmlformats.wordprocessingml.document" then tell application "Microsoft Word" activate open targetFile set docFind to find object of text object of active document set properties of docFind to {match case:false, match whole word:false, match wildcards:true} -- Replace terms with abbreviations repeat with pair in termPairs set term to item 1 of pair set abbr to item 2 of pair if term is not missing value and abbr is not missing value then execute find docFind find text term replace with abbr replace replace all end if end repeat -- Remove double spaces set doubleSpaceFound to true repeat while doubleSpaceFound execute find docFind find text " " replace with " " replace replace all set doubleSpaceFound to found of docFind end repeat end tell end if -- Process Excel file if targetFileType is "org.openxmlformats.spreadsheetml.sheet" then tell application "Microsoft Excel" activate open targetFile set targetSheet to active sheet of active workbook -- Replace terms with abbreviations repeat with pair in termPairs set term to item 1 of pair set abbr to item 2 of pair if term is not missing value and abbr is not missing value then find (used range of targetSheet) what term look at xl part look in xl values match case false match byte false replace (result) what term replacement abbr look at xl part look in xl values match case false match byte false replace all end if end repeat -- Remove double spaces set doubleSpaceFound to true repeat while doubleSpaceFound set findResult to find (used range of targetSheet) what " " look at xl part look in xl values match case false match byte false if findResult is not missing value then replace findResult what " " replacement " " look at xl part look in xl values match case false match byte false replace all else set doubleSpaceFound to false end if end repeat end tell end if
内容的提问来源于stack exchange,提问作者Han Ding

