Find and replace Script editor from outside app

Hi folks, I wish I was smarter with AppleScript. OR, I wish Appelscript could be a little more clear … oh well.

I need help, please. I want to paste a replaced line into the Script editor. But I can not find an example anywhere. This is something I wrote so that I could keep adding changes to my spell checking in Mailsmith. After running a script that puts up a new line with replace… example, I want the script to open the script editor (the file is already open or it will hang indefinitely).

I want to paste the results into the end of the file but of course “end tell” is already at the end. So, I need to “find” the line “end tell” and replace it with what I am pasting (adding my own “end tell” to the pasted text)

Or, I need to learn how to insert text. It appears this can be done, but I sure cant find a clear example of how it can be done.

Thanks for any help.

tell application "Script Editor"
	activate
	tell front document
		set newtextpaste to contents
		set contents to newtextpaste & return & return & newaddition

-- Good to this point, but it pastes after the "end tell" so it wont compile or save.

		-- here newaddition contains the added replace line with the newly added and corrected misspelling.
		-- does not work (trial and error here folks): find selection contains "end tell" does not work (trial and error here folks)
		-- - does not work (trial and error here folks): searching in text 1 of text document 1 options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false} with selecting match		
		-- save (This should work but I have it taken out until I get it figured out)
	end tell
end tell

Hi,

try something lke this:

set newaddition to "beep"
set LF to ASCII character 10
tell application "Script Editor"
	tell document 1
		set newtextpaste to paragraphs of (get contents)
		set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, LF}
		set newtextpaste to (items 1 thru -2 of newtextpaste & LF & LF & newaddition & last item of newtextpaste) as Unicode text
		set AppleScript's text item delimiters to ASTID
		set contents to newtextpaste
		compile
	end tell
end tell

Wow, pretty complex. I will give this a try and get back to a post if I have a problem with it. I dont think I understand it at all but, if it works, great.

Guess it was pretty foolish to think I could get this with trial and error. The AS dictionary does say there is an insertion point command though. I can’t make sense of the AS dictionary to this day as it uses no contextual examples. But, I would have thought that there should be something similar to other scripts I see out on the net like;

set insertion point before word 1 to “Only a test”.

I might assume this could be extrapolated to something like find “end tell” and replace with what I have to paste. Or at least;

set insertion point before first instance of word “end tell”.

How does the script editor use “insertion point”?
How does the script editor use “find and replace or find with replacing”?

Can you give me an example so that I can at least see this used in an actual script? Woe the dictionary isn’t written to give contextual examples.

Thanks very much.

Well, it is a mystery to me how, but it worked very well, thank you. I just pulled the two LFs as they were not needed.

The only minor annoyance now, if anyone is a Mailsmith scriptor, is that I am using a text window in Mailsmith to do the replacing of the misspelled and then the correctly spelled word. But for some reason the script is not acting to bring the mail browser to the front window.

The first part goes like this:

-------------------------------> SCRIPT A
tell application “Mailsmith”
activate
set newword to contents of selection
set thestring to "replace " " & newword & “" using " XXXX" searching in text 1 of message window 1 options {search mode:grep, starting at top:true, wrap around:false,backwards:false, case sensitive:false, match words:false, extend selection:false}” & return & return
tell (make new text window with properties {name:“temptextwidow”})
set contents to thestring
set bounds to {1, 714, 1021, 1438}
end tell
end tell

tell application “Mailsmith”
activate
set zoomed of mail browser to true
end tell

– This should leave the selected word active and in the front window, but it doesn’t. This script is for learning the words as I correct them and adding them to the dictionary. The next script is going to take the corrected word, still selected after correcting, and replace the “XXXX” with the new word, the use your provided script to finish by adding the corrected phrase into the spell checking dictionary.

– I set the line about set bounds (set bounds to {1, 714, 1021, 1438}) just to get the temp text window out of my way. I would much prefer to hide it or send it to the back, bit I can’t find a way to do that.

-------------------------------> SCRIPT B
set newaddition to “beep”
set LF to ASCII character 10

tell application “Mailsmith”
activate
set newword to contents of selection
tell text window “temptextwidow” to find “xxxx” searching in text 1 of text document 1 options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false} with selecting match
tell text window “temptextwidow” to set selection to newword
tell text window “temptextwidow” to set newaddition to contents of text 1
end tell

