2012-02-09 67 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

相關問題