gpt4 book ai didi

macos - 使用 AppleScript 阅读 iMessage

转载 作者:行者123 更新时间:2023-12-03 23:55:49 25 4
gpt4 key购买 nike

我正在尝试编写一个脚本,该脚本将遍历消息应用程序中的所有聊天记录 - 我的目标是找到我尚未回复的所有消息并向我发送提醒。

但我被困在第一个 - 我可以看到我有多少消息:

tell application "Messages" to log (count of chats)

但是我什至无法从聊天中获得简单的属性;例如:
tell application "Messages" to set x to started of first chat

给出错误:无法开始聊天 1。” 从聊天 1 开始的数字 -1728

任何想法我哪里出错了......?

最佳答案

我用过你的 sqlite3代码进行了一些小的调整,然后创建了这个 AppleScript,它会生成一个提醒列表(在提醒应用程序中)。唯一的缺点是发件人显示为电话号码或 iMessage 地址,并且不会使用您的地址簿“转换”为姓名。这可能可以通过一些 tell 来实现命令联系人并搜索电话号码,但目前这超出了这项特殊努力的范围。

    -- Run an SQL query on the messages database via shell script
do shell script (["sqlite3 -line ~/Library/Messages/chat.db ", ¬
"'SELECT MAX(date) lastMessageDate, h.id, text ", ¬
"FROM message m INNER JOIN handle h ON h.ROWID=m.handle_id ", ¬
"WHERE is_from_me = 0 GROUP BY h.ROWID' | egrep -io -e '\\w+ = .+'"] as text)

set query_response to result
--> lastMessageDate = %timestamp%
--> id = %phone number or iMessage address%
--> text = %message content%
--> ...

-- Groups all messages into a single item of an array
-- Each occurence of "lastMessageDate = " indicates
-- a new message and, hence, a new array item.
set AppleScript's text item delimiters to "lastMessageDate = "
set query_response to rest of text items of query_response

-- Loop through the array organise each message
repeat with message in query_response

-- Split the array item up into its components
set [receivedOn, sentFrom, textContent] ¬
to [paragraph 1, ¬
text 6 thru -1 of paragraph 2, ¬
text 8 thru -1 of paragraph 3] of message

-- Convert ReceivedOn to a value representing
-- the number of days since 01/01/2001
set receivedOn to receivedOn / (1.0E+9 * 86400)

-- Convert ReceivedOn again to the date
-- corresponding to the number of days since
-- 01/01/2001, i.e. the date and time the message
-- was sent
set receivedOn to (date "Monday, 1 January 2001 at 00:00:00") + receivedOn * days

-- Make a reminder
tell application "Reminders"
if not (exists (list named "Awaiting My Reply")) then ¬
make new list with properties {name:"Awaiting My Reply"}

tell list named "Awaiting My Reply"
-- This creates a reminder with a past due date
-- meaning you can sort the reminder list by due
-- date to see who has been waiting longest
-- for you to reply
make new reminder with properties ¬
{name:"Reply To " & sentFrom, body:¬
textContent, due date:¬
receivedOn} ¬

end tell
end tell
end repeat
当然,如果您的任何文本消息碰巧包含短语“lastMessagedate =”,那么这将不适用于上面的脚本。但是,对此有一些解决方法,我尝试了 sqlite3 返回的各种格式。包括list、column、csv,各有优缺点。
附录:从匹配的电话号码中检索联系方式
经过多次试验,已设法将上述脚本与地址簿搜索集成,以将电话号码替换为联系人姓名:
    -- Replace dbID with your address book's serial number
-- (look in ~/Library/Application Support/AddressBook/Sources)
property dbID : "377FDA5C-013A-430A-A964-9943C0B40137"
property db : {Messages:"/Users/CK/Library/Messages/chat.db", Addresses:"/Users/CK/Library/Application Support/AddressBook/Sources/" & dbID & "/AddressBook-v22.abcddb"}

-- Run an SQL query on the messages database via shell script
set Messages to do shell script (["sqlite3 -line ", ¬
quoted form of (Messages in db) as text, ¬
" 'SELECT MAX(date) lastMessageDate, h.id, text", ¬
" FROM message m INNER JOIN handle h ON h.ROWID=m.handle_id", ¬
" WHERE is_from_me = 0 GROUP BY h.ROWID'", ¬
" | egrep -io -e '\\w+ = .+'"] as text)
--> lastMessageDate = %timestamp%
--> id = %phone number or iMessage address%
--> text = %message content%
--> ...

set PhoneNumbers to do shell script (["sqlite3 -list -separator : ", ¬
quoted form of (Addresses in db) as text, ¬
" 'select r.ZFIRSTNAME, r.ZLASTNAME, p.ZFULLNUMBER", ¬
" from ZABCDRECORD as r, ZABCDPHONENUMBER as p", ¬
" WHERE p.ZOWNER=r.Z_PK'", ¬
" | tr -c -d '[:alnum:]:[:cntrl:]:[:space:]:+#*'"] as text)
--> %first name%:%last name:%phone number%
--> ...

set NamesAndNumbers to paragraphs of PhoneNumbers
set AppleScript's text item delimiters to ":"
tell NamesAndNumbers to ¬
repeat with n from 1 to count it
set its item n to ¬
{firstname:text item 1, lastname:text item 2, phone:text item 3} ¬
of its item n
end repeat

-- Groups all messages into a single item of an array
-- Each occurence of "lastMessageDate = " indicates
-- a new message and, hence, a new array item.
set AppleScript's text item delimiters to "lastMessageDate = "
set Messages to rest of text items of Messages

-- Loop through the array organise each message
repeat with message in Messages

-- Split the array item up into its components
set [receivedOn, sentFrom, textContent] ¬
to [paragraph 1, ¬
text 6 thru -1 of paragraph 2, ¬
text 8 thru -1 of paragraph 3] of message

-- Convert ReceivedOn to a value representing
-- the number of days since 01/01/2001
set receivedOn to receivedOn / (1.0E+9 * 86400)

-- Convert ReceivedOn again to the date
-- corresponding to the number of days since
-- 01/01/2001, i.e. the date and time the message
-- was sent
set receivedOn to (date "Monday, 1 January 2001 at 00:00:00") + receivedOn * days

-- Keep the "+" delimiter but replace the "+44" with
-- your own country's intl dialling code, e.g. {"+1", "+"}
-- as this may or may not prefix a phone number
set AppleScript's text item delimiters to {"+44", "+"}
set sentFrom to last text item of sentFrom

-- Find the name to whom the phone number belongs
ignoring white space
set match to ""
repeat with person in NamesAndNumbers
if the person's phone contains sentFrom then
set match to contents of the person
exit repeat
end if
end repeat
end ignoring

set AppleScript's text item delimiters to space
if match is not "" then set sentFrom to the contents of ¬
{firstname, lastname} of match as text

-- Make a reminder
tell application "Reminders"
if not (exists (list named "Awaiting My Reply")) then ¬
make new list with properties {name:"Awaiting My Reply"}

tell list named "Awaiting My Reply"
-- This creates a reminder with a past due date
-- meaning you can sort the reminder list by due
-- date to see who has been waiting longest
-- for you to reply
make new reminder with properties ¬
{name:"Reply To " & sentFrom, body:¬
textContent, due date:¬
receivedOn} ¬

end tell
end tell
end repeat

关于macos - 使用 AppleScript 阅读 iMessage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48086287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com