Applescript用于使用Mail创建新消息应用程序

Ran*_*r G 5 applescript

我有一个AppleScript for Mail.app,它将打开一个新的消息窗口,其中包含预定义的收件人地址和主题。每次我运行该脚本都会打开一个新窗口:

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
Run Code Online (Sandbox Code Playgroud)

但是我希望脚本仅在关闭先前打开的窗口时才打开新的消息窗口–否则,先前打开的窗口应位于最前面。

谁能帮我修改此脚本以实现上述功能?

reg*_*633 4

我没有对此进行测试,但它应该可以满足您的需要......至少它向您展示了正确的方法。您基本上使用“属性”来跟踪上次运行脚本时的某些值。在本例中,我们检查最前面窗口的名称,看看它是否符合您的条件。如果窗口名称不能满足您的需要,那么只需找到一些其他值来跟踪脚本的启动之间的情况即可。基本方法应该有效。

编辑:使用唯一的消息 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
Run Code Online (Sandbox Code Playgroud)

可见性切换似乎是使窗口位于前面的唯一方法,frontmost只读属性也是如此。只要脚本不重新编译,lastWindowID 属性就会存储 ID(买者自负:不要将其放入 Automator 服务中,因为每次加载服务这些都会重新编译)。