I do a lot of papers on TextEdit for my Spanish class. I want to make a script that will go over the document and change places where I put down e’ to é or where I put n~ to ñ . It seems really simple, but I can’t think of how I’d do that. Help would be nice
tell application "TextEdit"
tell document 1
set a1 to text 1
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"n~"}
set a2 to text items of a1
set AppleScript's text item delimiters to oldDelims
set a3 to count of a2
set newText to ""
repeat with i from 1 to a3
set newText to (newText & item i of a2 & "ñ")
end repeat
end tell
end tell
on TranslateText(str, tokens1, tokens2)
--
-- tokens == string of character tokens, or
-- list of string tokens
--
-- The bulk of this handler is ensuring that translations
-- like a->b, b->c, c->a work properly. That is to say, we
-- can't simply replace all occurances of 'a' with 'b',
-- because we would then replace all occurances of 'b' with
-- 'c', meaning that all occurances of 'a' would also become
-- 'c', etc.
--
-- We could just parse character for character, but the
-- following use of 'sentinal' characters allows us to
-- keep the speed of the text item delimiters.
-- We make two important assumtions, one: that these two
-- characters do not exist in str or in either of the tokens
-- parameters, and two: that the numbers of items in tokens1
-- and tokens2 is *exactly* the same!
--
set kCounter to ASCII character 0 -- assumes these characters are not in parameters
set kDelimit to ASCII character 1
set tokens1 to every item of tokens1 -- coerces string to character list, if nessesary
set tokens2 to every item of tokens2
-- We will build up a temporary "replacement" character set, using
-- multiples of kCounter saved to countOf
--
set countOf to ""
set astids to AppleScript's text item delimiters -- save
try
repeat with i from 1 to tokens1's length
set AppleScript's text item delimiters to tokens1's item i
set str to str's text items
set countOf to countOf & kCounter -- build our "replacement" character set
set AppleScript's text item delimiters to kDelimit & countOf & kDelimit
set str to str as string
end repeat
repeat with i from tokens1's length to 1 by -1 -- GO BACKWARDS
set AppleScript's text item delimiters to kDelimit & countOf & kDelimit
set str to str's text items
set AppleScript's text item delimiters to tokens2's item i
set str to str as string
try
set countOf to countOf's text 1 thru -2 -- replace longest first
end try
end repeat
set AppleScript's text item delimiters to astids -- restore
return str
on error e number n from f to t partial result p
set AppleScript's text item delimiters to astids
error e number n from f to t partial result p
end try
end TranslateText
-- debugging:
-- TranslateText("abcabc", "abc", "cba") --> "cbacba"
-- Add to these as needed, making sure that they stay the
-- exact same length:
--
set findTokens to {"e'", "n~"}
set replaceTokens to "éñ" -- you can use a string for single-character tokens
TranslateText("e'l nin~o", findTokens, replaceTokens) --> "él niño"