2012-02-09 69 views
3

我有一个AppleScript for Mail.app它打开一个新的消息窗口与预定义的收件人地址和主题。该脚本将打开一个新的窗口,我每次运行时间:Applescript用于创建邮件应用程序的新邮件

tell application "Mail" 
    set newMessage to make new outgoing message with properties {subject:"some subject", content:"" & return & return} 
    tell newMessage 
     set visible to true 
     make new to recipient at end of to recipients with properties {name:"some name", address:"some address"} 
    end tell 
    activate 
end tell 

不过我希望脚本打开新邮件窗口,只有当被关闭之前打开的窗口 - 否则,先前打开的窗口应该到前面来。

任何人都可以请帮我修改这个脚本来实现上述功能吗?

回答

2

我没有测试这个,但它应该做你需要的东西......至少它会告诉你正确的方法。你基本上使用一个“属性”来跟踪上次脚本运行时的某个值。在这种情况下,我们检查最前面的窗口的名称,看它是否符合您的标准。如果窗口名称不能满足您的需求,只需在脚本启动之间查找其他值即可。基本的方法应该工作。

编辑:使用的消息,这是唯一的ID,下面会做你想要什么:

property lastWindowID : missing value 
tell application "Mail" 
    set windowIDs to id of windows 
    if windowIDs does not contain lastWindowID then 
     set newMessage to make new outgoing message with properties {subject:"some subject", content:"" & return & return} 
     tell newMessage 
      set visible to true 
      make new to recipient at end of to recipients with properties {name:"some name", address:"some address"} 
     end tell 
     activate 
     set lastWindowID to id of window 1 
    else 
     tell window id lastWindowID 
      set visible to false 
      set visible to true 
     end tell 
     activate 
    end if 
end tell 

能见度切换似乎得到前面的窗口的唯一途径,因为frontmost是只读属性。只要脚本不被重新编译,lastWindowID属性将存储该ID(注意empteor:不要将其放入Automator服务中,因为每次加载服务时都会重新编译它们)。

+0

感谢您的建议。仅当其他邮件窗口未打开时才能正常工作。如果打开其他窗口,则返回窗口1名称的值将是错误的。 – 2012-02-13 09:54:52

+0

正如我在我的回答中提到的,如果窗口名称没有这样做,则使用其他属性。一个窗口有很多属性,所以你需要找到一些独特的东西。 – regulus6633 2012-02-13 18:50:48

+0

@ regulus6633:实际上,'window 1'将总是返回消息窗口,因为这是创建时的活动窗口 - 原始脚本创建的复制新消息不是因为这个原因,而是因为它测试的第二个条件('窗口1是lastWindowName'会导致在窗口打开时创建一条新消息)。我编辑它,切换到测试窗口ID(唯一,不像窗口名称),并添加了一个'else'子句,以打开窗口前面。 – kopischke 2012-02-27 00:51:26

相关问题