Brulog

Words of occasional wisdom from Bruce Oakley

Search-Replace

Links: Tips-Traps | Change Case | Parse Data | Search-Replace

Purpose: Performs multiple search and replace from a list of standard search-replace pairs (useful for style enforcement or translation from outside sources).
Operates on: Full text of the frontmost window of MediaSpan Media Software’s NewsEdit application.
Suggested uses: Any material that takes repetitive cleanup, such as horoscopes, bridge columns, sports statistics, court filings, bankruptcy listings. Expansion or abbreviation in addresses: Road to Rd., Avenue to Ave. Style cleanup: AM or PM to a.m. or p.m. Removing zeros from times: “:00 p.m.” to “ p.m.”
How it works: Reads through a list of search-replace pairs and feeds them to a subroutine from the folks at Apple that takes advantage of the AppleScript text item delimiters.
Method: Build list of search-replace pairs. Load the text to be tested into a variable. Step through the list of pairs, at each iteration set the AppleScript text item delimiters to the string to be replaced. Generate a list of the text items, which will now be text items on either side of the search string. Change the delimiters to the replacement string and write the text items back into the original string. This substitutes the replacement string at the boundary positions occupied by the search string. Restore the text item delimiters to the default.
Example:
Search-replace pairs: {Smith, Jones},{is,was}
Text to test: John Smith is the oldest Smith there is.
The original text item list is the list of the individual characters: {"J","o","h",...}
Switching the delimiters to “Smith” makes the text item list {"John "," is the oldest "," there is."}
Changing the delimiters to “Jones” and writing the list back to the original string as text produces “John Jones is the oldest Jones there is.
Switching the delimiters to “is” makes the text item list {"John Jones "," the oldest Jones there ","."}
Changing the delimiters to “was” and writing the list back to the original string as text produces “John Jones was the oldest Jones there was.
Potential enhancements:

  • Operate on a selection instead of the entire text.
  • Change the list of search-replace pairs as needed.
  • Invoke the “swap_string” routine independent of the pairs list for specialty search and replace after applying other tests, such as whether a particular stylesheet is in effect or whether the previous word or paragraph contains some test string.

Caution: The code is presented so that it can be copied and pasted into a Script Editor window, but as with any copy and paste, you should check for unintended line breaks or mistranslated special characters. Some script lines may be longer than the browser window; stretch the display to be sure.
Inline comments are indicated by two hyphens:
-- This is an inline comment
Block comments are wrapped in parentheses and asterisks:
(* This is a block comment.
*)

The code:

--Declare variable scope
global theText
global strings_subs, a_sub, this_sub, this_string

(*
About pairs of strings and substitutions in strings_subs:
  •First pair is blank placeholder -- not sure why, but script ignores first pair
  •Space-question-space to dashes
  •Space-caret-space to dashes
  •mixed quotes to open quotes
  •space-close quote to close quote
  •subscript quote to open quote
  •supershift comma (ASC226) to apostrophe
  •supershift % (ASC228) to close quote
  •question-s to apostrophe-s
  •space-question to space-open quote
  •tab (ASC9) to space
  •ellipsis (...) to colon-space
  •AM, PM, A.M., P.M.  to a.m., p.m.
  •double spaces with single
  •return-space with return
*)
set strings_subs to {{"", ""}, ¬
  {" ? ", " -- "}, ¬
  {" ˆ ", " -- "}, ¬
  {"‘’", """}, ¬
  {" ’’", "”"}, ¬
  {"„", "“"}, ¬
  {ASCII character 226, "’"}, ¬
  {"‰", "”"}, ¬
  {"?s", "’s"}, ¬
  {" ?", " “"}, ¬
  {ASCII character 9, " "}, ¬
  {"...", ": "}, ¬
  {" AM ", " a.m. "}, ¬
  {" PM ", " p.m. "}, ¬
  {"A.M.", "a.m."}, ¬
  {"P.M.", "p.m."}, ¬
  {"  ", " "}, ¬
  {return & " ", return}}

--Take control of the application
tell application "NewsEditPro IQue"
  --A "try ... on error ... end try" loop captures errors
  try
  --Take control of the story in the frontmost window
  --Different applications will vary in the commands
  --for referring to window, story, text (word, paragraph, etc.)
  activate
  --capture text of story in front window
  set theText to text of story 1
  --step through search-replace pairs in strings_subs
  repeat with a_sub in strings_subs
    set this_string to item 1 of a_sub
    set this_sub to item 2 of a_sub
    --invoke search-replace routine
    set theText to my swap_string(theText, this_string, this_sub)
  end repeat
  --write modified text back to story in front window
  set text of story 1 to theText
  on error
    display dialog "You must have a file open and text selected!" & return with icon 2
  end try
end tell

display dialog "Done" buttons {"•"} giving up after 1 default button 1

-- Cleanup routine to make substitutions
on swap_string(theText, SearchString, ReplaceString)
  set OldDelims to AppleScript's text item delimiters
  set AppleScript's text item delimiters to SearchString
  set newText to text items of theText
  set AppleScript's text item delimiters to ReplaceString
  set theText to newText as text
  set AppleScript's text item delimiters to OldDelims
  return theText
end swap_string

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: