TextEdit.app
TextEdit.app
- Subject: TextEdit.app
- From: John Woodrow <email@hidden>
- Date: Tue, 1 Jul 2003 12:48:36 +0100
Hi List.
I've lurked long and hard around here and learnt a lot - thanks. This
is my 'coming out post', so be gentle with me :-)
Inspired by recent threads on 'Remove spaces' and on 'Scriptable Text
Editor for OSX', I started playing around with TextEdit.app. and was
taken aback both by the (small) size of its dictionary, and by not
being able to find much help on the web.
With trial and error, I came up with this script which does a global
find and replace, preserving styled text, and which is offered as a
small step to help remedy the above. No doubt you can suggest
improvements, and there is no error trapping. The 'business part' is
Vanilla AS. Replacing it with a call to Perl would add regular
expressions, but handling selection/insertion point with TextEdit's
dictionary is presently beyond me - TextEdit is not like STE.
-- start script--
global replaceCount
set replaceCount to 0
set searchString to text returned of (display dialog "Please enter the
search string" default answer "")
set replaceString to text returned of (display dialog "Please enter the
replace string" default answer "")
tell document 1 of application "TextEdit"
repeat with i from 1 to count of (get attribute runs)
set styleIndex to i as integer
--get text and formatting of attribute run
set theText to attribute run styleIndex
if theText is not "" then
set theFont to font of attribute run styleIndex
set theColour to color of attribute run styleIndex
set theSize to size of attribute run styleIndex
--do replace on text
set attribute run styleIndex to replaceSelection of me out of
theText by replaceString instead of searchString
--write formatting back to document
set size of attribute run styleIndex to theSize
set color of attribute run styleIndex to theColour
set font of attribute run styleIndex to theFont
end if
end repeat
end tell
to replaceSelection out of theText by replaceString instead of
searchString
set OTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to searchString
set replaceCount to replaceCount + ((count of text items of theText) -
1)
set theText to text items of theText
set AppleScript's text item delimiters to replaceString
set theText to theText as string
set AppleScript's text item delimiters to OTIDs
return theText
end replaceSelection
display dialog (replaceCount as string) & " instances were replaced."
buttons {"OK"} default button 1
-- end of script --
John
(Subscribed in digest mode - please be patient for reply)
----
John Woodrow
<
mailto:email@hidden>
----
<
http://www.goldendustman.co.uk>
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.