2014-02-16 21 views
1

我正在AppleScript中構建一個小插件,它將查找默認郵件應用程序並打開並粘貼到主題和正文中。AppleScript - 打開電子郵件程序時預計會結束但找到標識符

我收到錯誤「預計行結束但找到了標識符。」

on run 
    set mailClient to getDefaultMailClient() -- store application id 
    tell application id mailClient 
     set msg to make new outgoing message with properties {subject:"subject here", visible:true} 
     tell msg to make new to recipient with properties {address:"email.com"} 
    end tell 
end run 

on getDefaultMailClient() 
    set prefPath to (path to preferences as text) & "com.apple.LaunchServices.plist" 
    tell application "System Events" 
     try 
      value of property list item "LSHandlerRoleAll" of ¬ 
       (first property list item of property list item "LSHandlers" of ¬ 
        property list file prefPath whose value of property list items ¬ 
        contains "mailto") 
     on error 
      "com.apple.mail" 
     end try 
    end tell 
end getDefaultMailClient 

這工作得很好,當它打開了郵件程序在我的電腦上,但我希望它爲Entourage和OSX的任何其他電子郵件程序工作。

任何幫助,非常感謝。

回答

0

而不是預期不同的客戶端,併爲他們創造特定的代碼,你可以採取mailto: URL方案提供抽象的優勢 - 它會在默認的電子郵件客戶端打開一個新的電子郵件的形式:

on newEmailForm(addr, subj, body) 
    do shell script "open " & ¬ 
    quoted form of ("mailto:" & addr & "?subject=" & subj & "&body=" & body) 
end newEmailForm 

# Sample invocation: 
my newEmailForm("[email protected]", "This is my subject line", "") 

請注意,上述使用do shell scriptopen而不是open location - 前者方便地自動編碼字符串以包含在URI中,而後者需要您傳遞已編碼的字符串。

+0

雖然它可能不適用於jarrod,但我不認爲您可以指定您使用mailto鏈接發送哪個帳戶。你可以嗎? – adayzdone

+0

@adayzdone:你是對的:你不能 - 顯然URL方案本身不允許它: 「Originator字段像From [...]存在於URI中時,務必被忽略。」 - http://www.ietf.org/rfc/rfc6068.txt。接下來最好的事情(可能不足)是使用'reply-to =',儘管這也不是計劃本身的一部分,儘管客戶在實踐中似乎支持它。 – mklement0

0

您可以通過爲每個客戶端編寫命令來完成此操作。

if mailClient = "com.apple.mail" then 
    tell application "Mail" 
     --insert your code 
    end tell 
else if mailClient = "com.something.else" then 
    tell application "something.else" 
     --insert your code 
    end tell 
end if 
相關問題