try command+option+esc. You should be able to force quit such an app.
The “considering case” block ensures that applescript sees “a” as something different to “A”
So your lists should look something like this:
set theSearchList to {"a", "b", "A", "B"}
set theReplaceList to {"(1)", "(2)", "(27)", "(28)"}
according to your first post:
you are figuring out this step yourself and posting it here, let us see it
Here’s how I went about it:
To find and replace, I searched on this forum for something like “Find replace text” to find: http://bbs.applescript.net/viewtopic.php?id=18551
In that discussion, Bruce Philips shared his ‘clone’ of the PHP str_replace function.
Also further down the discussion explains the use of the “considering case” block.
set theSearchList to {"a", "b", "A", "B"}
set theReplaceList to {"(1)", "(2)", "(27)", "(28)"}
tell application "TextEdit"
-- open file
set theAlias to choose file
set theDoc to open theAlias
-- get text contents
set theText to text of theDoc
-- your conversion code here
-- use the case sensitive version of Bruce Philip's str_replace() which accepts lists as arguments
tell me to set newText to str_ireplace(theSearchList, theReplaceList, theText)
-- or: set newText to my str_ireplace(theSearchList, theReplaceList, theText)
-- replace text
try
set text of theDoc to newText
on error eMsg number eNum
display dialog "Error setting text (" & eNum & ")" buttons {"OK"} default button 1
end try
-- close and save file
try
--save theDoc in theAlias
close theDoc saving yes saving in theAlias
on error eMsg number eNum
display dialog "Error saving (" & eNum & ")" buttons {"OK"} default button 1
end try
-- reopen
open theAlias
end tell
(*
clone of PHP's str_ireplace function
original posted by Bruce Philips
http://bbs.applescript.net/profile.php?id=5342
on bbs.applescript.net
http://bbs.applescript.net/viewtopic.php?id=18551
note: the above mentioned version doesn't consider case, that's the only
modification by SwissalpS. The original would be str_replace.scpt
*)
on str_ireplace(search, replace, subject)
considering case
local search, replace, subject, ASTID, returnList, searchCount, thisSubject, i, n
set ASTID to AppleScript's text item delimiters
set returnList to true
-- This wouldn't make sense
if search's class is not list and replace's class is list then return subject
-- Make one-item lists if needed
if search's class is not list then set search to {search}
if subject's class is not list then set {subject, returnList} to {{subject}, false}
set searchCount to count search
get count subject
try
repeat with i from 1 to result
set thisSubject to subject's item i
repeat with n from 1 to searchCount
set AppleScript's text item delimiters to search's item n
set thisSubject to thisSubject's text items
if replace's class is list then
try
get replace's item n
on error
get "" -- `replace` ran out of items
end try
else
get replace
end if
set AppleScript's text item delimiters to {result}
set thisSubject to "" & thisSubject
end repeat
set subject's item i to thisSubject
end repeat
end try
set AppleScript's text item delimiters to ASTID
if not returnList then set subject to subject's first item
return subject
end considering
end str_ireplace
If it’s just the case you want to change, then I’d do something like this:
property sLower : "abcdefghijklmnopqrstuvwxyz"
property sUpper : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
tell application "TextEdit"
-- open file
set theAlias to choose file
set theDoc to open theAlias
-- get text contents
set theText to text of theDoc
-- your conversion code here
-- e.g. begin
set newText to "" as Unicode text
try
considering case
repeat with i from 1 to number of characters in theText
set thisChar to character i of theText
-- convert character by character to lower case
if thisChar is in sUpper then
set newChar to sLower's character (offset of thisChar in sUpper)
else
-- if the character isn't in our upper case string we don't touch it
set newChar to thisChar
end if
(*
-- convert character by character to upper case
if thisChar is in sLower then
set newChar to sUpper's character (offset of thisChar in sLower)
else
-- if the character isn't in our lower case string we don't touch it
set newChar to thisChar
end if
*)
set newText to newText & newChar as Unicode text
end repeat
end considering
on error eMsg number eNum
display dialog "Error converting text: " & eMsg & " (" & eNum & ")" buttons {"OK"} default button 1
end try
-- e.g. end
-- replace text
try
set text of theDoc to newText
on error eMsg number eNum
display dialog "Error setting text: " & eMsg & " (" & eNum & ")" buttons {"OK"} default button 1
end try
-- close and save file
try
--save theDoc in theAlias
close theDoc saving yes saving in theAlias
on error eMsg number eNum
display dialog "Error saving: " & eMsg & " (" & eNum & ")" buttons {"OK"} default button 1
end try
-- reopen
open theAlias
end tell
I guess you know that AppleScript isn’t the best choice for string conversions.
If you have long text, you’ll be happier using a faster language or shell script.