Help please on a MAIL script...

I have an annoying typing habit I cant fix. I hit the space bar too soon, most often when typing the word “the”. So my emails are littered with stuff lik ethis (actually, I just typed one w/o even trying to!)

I have tried to write a script to using FIND function in MAIL so that I get a quick hot key to check my entire currently typed email to fix these typos (like, find " e" replace with "e ". But cant figure out the coding nor how to start the RECORD function in Script Editor. I dont understand coding so I couldnt even manipulate an existing simple script to do what I want.

Can anyone help? Either by emailing me a short script for this annoying problem, or point me to where I might easily learn to create it?

thanx
Im running Leopard 10.5.2 on a very new laptop
Arnie Sawyer

There are several ways to do replacements in text. Here is one way.

You will need to enter the search and replace terms for each typing error you have.

Once you have typed your email execute this script. It will copy the text, format
per your search and replace list and paste back in the email window. Right now it
will only work on “New Message” windows and not on saved drafts because of the
window name.

hth,

Craig


--Add your search terms here
set searchList to {" ethis", "search_term2", "search_term3"}
--Add your replacement terms here
set replaceList to {"e this", "replace_term2", "replace_term3"}

activate application "Mail"
tell application "System Events"
	tell process "Mail"
		
		set mailText to get value of static text of group 1 of UI element 1 of scroll area 3 of window "New Message"
		
	end tell
end tell

repeat with i from 1 to count of searchList
	set this_item to item i of searchList
	set mailText to my searchANDreplace(item 1 of mailText, this_item, item i of replaceList)
end repeat

set the clipboard to mailText

my pasteAll("Mail")

on pasteAll(target_program)
	activate application target_program
	tell application "System Events"
		tell process target_program
			keystroke "a" using command down
			keystroke "v" using command down
		end tell
	end tell
end pasteAll

on searchANDreplace(stringTOsearch, searchitem, replaceitem)
	set oldDelim to AppleScript's text item delimiters
	set AppleScript's text item delimiters to searchitem
	set thetextitems to text items of stringTOsearch
	set AppleScript's text item delimiters to replaceitem
	set stringTOsearch to thetextitems as string
	set AppleScript's text item delimiters to oldDelim
	return stringTOsearch
end searchANDreplace

okay. I clicked on your first line and it pasted your script into the script editor window. It came in all in purple type. Then I clicked Compile and the text became blue, green and grey. So since I only need to replace my th e issues, I deleted the other text replacements you gave me. Then saved the script.

okay so far?

now, the dumb question: How do I activate it when Im done writing an email? I see that I can add it as a rule, but would it take effect when Im typing? Or do I set up a Command-Key, and how do I do that?

If you have the script open and text in a “New Message” window then you only need to run the script by clicking
the “Run” button in the script window.

There is a lot of information on this forum for attaching hot keys to scripts, running them from the script menu
or as an application. No need to repeat that information here.

Cheers!

Craig

its funny… I post to a collectors site every day for years, and every now and then someone new will pop up like they just discovered “scripts” in this case, and a lot of th eregulars with 1000s of posts will just say “thats been answered before… go find it”. And we regulars read it and say “yeah, its old news already. boring!” And, being an old-timer over there, I always agree with them. So now I see the world from the OTHER SIDE. Now Im the hopelessly ‘without a clue’ newbie asking the same ol’ same old questions in a room full of old timers. So while I may deserve it, sure doesnt feel much better than a smack in the head.

but wish me luck. there’s probably a thread called " Hey lazy dummy - - create scripts here!" Or maybe someone can post a link to one… like crumbs in the forest maybe??. I dont even know what half the stuff you guys talk about here on Applescript mean!! Id also like to think there was some instructions from Apple. I searched there too. Other programs, like Filemaker have built in scripting… And I think older Mail versions had scripting built in, didnt they?

: )

I think you are taking this a little hard. I am not telling you “Good luck, you’re on your own.”
I am saying there are lots of ways to do this. It would take me a very long time to cover them
all and they have already been explained in detail other places. I would not have suggested
you search for this if I thought you would not easily find it. I apologize that I came off terse.
That was not my intention.

To answer you question.
The method I use for setting up hot keys to scripts is a program called DragThing. It is a
replacement for the dock but has lots of functionality. You can find it at www.dragthing.com.
Not free but very useful. $25 I believe.

You can also use the Script’s Menu but you have to activate it first. Open AppleScript Utility
in the AppleScript folder inside your Apps folder and turn on “Show Script menu in menu bar”

Now the Script Menu is in the top right of your screen.

Click it and then Open Scripts Folder => Open User Scripts Folder

Move your new script in there and when you want to execute it click on it from the drop-down
menu.

That should get you setup.

