For Programmers: Free Programming Magazines  


Home > Archive > AppleScript > October 2004 > find/replace in TextEdit









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author find/replace in TextEdit
Something

2004-09-28, 3:55 am

Hi

I just can't figure out how to find and replace a chunk of text in OS
10.3.5 Text Edit. I have used this little chunk of code from the AS
examples folder. It was used to work with ScriptEditor. AS complains
that 'selection' is not known by TextEdit. Alternatively I could just
use a nother app to do the replacement (Xcode, AppleWorks) or if it can
be done w/o app. Each time I tackle a project in AS it drives me up the
walls because it's absolutely illogical and inconsistent (even between
Apple's own apps). This should be perfectly simple....


on replace_and_select(target_string, replacement_string)
tell application "TextEdit"
--tell the front document -- this doesn't work in TextEdit
--set this_text to the text
set this_text to the text of the front document --but this does
set this_offset to the offset of the target_string in this_text
display dialog this_offset
if this_offset is not 0 then
set selection to characters this_offset thru (this_offset +
(length of the target_string) - 1)
set the contents of the selection to the replacement_string
end if
--end tell
end tell
end replace_and_select

--
www.acmerecords.com/oddgitjazz.html
Michelle Steiner

2004-09-28, 8:55 am

In article <*****-7AB592.01502828092004@news.indiana.edu>,
Something <*****@*****.***> wrote:

> tell application "TextEdit"
> --tell the front document -- this doesn't work in TextEdit
> --set this_text to the text


tell the front document
set this_text to its text

-- Michelle

--
Stop Mad Cowboy Disease: Vote for John Kerry.
Patrick Stadelmann

2004-09-28, 8:55 am

In article <*****-7AB592.01502828092004@news.indiana.edu>,
Something <*****@*****.***> wrote:

> I just can't figure out how to find and replace a chunk of text in OS
> 10.3.5 Text Edit. I have used this little chunk of code from the AS
> examples folder. It was used to work with ScriptEditor. AS complains
> that 'selection' is not known by TextEdit.


TextEdit support for AS is somewhat basic, and selections are not
supported indeed. Are you sure your sample script was designed for
TextEdit ?

> Alternatively I could just
> use a nother app to do the replacement (Xcode, AppleWorks) or if it can
> be done w/o app.


It can be done w/o app using scripting additions command (eg "offset" in
Standard Additions) or shell tools (eg "sed") using "do shell script".

But it's probably simpler to use another app (BBEdit or Tex-Edit Plus
comes to mind).

Patrick
--
Patrick Stadelmann <Patrick.Stadelmann@unine.ch>
John Stewart

2004-09-28, 8:55 am

In article <*****-7AB592.01502828092004@news.indiana.edu>, Something
<*****@*****.***> wrote:

> Hi
>
> I just can't figure out how to find and replace a chunk of text in OS
> 10.3.5 Text Edit. I have used this little chunk of code from the AS
> examples folder. It was used to work with ScriptEditor. AS complains
> that 'selection' is not known by TextEdit. Alternatively I could just
> use a nother app to do the replacement (Xcode, AppleWorks) or if it can
> be done w/o app. Each time I tackle a project in AS it drives me up the
> walls because it's absolutely illogical and inconsistent (even between
> Apple's own apps). This should be perfectly simple....
>
>
> on replace_and_select(target_string, replacement_string)
> tell application "TextEdit"
> --tell the front document -- this doesn't work in TextEdit
> --set this_text to the text
> set this_text to the text of the front document --but this does
> set this_offset to the offset of the target_string in this_text
> display dialog this_offset
> if this_offset is not 0 then
> set selection to characters this_offset thru (this_offset +
> (length of the target_string) - 1)
> set the contents of the selection to the replacement_string
> end if
> --end tell
> end tell
> end replace_and_select


Text Edit has no "selection" property in it's dictionary. You can't use
what isn't there.

Here is a search and replace handler, just give it the indicated
parameters and it will work. You will need to break the searched text
down to a specific subset of the document's text if you don't want
global search and replacement.

tell application "TextEdit"
set text of document 1 to my doRplc(get text of document 1, "some
text", "other text")
end tell

on doRplc(txtStr, srchStr, rplcStr)
set {oldDelims, AppleScript's text item delimiters} to
{AppleScript's text item delimiters, {srchStr}}
set temp to every text item of txtStr
set AppleScript's text item delimiters to {rplcStr}
set txtStr to temp as text
set AppleScript's text item delimiters to oldDelims
return txtStr
end doRplc


Th

--
Use ROT-13 on the email address for email replies
Michelle Steiner

2004-09-28, 8:55 pm

In article <280920040408449473%cfnzrpu@crevtrr.arg>,
John Stewart <cfnzrpu@crevtrr.arg> wrote:

> Text Edit has no "selection" property in it's dictionary. You can't
> use what isn't there.


That seems to be a shortcoming of all of Apple's cocoa text
applications; you can't get the selection within a message in mail.app
either; "selection" returns the selected message.

-- Michelle

--
Stop Mad Cowboy Disease: Vote for John Kerry.
Something

2004-09-28, 8:55 pm

Thanks all for responding

My original post did say that the code chunk I used as a template was
taken from a AS intended for 'ScriptEditor'. Yes, looking at the
TextEdit AS dictionary I can't find 'selection'. It sure doesn't have to
be done in AS.

The task is as follows:
On my website I make use of MYSQL and frequently need to update/replace
the databases used on the remote server. For this I use PHPadmin to
export the database from my local folder. So far I haven't found a way
for the exported file to include a line of text that will clear the
destination database before importing - thus resulting in duplicate
entries. At the moment I add this line by hand, then upload the import
file to my server from where I have to use my ISP's clumsy MYSQL access
page to insert it into my database on the remote. Just wanting to
automate.

One alternative is to write my own PHP exporter. It's just a little more
work than I had thought necessary.

Peter


In article <michelle-0F874A.01061328092004@news.west.cox.net>,
Michelle Steiner <michelle@michelle.org> wrote:

> In article <*****-7AB592.01502828092004@news.indiana.edu>,
> Something <*****@*****.***> wrote:
>
>
> tell the front document
> set this_text to its text
>
> -- Michelle


--
www.acmerecords.com/oddgitjazz.html
Patrick Stadelmann

2004-10-02, 8:55 am

In article <*****-7AB592.01502828092004@news.indiana.edu>,
Something <*****@*****.***> wrote:

> I just can't figure out how to find and replace a chunk of text in OS
> 10.3.5 Text Edit. I have used this little chunk of code from the AS
> examples folder. It was used to work with ScriptEditor. AS complains
> that 'selection' is not known by TextEdit.


TextEdit support for AS is somewhat basic, and selections are not
supported indeed. Are you sure your sample script was designed for
TextEdit ?

> Alternatively I could just
> use a nother app to do the replacement (Xcode, AppleWorks) or if it can
> be done w/o app.


It can be done w/o app using scripting additions command (eg "offset" in
Standard Additions) or shell tools (eg "sed") using "do shell script".

But it's probably simpler to use another app (BBEdit or Tex-Edit Plus
comes to mind).

Patrick
--
Patrick Stadelmann <Patrick.Stadelmann@unine.ch>
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com