0

I'm trying to write an applescript which will insert some predefined text at the beginning of a message. This is what I currently have:

set msgClass to (choose from list {"Green", "Blue", "Purple"} with title "Choose:")
if result is false then
    stop
else
    set msgClasstxt to the result
    set msgClasstxt to "Classification: " & msgClasstxt

    tell application "System Events"
        key code 126 using {command down}
        keystroke return
        keystroke return
        key code 126 using {command down}
    end tell
tell application "Microsoft Outlook" to set selection to msgClasstxt
end if

I'm sure there's a better way to do this, but the intent is as follows:

  • Go home w/CMD+Up
  • Create two empty lines
  • Go home again
  • Insert text

My problem is that the text is being inserted BEFORE the keystrokes are performed. Vexing. Can anyone help?

jpdyson
  • 43
  • 1
  • 5

2 Answers2

0

Keystrokes and other gui tasks are input to the frontmost application. As such you should always activate the application you want targeted just before performing these actions. Thus I advise you put the following just before your system events code. Even if you think the application is frontmost you should do this anyway just to be certain.

tell application "Microsoft Outlook" to activate
delay 0.2

Also, as suggested in other comments you need short delays between each line of gui code to ensure the computer has time to physically perform the code.

So use delays and activate the application. That should help you.

regulus6633
  • 18,848
  • 5
  • 41
  • 49
  • I'll need to work out how to have Outlook activate to the correct window; this is not trivial, as I cannot assume there is only a main window and a message window open. – jpdyson Jan 30 '15 at 15:21
  • Additionally, this did not solve the actions being performed out of sync, by itself. I posted how I "fixed" it. – jpdyson Jan 30 '15 at 18:32
0

So, this is what I've done: -Added a provision to make sure I'm dealing with the currently-active message window -Activate that window -Done all actions via system events

tell application "Microsoft Outlook" to get the id of the first window
set currentWindow to the result

set msgClass to (choose from list {"Green", "Blue", "Purple"} with title "Choose:")

if the result is false then
    stop
else
    set msgClasstxt to "Classification: " & the result
    tell application "Microsoft Outlook"
        activate window currentWindow
        tell application "System Events"
            key code 126 using {command down}
            keystroke return
            keystroke return
            key code 126 using {command down}
            keystroke msgClasstxt
        end tell
    end tell
end if

That first line works because Outlook lists the frontmost window first. This does what I want for now.

jpdyson
  • 43
  • 1
  • 5
  • 1
    Great. I'm glad you found a solution that works. Microsoft applications in particular are difficult because they don't always do things the "Apple" way so it makes sense that you have to activate the window ID rather than just the application. One other tip. There's no reason to tell Outlook to tell System Events to do something. That's basically what you're doing by embedding one tell statement in another. It's good practice to separate your tell statements so commands don't conflict with each other. Just put the "end tell" of the outlook statement above the system events code. Good luck. – regulus6633 Jan 30 '15 at 19:07
  • ^Indeed; that's carry-over from when I was trying to go back and forth a couple times. Good tip. – jpdyson Jan 30 '15 at 20:04