2017-08-05 57 views
2

我無法通過使用Applescript獲取文本消息聊天的Id。獲取郵件標識

這是到目前爲止我的代碼:

using terms from application "Messages" 

    on message sent theMessage for theChat 
     display dialog (get id of theChat) 
    end message sent 

end using terms from 

當我使用AppleScript的處理程序,我得到一個錯誤:

Can’t get id of «class ictt» id "iMessage;-;+1xxxxxxxxxx". (-1728) 

(X的代替電話號碼)

我如何避免這個錯誤,並得到所需的輸出只是iMessage;-;+1xxxxxxxxxx

回答

0

我不確定是什麼觸發了這個錯誤,但我找到了可靠的解決方法。我把這個語句放在一個try塊中,並從錯誤信息中解析出id。

on splitText(sourceText, textDelimiter) 
    set AppleScript's text item delimiters to {textDelimiter} 
    set messageParts to (every text item in sourceText) as list 
    set AppleScript's text item delimiters to "" 
    return messageParts 
end splitText 

try 
    set myresult to get id of theChat 
on error errMsg 
    set errMsgParts to splitText(errMsg, "\"") 
    set errCount to count of errMsgParts 
    set myresult to item (errCount - 1) of errMsgParts 
end try 

這個工作原理是通過解析文本從引號中僅取出id。分割文本功能取自here