tell application “Script Editor”
tell document 1
set newtextpaste to paragraphs of (get contents)
set {AsTID, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, LF}
set newtextpaste to (items 1 thru -2 of newtextpaste & newaddition & last item of newtextpaste) as Unicode text
set AppleScript’s text item delimiters to AsTID
set contents to newtextpaste
compile
end tell
end tell

I hope this helps. If this would be useful to any other users of Mailsmith I am happy for it.

Is this what you are after?

set new_line to "beep"
tell application "Script Editor"
	set selection to paragraph -2 of front document
	set Old_line to contents of selection
	set contents of selection to Old_line & new_line & return
	compile front document
	"This will be the selection"
end tell

You do not have to keep old_line in

Can you say more about how to use this line?

set selection to paragraph -2 of front document

Meaning, what is this going to point to (so I can better understand how to reuse it in the future)? Specifically, what is “paragraph -2” and what differentiates it from the last paragraph, or the third paragraph, etc.? does the dash in front of the two mean select the second from the last paragraph? Or is this really pointing to the second paragraph?
I still don’t see how insertion point is used but I think you are showing me another way to do it, yes?

Thanks for any and all attemps to get through my thick head about using AppleScript.

Yes,

You can use the numbering system in applescript : 1 to -1

1 counts down from the first item, and useing - 1 counts up from the end of the items

