Applescript 在邮件开头插入文本

Applescript 在邮件开头插入文本,applescript,outlook-2011,Applescript,Outlook 2011,我正在尝试编写一个applescript,它将在消息开头插入一些预定义的文本。这就是我目前拥有的: 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: " &a

我正在尝试编写一个applescript,它将在消息开头插入一些预定义的文本。这就是我目前拥有的:

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
我相信有更好的方法可以做到这一点,但目的如下:

带CMD+向上回家 创建两个空行 再回家 插入文本
我的问题是,文本是在执行击键之前插入的。烦人。有人能帮忙吗?

击键和其他gui任务被输入到最前端的应用程序中。因此,您应该始终在执行这些操作之前激活您想要的目标应用程序。因此,我建议您将以下内容放在系统事件代码之前。即使您认为应用程序是最前端的,您也应该这样做,只是为了确定

tell application "Microsoft Outlook" to activate
delay 0.2
此外,正如其他评论中所建议的,在每行gui代码之间需要短暂的延迟,以确保计算机有时间实际执行代码


因此,使用延迟并激活应用程序。这应该会对你有所帮助。

所以,这就是我所做的: -添加了一个设置,以确保我正在处理当前活动的消息窗口 -激活那个窗口 -通过系统事件完成所有操作

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

第一行之所以有效,是因为Outlook首先列出了最前面的窗口。这正是我现在想要的。

顺便问一下,您使用的是哪个OS X版本?在10.10.2上测试。尝试添加延迟,在所述延迟之后,插入文本发生,然后所有系统事件发生;这并不是一件小事,因为我不能假设只有一个主窗口和一个消息窗口打开。此外,这本身并没有解决正在执行的不同步操作。我发布了我是如何修复它的。太好了。我很高兴你找到了一个有效的解决方案。微软的应用程序尤其困难,因为它们并不总是以苹果的方式工作,所以你必须激活窗口ID,而不仅仅是应用程序。另一个提示。没有理由让Outlook告诉系统事件执行某些操作。这基本上就是通过将一个tell语句嵌入另一个tell语句来实现的。最好将tell语句分开,这样命令就不会相互冲突。只需将outlook语句的结尾置于系统事件代码之上。祝你好运。^真的;这是我几次来回尝试时遗留下来的。好提示。