Back to your last post.
I know it is sometimes difficult to find the answers you are looking for especially in a new
language and forum. I don’t think it is unreasonable or offensive to suggest searching for
subjects that have already been covered many times on the forum. That is what the forum
is here for. I find myself asking very few questions anymore, not because I know so much,
but because I find someone else has already asked and been answered my same question.

The more time you spend on the forum the easier it gets to find what you want. Not always easy,
mind you, but easier.:slight_smile:

Regards,

Craig

Im sorry I was so crabby. I apologize for being spoiled and I blame Apple! Ive been on Macs since 1979 and the few times I have to dig around to solve a problem like this are, between rare and never. But when I do Im frustrated when the solutions arent as easy as everything else in the Mac world. I know that mostly because Im happy with what Im given, system wise, and not trying to make it even better. But imagine my frustration going back and forth with HELP and the Apple online Script area. I feel like Im looking for a needle in a haystack! So I wrote that post out of my frustration, and tried to keep it as upbeat as I could since obviously, you are just trying to help a stranger… I searched around and found a couple of close situations but no solution…

But I just did as you suggested. Added my script to the Scripts folder in my User Library. then I ran the script as I modified yours above (which had three items to look for and fix. The script worked - - but not as intended. it deletes everything but the first letter…

“teh th eryu” becomes just “t”

Maybe it would be a simpler task to have the script look only in whatever text I have selected? Then my solution would be write an email (or any other text since it would no longer be MAIL specific and I make these typos everywhere!) hit COMMAND-A then COMMAND-X or F-X and voila! A simple 2-step solution. Or a one-step if the COMMAND A were included in the macro/script. That sounds easier…

Id like to add that my little exposure to coding here has been fascinating. I used to set type and used a language that is very much like HTML today. And I remember the joy of getting each letter of each command just right so the type would appear as I intended, but it brings back the ugghh of having to search line by line to find the errant keystroke or wordspaces that threw it all off! I wish I could look at these new codes Im seeing and really SEE them… instead of not knowing a thing about the rules. I truly appreciate your patience and help.

Hey Arnie,

I understand. Using AppleScript can be a very frustrating or exhilarating experience
depending on how your success is going at the time.

I would suggest getting a couple of books to help in learning AppleScript:

  1. Hanaan Rosenthal’s “AppleScript: The Comprehensive Guide to Scripting and Automation on Mac OS X”
  2. Matt Neuburg’s “AppleScript: The Definitive Guide”

These two alone can keep you busy for a very long time.

Start with Hanaan’s and once you get the gist of AppleScript then read Matt’s. His is more technical but
a must read once you have some experience.

As for your script. I think you are right on putting it into another app first. How about TextEdit since
it is already on your machine.

Hang in there. It does get easier.

Craig

Try this. I will put the new text on your clipboard so you can paste it wherever you need.


--Add your search terms here
set searchList to {" ethis", " ebest", " ethe", " eis"}
--Add your replacement terms here
set replaceList to {"e this", "e best", "e the", "e is"}

tell application "TextEdit"
	set mailText to text of document 1
end tell

repeat with i from 1 to count of searchList
	set this_item to item i of searchList
	set mailText to my searchANDreplace(mailText, this_item, item i of replaceList)
end repeat

set the clipboard to mailText

on searchANDreplace(stringTOsearch, searchitem, replaceitem)
	set oldDelim to AppleScript's text item delimiters
	set AppleScript's text item delimiters to searchitem
	set thetextitems to text items of stringTOsearch
	set AppleScript's text item delimiters to replaceitem
	set stringTOsearch to thetextitems as string
	set AppleScript's text item delimiters to oldDelim
	return stringTOsearch
end searchANDreplace

Ive ben playing with what youve given me. Heres where it stands. I now have a working script in my Scripts menu bar list. It successfully corrects all my th e issues. So, going for broke, Ive been trying to also make it fix my other typo, teh for the, but adding a new term to fix causes the problem I mentioned earlier, namely that th eresult is all text is erased leaving just the first letter. So I must be doing it wrong. I copied what appeared to be th emethod: {“term”, “term”}. But just ading one more term kills the scrit as written.
BTW - -you see I made the mistake three times right here, so solving my Mail problem is juts the beginning! (juts is yet another I guess!! oy!

THIS WORKS:

.
.

THIS ERASES ALL BUT FIRST LETTER

.
.
.
heres the entire rest of the script that works

Arnie,

Use the script I did in my last post. If you look closely, I made several changes
to fix that issue. It had to do with “item 1 of mailText”.

Also, when I was testing with the one that grabs the text from Mail I was not
getting good results. That is why I moved it to TextEdit. It seemed to work
consistently there with multiple search/replace criteria.

Give it a try and let me know.

Craig

ok. but, Im a little confused about the steps I take. please explain. Do I have to do anything after I type (in whatever program Im working in) first? Or just type, click the SCript, then paste into my document. Im confused where TextEdit comes into play, whether I have to put the text there, or if the script does that too.

Yes, type the text in TextEdit and then run the script.

The script will grab the text from TextEdit,
correct the typos and place the corrected text
on your clipboard.

Now you can paste it in any application.

Let me know if this works.

Craig

If you want to automatically add this to a new email message then
try this. I commented out the line that added it to the clipboard
since it would no longer be necessary.


--Add your search terms here
set searchList to {" ethis", " ebest", " ethe", " eis"}
--Add your replacement terms here
set replaceList to {"e this", "e best", "e the", "e is"}

tell application "TextEdit"
	set mailText to text of document 1
end tell

repeat with i from 1 to count of searchList
	set this_item to item i of searchList
	set mailText to my searchANDreplace(mailText, this_item, item i of replaceList)
end repeat

--set the clipboard to mailText

tell application "Mail"
	activate
	set theMessage to make new outgoing message with properties {visible:true, content:mailText}
end tell

on searchANDreplace(stringTOsearch, searchitem, replaceitem)
	set oldDelim to AppleScript's text item delimiters
	set AppleScript's text item delimiters to searchitem
	set thetextitems to text items of stringTOsearch
	set AppleScript's text item delimiters to replaceitem
	set stringTOsearch to thetextitems as string
	set AppleScript's text item delimiters to oldDelim
	return stringTOsearch
end searchANDreplace

I did as you suggest… (though I altered the beginning to find and fix my specific typos. (I only have three so I doubled one up to match the four you gave me.)). Then I typed a sentence in Textedit and ran the script, and… IT WORKS! It fixed all three kinds of typos. Very sweet!

Of course, writing emails and all other text in Text edit instead of Mail etc is kinda a big workaround. Can this same script not copy and paste selected all text INTO TextEdit first and do its magic? Thats kinda the timesaver a script is for, no?

I hadnt seen this post before I replied… Honestly, I dont mind ending up having to PASTE as much as I do having to actually open and type in TextEdit. I rarely if ever use it except when I double click a system file… Blasphemy I know!

what Im saying is that thanks to you, Ive got a script I can use.

heres another angle on it: my iPhone(and my older Blackberry) have a dictionary and chooses the word it thinks I want. Is that available for Leopard? If so, Im all set, right? I just add my typing foibles to the user dictionary and Im done.

I think this will do it. Let me know if you find any bugs.
UI scripting is not always reliable.

Type your text in a “New Message” window and then
run the script.

Craig


--Add your search terms here
set searchList to {" ethis", " ebest", " ethe", " eis"}
--Add your replacement terms here
set replaceList to {"e this", "e best", "e the", "e is"}

set newText to ""
activate application "Mail"
tell application "System Events"
	tell process "Mail"
		
		set rowCount to get value of attribute "AXChildren" of UI element 1 of scroll area 3 of window "New Message"
		set numRows to count rowCount
		
		repeat with i from 1 to numRows
			set mailText to get value of static text of group i of UI element 1 of scroll area 3 of window "New Message"
			try
				set thisLine to item 1 of mailText
			on error
				set thisLine to ""
			end try
			set newText to newText & thisLine & return
		end repeat
		
	end tell
end tell

repeat with i from 1 to count of searchList
	set this_item to item i of searchList
	set newText to my searchANDreplace(newText, this_item, item i of replaceList)
end repeat

set the clipboard to newText

my pasteAll("Mail")

on pasteAll(target_program)
	activate application target_program
	tell application "System Events"
		tell process target_program
			keystroke "a" using command down
			keystroke "v" using command down
		end tell
	end tell
end pasteAll

on searchANDreplace(stringTOsearch, searchitem, replaceitem)
	set oldDelim to AppleScript's text item delimiters
	set AppleScript's text item delimiters to searchitem
	set thetextitems to text items of stringTOsearch
	set AppleScript's text item delimiters to replaceitem
	set stringTOsearch to thetextitems as string
	set AppleScript's text item delimiters to oldDelim
	return stringTOsearch
end searchANDreplace

Bingo baybee! Works like a charm right in Mail! Thank you, sir. If I discover more typos, can I just add them to the string up top? And, I noticed a typo right here so I copied and pasted this first sentence into TextEdit, ran the old script, and came back and pasted the fix. Im going to love not having to go back and fix all my typos… maybe I oughtta just take a typing class to stop my thumb from "premature space bar syndrome??? : )

Thank you, Craig!

You are very welcome.

You can add as many search/replace items as you want.

Glad I could help!

Craig