Some applescripting help to make my life easier

Hello everyone:

My work situation is such that I use two laptops. One a Mac (yeah!) and one a PC (bleah). Most of the time I have the PC sitting in my lap and I’m typing, while the Mac sits off to the side checking email, et cetera.

I am trying to automate as much as possible with my Mac so that I don’t have to awkwardly reach over the type things into the Mac when I need it to do something.

I have just begun using the Speakable Items feature to help me with this, and it’s going pretty well, but I’m brand new to creating applescripts and so forth, so I have some very newbie questions. It seems like it should be relatively easy to do these things, but I’ve been scanning this board all afternoon and just can’t seem to find the answers to my questions. I’m hoping one (or more) of you kind souls will be able to give me some tips and advice.

First, I have everything set up so that when my Mac purrs to tell me there’s a new email, I get simply speak to the Mac and up pops the new email. However, I want to take it a step further. I’d like the Mac to read the message aloud to me (assuming it’s readable, of course, and not HTML). I just can’t seem to make that happen. I’ve tried all kinds of scripts and tricks and whatnot, but it seems like the only way to make the Mac read the message aloud is to reach over and select the Edit menu, scroll down to Speech, and then click on Start Speaking. What I really want is a script that will allow me to say “Read Message” or some such, and the Mac will start speaking the message out loud for me.

Secondly, I have several URLs already set up in Safari where I can just say the name I’ve assigned and that page pops up. However, many of these pages require a login and then a click on a “go” or “login” button, or something similar. I have Safari enabled to remember my user names and passwords since security is not an issue for me. Is there a way to speak to the Mac and tell it to click on the button for me?

Third (and lastly :), I’d like to be able to “dictate” to my Mac on occasion. For example, if I tell it to open the dictionary, I’d like to be able to spell out the word I would like the Mac to look up for me. Is there a way to tell it to type each letter as I speak it?

Thanks in advance for any and all help. You folks who have mastered applescript are amazing! (I get a migraine just looking at some of the scripts you post in answer to others’ questions!)


Model: Black MacBook (Intel)
AppleScript: 1.10.6
Browser: Safari
Operating System: Mac OS X (10.4)

Here are a few suggestions, dms, all of which use GUI Scripting ” which should therefore be enabled on your machine.

Save the following (as: script) in “/Users/User Name/Library/Speech/Speakable Items/”, naming it something like “Read Mail Message.scpt”. Then, when you want to call it (and assuming Speakable Items is switched on), just say “Read Mail Message”.

activate application "Mail"
tell application "System Events" to click menu item "Start Speaking" of menu 1 of menu item "Speech" of menu 1 of menu bar item "Edit" of menu bar 1 of application process "Mail"

Pressing the Enter or Return key should normally do the trick. This is the GUI Scripting equivalent of hitting Enter:

activate application "Safari"
tell application "System Events" to key code 52

(Again, if you want to trigger it via Speakable Items, use the technique outlined above.)

The following should get you started. Since the recognizer may have some difficulty with short sounds, I’ve suggested including something like the NATO phonetic alphabet ” which might help to clarify things when alphabetical characters are phonetically similar. (The script optionally opens the Speech Commands window to display the list of commands available, as well as the phonetic list.)

