Entourage script works from menu but not keyboard shortcut

I’d like to have a keyboard shortcut for a script that moves a message in Entourage to a folder I’ve called Misc (my only folder starting with the letter M). This script works fine if I run it from my script menu:

tell application "Microsoft Entourage"
	tell application "System Events"
		tell process "Entourage"
			keystroke "m" using {command down, shift down}
			keystroke "m"
			delay 1
			keystroke return
		end tell
	end tell
end tell

However when I try to run it using a keyboard shortcut, it just opens up the message that I’m in my inbox instead of moving it. I have this problem whether I’m assigning a keyboard shortcut from the Entourage script menu via System Preferences or whether I assign a keyboard shortcut via FastScripts Lite. Any ideas on why this would work from the menu but not a shortcut? Thanks.

Keystrokes only work on the frontmost application. Most likely when you call the script from the menu entourage is frontmost so you don’t have a problem. Conversely when you call if from the keyboard shortcut it isn’t frontmost. Easy fix…

Change to this line…
tell application “Microsoft Entourage” to activate

Also don’t put the system events stuff inside of the microsoft entourage stuff… it should be on its own. You really don’t need the “tell process” part either because system events doesn’t tell the process when you do keystrokes… like I mentioned it only tells the frontmost application no matter what. So the final script would look like this…

tell application “Microsoft Entourage” to activate
tell application “System Events”
keystroke “m” using {command down, shift down}
keystroke “m”
delay 1
keystroke return
end tell

Thank you for the suggestion. Unfortunately that’s also not working either when I try to run it from a keyboard shortcut or from the menu (although it works just fine if I run it from Script Editor). Very odd.

Well there’s really only 2 things that can go wrong with keystrokes. 1) the application must be frontmost and 2) use delays so you don’t perform the keystrokes too fast. Focus on those 2 things. Put a delay before every line. Use display dialog boxes to make sure the script is actually running when you use the keyboard shortcut. Get the frontmost application from system events and display that while the script is running.

When you display the dialogs, make sure you do it this way to keep entourage frontmost…
tell app “microsoft entourage”
activate
display dialog …
end tell

The script run from a keyboard shortcut starts off OK - it pulls up the Entourage move menu and highlights the correct folder, but stalls when the “return” key should be selecting the highlighted “move” option on the menu. Extremely odd that the same series of commands works just fine from the ScriptEditor. Will fiddle around with delays and dialogues and see what happens, thanks.

I am having the same problem with a completely different implementation, based on a handler I found elsewhere - this also works just fine when run from ScriptEditor but won’t run from the menu or a keyboard shortcut.

tell application "Microsoft Entourage" to activate
menu_click({"Microsoft Entourage", "Message", "Move To", "Misc (Exchange)"})

-- `menu_click`, by Jacob Rus, September 2006
-- 
-- Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`
-- Execute the specified menu item.  In this case, assuming the Finder 
-- is the active application, arranging the frontmost folder by date.

on menu_click(mList)
	local appName, topMenu, r
	
	-- Validate our input
	if mList's length < 3 then error "Menu list is not long enough"
	
	-- Set these variables for clarity and brevity later on
	set {appName, topMenu} to (items 1 through 2 of mList)
	set r to (items 3 through (mList's length) of mList)
	
	-- This overly-long line calls the menu_recurse function with
	-- two arguments: r, and a reference to the top-level menu
	tell application "System Events" to my menu_click_recurse(r, ((process appName)'s ¬
		(menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menu_click

on menu_click_recurse(mList, parentObject)
	local f, r
	
	-- `f` = first item, `r` = rest of items
	set f to item 1 of mList
	if mList's length > 1 then set r to (items 2 through (mList's length) of mList)
	
	-- either actually click the menu item, or recurse again
	tell application "System Events"
		if mList's length is 1 then
			click parentObject's menu item f
		else
			my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
		end if
	end tell
end menu_click_recurse


I am guessing this has something to do with accessing multiple menus, because a simpler script doing a compound keystroke to access a top-level menu works just fine from the menu and as a shortcut as well as from ScriptEditor.

Oh well. Thanks again for the suggestions.