Say I had a doc with 5 paragraphs which I know there are 5 paras.
And I wanted to get everything from paragraph 1 to paragraph 4 (first through second from last I would use,
set P to paragraphs 1 thru 4 of …

But what if I do not know how many paras. there are.
I would use,
set P to paragraphs 1 thru -2 of …

does that help?

Yes, that is a good start, thank you. Continuing, It would help me to make more sense of using Appescript if I understood not only how the dictionary commands work, but how I can actually tell what I am selecting, as shown from your explanation. I am trying to tweak what I have as I see that I want the script to end with a two line alert as the script is taking more than 10 seconds - and growing, to run. I have tried all sorts of ways of changing what I have been given but so far I am not getting it right. This is the end of the script:

end tell
beep 2
display dialog “Double Caps Script has finished”

I tried changing the selection from “-2” to “-4”.
I tried adding the final data to a new variable, thinking it could clean the end and append each time:
set tailendcharly to return & “beep 2” & return & “display dialog "Double Caps Script has finished"” & return & return
And then …
tell text window “temptextwidow” to set newaddition to contents of text 1 – & return & return & tailendcharly

But clearly I am not understanding what I am doing - yet.

  1. What makes a paragraph in AppleScript’s reasoning? Every carriage return I would assume makes a new paragraph, and if that is true, there doesn’t need to be any written text at all to call a line a paragraph. Am I following this correctly so far?

In my example, “end tell” is the last line (until I get this new way fixed), so it would also be the last paragraph. When I am looking at;

set selection to paragraph -2 of front document

would this line put the selection or insertion point, on the line just before the (currently) last line of “end tell”?

  1. The dictionary specifies insertion point but I still don’t see it used anywhere. Can you suggest it written correctly once? Do I even need it if I follow your earlier suggestion, assuming my interpretation is correct? If I am following you and I use that line

set selection to paragraph -2 of front document

Would I even need the insertion point statement at that time? Still, I would like to see how it is used if I could.

  1. Is there no way in script editor to use AppleScript to find a word with replacing? It is perhaps the grandest conundrum of may in comprehending AppleScript that the way some applications use the AppleScript language is not the same way other applications use it. AppleScript editor will let you find and replace, manually. And many other AppleScript savvy applications will let you find with replacing - the beginning point of my script actually. It just makes no sense to me that the script editor allows find and replace, but that function may not be scriptable Since end tell is only used once in my example, this would be a very simple solution to try.

Apple likes to call AppleScript a plain English programming language and it may be all that. But the way different applications omit or apply or allow you to record script steps, or don’t let you record script steps, or allow some things to be recorded but mysteriously omit other things from being recorded, is as plain English as coming from inner city Boston and trying to have a conversation in a high brow southern society while you pick up deep southern dialects on the fly. OR someone from laid back surfer dude southern California trying to talk about something really important to someone living deep in Brooklyn. Yes, its all English language, sort of… :wink:

Ongoing thanks. I hope my humorously intended irony about my frustration in using AppleScript isn’t offending anyone. If I didn’t get help with this here I would not be able to do it at all. I really do appreciate any assistance.

Hi, levelbest;

First, you really ought to read these tutorials by Craig Smith to clarify some of the things you’re asking here. I think they will substantially ease your frustration.

AppleScript Tutorial for Beginners I - Getting Started & Script Editor
AppleScript Tutorial for Beginners II - Variables and Dictionaries
AppleScript Tutorial for Beginners III - The Power of Lists
AppleScript Tutorial for Beginners IV - Records & Repeats
AppleScript Tutorial for Beginners V - Testing & Shorthand
AppleScript Tutorial for Beginners VI - User Interaction
AppleScript Tutorial for Beginners VII - Errors
AppleScript Tutorial for Beginners VIII - Getting the Drop on Droplets

Second, I’m not sure that editing a script is the way to go with your problem. Your script should make use of

set newText to text returned of (display dialog "some question" default answer "something")

so that you’ve got your change as a variable. I confess that I haven’t entirely understood what you want to do, but in my experience editing a script isn’t a good way to proceed.

I will read through the links you posted. Thanks.

The reason I am scripting the script editor to answer your question as to what I am actually doing is to update a list I am using to spell check in Mailsmith. After I started I also realized I can do most spell checking with Spell Catcher or Grammarian. And I do use Grammarian most of the time. Call this a learning experience with AppleScript for me then. And to be fair it is really not possible in any way I have seen to update the spelling lists in either spell checking program except for one word at a time.

The best spell checker I have ever had was Eudora. Eudora allowed me to keep a text file, easily updated, and it checked live as I typed, replacing my bad typos from the words on that list.

I also used some grep tricks to kill my often used mistake of fat fingering the first two letters of some words when the first letter was capitalized.

I have a script now that continues to grow. It holds many misspelled words and their replacement correctly spelled words.

The reason I am editing the script is because every time I run the spell checking script and errors are still found, I have to add the new errors to the spell checking scrip[t.

I hope that clarifies it?

I wish there was a better way, or a faster way to do this. As I mentioned the spell checking script is already getting slow. It does have lots to check.

If you can suggest a faster way of doing this, or more efficient, I would love to hear about it. Right now, aside from the grep piece, I have one of the following lines for each misspelled word. There is also a section with a slightly different line if capitalization is part of it.

replace " Iwas" using " I was" searching in text 1 of message window 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}

This is a quickly cobbled to gather script, Its to give you an idea for an approuch you could take…

if you have a plain text file with all your words in, formated like so:

iwas, i was
heis,he is

each line has one incorrect spelling and one correct spelling seperated by a comma.

you can have the script load them and then use that to check.
You can even have your newly found words written to the file.( not included here, plenty of examples on site on how to write
to a file. But if it were me, I would use the shell command echo, to append the text)

I do not have Mailsmith so can not test the script below. and as I said it is really to see if there is a different approach.

(* read file spell.txt *)
set cat to paragraphs of (do shell script "cat /Users/username/desktop/spell.txt")

set checkList to {}
set correctList to {}
(*make two lists from each paragraph found in cat*)
repeat with i from 1 to number of items in cat
	set this_item to item i of cat
	set AppleScript's text item delimiters to ","
	copy text item 1 of this_item to end of checkList
	copy text item 2 of this_item to end of correctList
end repeat
set AppleScript's text item delimiters to ""

(* use the two new lists, to checkList and correctList to check and correct spelling mistakes found*)
repeat with i from 1 to number of items in checkList
	set in_correct to item i of checkList
	set this_correction to item i of correctList
	replace in_correct using this_correction searching in text 1 of message window 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
end repeat

edit added some notes to script

Cool, a new twist. Does this method allow for caps sensitive words? I had two lists, one that checked with caps sensitive matches and one that did not.

I confess I am not quite understanding your using the “cat” phrase? I looked around on Google and it seems a needed prefix to the file path when using shell scripts - a new area for me, but I can’t seem to use it without an error that “the file or file path does not exist”.

I tried setting the correct path to a variable and prefixing “cat” and not, with the same bad results.

set thePath to POSIX path of ("Macintosh HD:Users:mydirectory:Library:Application Support:Mailsmith:spell")
-- set cat to paragraphs of (do shell script "cat" & thePath)
set cat to paragraphs of (do shell script thePath)

Sorry to be such a simpleton but, can you please clarify this for me?

Thanks

Because there’s a space in the address, you either have to escape the space with a back slash or much simpler use "quoted form (which inserts single quotes):

set thePath to quoted form of POSIX path of ("Macintosh HD:Users:mydirectory:Library:Application Support:Mailsmith:spell")

quoted form always works so I always use it, need it or not.

Shucks, that didn’t work either. Between posts I was reding on the Apple tech pages and I thought I had it figured out. I am very inexperienced with Unix so I am pretty much cutting and pasting here.

http://developer.apple.com/technotes/tn2002/tn2065.html

I tried using the example on that page of the following to check the correct path:

It seemed correct and I thought I had added extra quote marks in the following example: I also put “CAT” back in, remember, I still don’t know why your example showed “CAT” in the path name but it seems correct.

You will see I have introduced a new handler : considering
Followed by the attribute case
this will allow the part of the script between the considering case and end considering to be able to consider the case of the words in use.

Any words looked at outside of this, case will not be considered.
So you could have one check inside and a second check outside.

To add to Adams post, you must have a space after the word cat in the do shell script.
Incorrect form: do shell script “cat” & quoted form of thePath
Correct form: do shell script "cat " & quoted form of thePath
Without a space the shell thinks the command cat is: cat/Users/mydirectory/Library/Application\ Support/Mailsmith/spell
instead of : cat

(* read file spell.txt *)
set cat to paragraphs of (do shell script "cat /Users/mydirectory/Library/Application\\ Support/Mailsmith/spell")

set checkList to {}
set correctList to {}
(*make two lists from each paragraph found in cat*)

repeat with i from 1 to number of items in cat
	set this_item to item i of cat
	set AppleScript's text item delimiters to ","
	copy text item 1 of this_item to end of checkList
	copy text item 2 of this_item to end of correctList
end repeat
set AppleScript's text item delimiters to ""

(* use the list checkList and correctList to check and correct spelling mistakes found*)
repeat with i from 1 to number of items in checkList
	considering case (* make the words case sensitive *)
		set this_check to item i of checkList
		set this_correct to item i of correctList
		replace this_check using this_correct searching in text 1 of message window 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
	end considering
	(* none case sensitive*)
	--replace this_check using this_correct searching in text 1 of message window 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
end repeat


Note: the \ in cat /Users/mydirectory/Library/Application\ Support/Mailsmith/spell
Unix sees space normally as part of the command.

The [b][/b] is used to esacpe the space, so unix can treat the space as a literal character rather than part of the command.
The second [b][/b] is only used in applescript, to escape the first [b][/b] needed for unix

Yea, I tried it with a single slash, didn’t think of the two. Thanks.

Now it is up and working. Thanks. I will test run it for a while and see it compares to what I had before.

One last little missing detail though. When I said I wanted the capitalization checked, here is what I was meaning by saying that.

My other script had two instances of replaced words. One list was case sensitive:false, and the second list was case sensitive:true. I was adding misspelled words, depending on whether I cared if they needed capitalization when corrected or not.
– Case sensitive = FALSE follows:

replace " tat " using " that " searching in text 1 of message window 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}