property open_window : true (* set to false if you don't want the Speech Commands window to open *)

property alpha_list : {" ", "-", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
property phon_list : {"Space", "Hyphen", "Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel", "India", "Juliet", "Kilo", "Lima", "Mike", "November", "Oscar", "Papa", "Quebec", "Romeo", "Sierra", "Tango", "Uniform", "Victor", "Whiskey", "Xray", "Yankee", "Zulu"}
property cmd_list : {"New Word", "Next Row", "Previous Row", "Delete", "Select", "Forget It", "Thanks"}
property key_list : {51, 125, 126, 51, 52}
property display_list : cmd_list & phon_list
property listen_list : display_list & alpha_list
property phon_count : count phon_list
property key_count : count key_list

to end_dictation(hide_dict, win_closed)
	tell application "System Events"
		if open_window and win_closed then click button 1 of window "Speech Commands" of application process "SpeechFeedbackWindow"
		if hide_dict then set visible of application process "Dictionary" to false
	end tell
	error number -128
end end_dictation

if open_window then tell application "System Events" to tell application process "SpeechFeedbackWindow"
	set win_closed to not (exists window "Speech Commands")
	if win_closed then tell menu button 1 of window "Speech Feedback"
		click menu item "Open Speech Commands window" of menu 1
	end tell
	repeat until exists window "Speech Commands"
		delay 0.2
	end repeat
end tell

activate application "Dictionary"
		tell application "SpeechRecognitionServer" to set cmd to listen for listen_list giving up after 60 displaying display_list
	on error
		end_dictation(false, win_closed)
	end try
	tell application "System Events" to if cmd is in cmd_list then
		if cmd is in {"Forget It", "Thanks"} then my end_dictation(cmd is "Forget It", win_closed)
		if cmd is "New Word" then key code 0 using command down
		repeat with i from 1 to key_count
			if cmd is my cmd_list's item i then
				key code my key_list's item i
				if cmd is "Select" then my end_dictation(false, win_closed)
				exit repeat
			end if
		end repeat
		if cmd is in phon_list then repeat with i from 1 to phon_count
			if cmd is my phon_list's item i then
				set cmd to my alpha_list's item i
				exit repeat
			end if
		end repeat
		keystroke cmd
	end if
	delay 0.2
end repeat

Have you thought of using VNC to control your Mac desktop from your PC? There are several free VNC clients for Windows (e.g. TightVNC) and OS 10.4 comes with a VNC server already built in (go into System Preferences->Sharing and set up Apple Remote Desktop, turning on the VNC option under access privileges).


Dear Kai:

Thank you so much for your very thorough and helpful replies. All three worked like a charm! You are my hero! :slight_smile:

I do have two small problems, but they exist because I didn’t make it clear enough what I need, not from your splendiferous help. Can I be a bit more of a pain and ask if you can help me with these two small tweaks?

First, with having my computer (whom I’ve name Clifford :slight_smile: read email to me, I still must manually select the text he will read. Is there a way to tell him to move the cursor into the body of the email, select all, and then read? In addition, I changed the script slightly so that I could tell him to stop speaking if the email is long, or I need to stop him to answer the phone, or whatever. However, he doesn’t recognize it when I say the “Stop Speaking” command. I suspect that, like most men :), he cannot listen while he is speaking; therefore, this cannot be accomplished. Howeiver, I just wondered if perhaps you have tried that and gotten it to work for you?

My other issue is with the dictation. It works beautifully with Dictionary, but there is also a website I use to look up the spelling of prescription drugs for my work. I already have Clifford set up so that if I say “Drugs” to him, he will open the webpage for me. I would like to be able to dictate verbally the letters for that one as well. I have discovered that I have to position the cursor in the box for the drug name on the site (don’t know if there’s a way to verbally get the cursor there or not), and then type in my letters. Interestingly, your fix for my “login” issue works here. I used your script and made the speakable command “Confirm.” In this instance, after I type in my characters and say “Confirm” (Blake’s 7, anyone? :), Clifford presses the “go” button for me, so that’s lovely. Now, if I could just get him to put the cursor in the box and type the letters for me, I’d be completely hands free for this task, too.

Once again, Kai, you are an absolute peach and I’m enormously grateful for your help. It would have taken me months to figure this out on my own, and may hair is already gray enough! :slight_smile:


Dear HTH:

Thank you for taking the time to make this suggestion; however, it doesn’t really accomplish what I need. I already have a large number of windows open on my PC when I am doing my work, and I need to open and close many windows/applications on my Mac when I need it to do things for me. Productivity is the key to making money in my business, and the quicker and more painlessly I can accomplish things, the better my workflow and moneymaking. You have given me food for thought for other things I might be able to accomplish, but for my present needs, enabling Speakable Items is by far the best way for me to go.

Thank you again for your thoughtful reply.


Model: Black MacBook (Intel)
AppleScript: 1.10.6
Browser: Safari 417.9.3
Operating System: Mac OS X (10.4)

Dear Kai:

Further to my last post, I played around with your spelling script some more and I now have it working on the drug list page I described in my eariler message. I deleted all references to the dictionary application, gave the new script a new name, and placed in the Speakable Items folder for Safari. It now does exactly what I want. I also made a keyboard command for the tab key (called “tab over”) and I can speak that command to navigate to the input box on the page. It’s a little tedious at the moment because there are six boxes/buttons to get away from before I get to the button I want, but I’m sure I’ll figure out how to refine that as I contintue to play around.

So, please ignore my question about how to make the dictating feature work in Safari. I should have experimented more on my own before I asked you for help on this one!

Also, the read mail function does seem to read the subject mail sometimes and sometimes not. I can’t figure out why it will sometimes read the body without further help from me and sometimes it just stops after reading the header info. I’ll keep playing around and try to figure that one out on my own, too.

I don’t want to pester you with ignorant newbie questions – you’ve already been such a great help.

I also found that using the dictionary and dictating to it too much seems to make Speakable Items crash after a while. What happens is that Clifford will slow down and stop responding. Then he will finally (after as much as a minute) repeat back to me the last letter I dictated, then he’ll just disappear. If I open the System Preferences window, Speakable Items is still turned on, but it no longer responds and nothing is visible on the screen. I have to restart the computer to get it back. It only seems to happen if I dictate a whole rash of words without stopping. If I quit dictionary after a word, then reopen and come back, it’s fine. So, I’ll just keep my word requests to one at a time.

All in all, this is a HUGE help to me. My ideal environment is to sit with my yucky PC in my lap and do my work, occasionally calling out to Clifford to retrieve my emails and read them to me, open a browser window and perform some function, and otherwise never have to reach over to type awkwardly with one hand on the Mac or to put the PC aside and pick up the Mac, unless I’m finished with the PC for the time being.

Thanks once again for all your help. I feel like an episode of Star Trek! :slight_smile:


Model: Black MacBook (Intel)
AppleScript: 1.10.6
Browser: Safari 417.9.3
Operating System: Mac OS X (10.4)