【问题标题】:Applescript for creating New Message with Mail application用于使用邮件应用程序创建新消息的 Applescript
【发布时间】:2012-02-27 10:05:03
【问题描述】:

我有一个用于 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

但我希望脚本仅在之前打开的窗口关闭时打开一个新的消息窗口——否则,之前打开的窗口应该出现在前面。

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

【问题讨论】:

    标签: applescript


    【解决方案1】:

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

    编辑:使用唯一的消息 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(caveat empteor:不要将其放入 Automator 服务,因为它们会重新编译每次加载服务时)。

    【讨论】:

    • 感谢您的建议。只有在未打开其他邮件窗口时才能正常工作。如果打开其他窗口,则返回的窗口 1 名称的值将是错误的。
    • 正如我在回答中提到的,如果窗口名称不这样做,则使用其他属性。一个窗口有很多属性,所以你需要找到一些独特的东西。
    • @regulus6633:实际上,window 1 将始终返回消息窗口,因为那是创建时的活动窗口——您的原始脚本创建重复的新消息不是因为这个,而是因为第二个它测试的条件(window 1 is lastWindowName 导致在窗口打开时创建一条新消息)。我对其进行了编辑,切换到测试窗口 ID(唯一,与窗口名称不同)并添加了一个 else 子句以将打开的窗口置于前面。
    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多