-- Case sensitive = TRUE follows:

replace " php" using " PHP" searching in text 1 of message window 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:true, match words:true, extend selection:false}

I also had the following grep piece that was intended to correct my fat fingered double capitalization errors. In this piece, ERrors would become Errors.

replace "\\b([A-Z])([A-Z])([a-z])" using "\\1\\l\\2\\3" searching in text 1 of message window 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:true, match words:false, extend selection:false}

I tried placing this as my last line of code but the script doesn’t seem to be using it.

Neither this \b([A-Z])([A-Z])([a-z]),\1\l\2\3
nor this “\b([A-Z])([A-Z])([a-z])”,“\1\l\2\3” work.

EDITED: Forgot to call the second instance “True”.

Sorry, As I said I do not have mailsmith, so it hard to test your scripts.

But I would try and wrap the case sensitive TRUE part of the script in a considering case handler.

You could also have your case sensitive text in the same file as a third entry on the line.

photp,photo,Photo

Add a lines in the script
set CASEtList to {}
put under the lines similar to it in the script

copy text item 3 of this_item to end of CASEList
put under the lines similar to it in the script

set this_CASE to item i of CASEtList

put under the lines similar to it in the script or where needed in your changes. ( remember the i in item i of CASEtList is number the repeat has reached, So if there are 10 words in checklist the repeat will repeat 10 times, and i will iterate through 1 to 10 . I say this to remind you that the last line will need to be in a repeat loop )