AppleScript 實現短信自動轉發


背景:這段時間某位同事需要頻繁登錄我的賬號,並且要驗證手機驗證碼,每次都需要我手動將驗證碼轉發給他,覺得非常的麻煩,便想看一下有沒有什么能夠解放雙手的辦法,然后發現了AppleScript這么一個非常好用的東西 :)

所需配置

  • Mac 電腦:既然是使用AppleScript,那么一台MAC電腦自然是不可少的(macOS Catalina 10.15.5)
  • iPhone:如果消息類型是iMessage的話,則只需要MAC上登錄蘋果賬號就可以,如果消息類型是SMS的,則需要利用蘋果的生態,iphone 上的消息能夠自動轉發到MAC上(IOS 8 以后支持)。

AppleScript

什么是AppleScript?

AppleScript是用來編寫運行於mac的腳本的,並且能夠操作應用程序。蘋果官方也要求開發mac上的軟件需要留出能夠共AppleScript能夠操作的方法。利用AppleScript我們能非常方便實現一些平常工作中重復工作的腳本化,提升工作效率,避免重復勞動。

基礎語法

AppleScript 的語法非常接近自然語言,幾乎沒有標點符號,語法不像其他語言那樣嚴格

變量定義

-- 將百度網址賦值給變量url_str
set url_str to "https://www.baidu.com"

通知

-- 將百度網址賦值給變量url_str
set url_str to "https://www.baidu.com"
-- 通知
display notification "url set success"

操作應用

-- 將百度網址賦值給變量url_str
set url_str to "https://www.baidu.com"
-- 將bing網址賦值給變量url_str2
set url_str2 to "https://www.bing.com"
-- 通知
display notification "url set success"
-- 操作應用
tell application "Google Chrome"
	-- 創建新窗口
	set newWin to make new window
	-- 獲取新窗口tab
	set theTab to get active tab of newWin
	-- 操作tab
	tell theTab
		-- 設置tabURL
		set URL of theTab to url_str
		-- 訪問
		go forward
		-- 延遲3秒
		delay 3
		--重新設置tabURL
		set URL of theTab to url_str2
		go forward
	end tell
end tell

字典

蘋果官方會要求mac軟件的開發提供能夠操作應用軟件的方法,可以通過字典查詢能否由AppleScript操控軟件

字典打開方式

Script Editer -> 打開詞典 -> 選擇想要操作的軟件

"open_dict.png"

chrome dictionary.
"chrome_dict"

AppleScript Language Guide

了解 AppleScript 的基礎語法是掌握 AppleScript 自動化的第一步,在對 AppleScript 有了一定的了解后,在日后發現自己有重復操作時,就可以先想想可以如何通過 AppleScript 解決它

問題解決

回到正題,因為對AppleScript 有過一定了解,並且也做過簡單的測試,所以第一時間想到的就是能不能用自動話腳本來處理重復的短信轉發工作(在這之前還從沒用AppleScript解決過現實問題)

發送短信

首先需要的就是打開字典,因為Message本身就是蘋果開發的,所以肯定會有豐富的API供我們使用的

"message_dict_send.png"

通過翻找,我們能夠基本定位我們發送短信需要用到的send方法,send方法中還需要我們明確一個buddy,繼續查看buddy

"message_dict_buddy.png"

查看我們的buddy,他是包含在一個service里面的,繼續service ...

"message_dict_service.png"

service 上層為application包含,因此我們可以書寫我們的腳本了

tell application "Messages"
	send "test" to buddy "+86199xxxx" of service "SMS"
end tell

非常簡單的一段代碼,可以測試發送了。在測試的過程中發現需要有和該號碼發過消息的記錄(即需要打開有一個窗口)

"message_send_test.png"

事件監聽

在能夠成功發送短信后,我們距離解放雙手已經很近了,接下來我們只需要在接收到驗證碼信息的時候,調用我們的發送腳本將我們收到的消息轉發出去就大功告成了:)

繼續查看我們的字典,有一個message received 事件,看字面上的意思就能滿足我們的需要,就是他了

"message_dict_message_received.png"

很簡單,我們只需要獲取到我們需要的消息,以及發送給我們的人用來判斷(畢竟不能把所有的消息都轉發出去)

using terms from application "Messages"
	on message received theMessage from theBuddy
		-- 通知接收到消息
		display notification "message"
		-- 將Message App可見
		set visible of window "Messages" to true
		-- 將Message App置頂
		set frontmost of window "Message" to true
	end message received
end using terms from

事情到這里就結束了嘛?哪有那么簡單,正所謂“天之道,沒那么容易讓你成功~”

經過不斷的測試,發現這個腳本根本就沒達到我的預期,沒有通知也沒有置頂,是我哪里寫的有問題嘛?好嘛,知錯要改,要虛心學習。百度~,嗯,查不到,害,谷歌~(個人習慣,先百度,當發現查不到想要的信息或者百度上全是同一篇文章抄來抄去的時候才谷歌,畢竟谷歌是不被允許,我可是乖孩子)

不斷的變換關鍵詞及描述,用我的散裝英語終於找到了一些解決辦法
發現我的腳本寫的是沒有問題的,只是還少一些操作步驟。例如

copy the script into a new AppleScript Editor document and save it in ~/Library/Application Scripts/com.apple.iChat/

Then activate it by selecting it in Messages -> Preferences -> General -> AppleScript Handler:

以及

AppleScript file copied to ~/Library/Scripts/Messages by Messages, it started to work.

不同的路徑,但是好像都需要拷貝,於是分別考到目錄下,但是一直沒有找到Message -> Preference —> General(發現我的MAC好像和廣大網友不太一樣啊,買到了假貨了吧)總之還是不成功,繼續google...

終於,原來Message.app Message Received Event HandlermacOS High Sierra 10.13.4版本被移除,並且開通了Business Chat emmmmmmm.....,害~

分析數據庫

事件的方式走不通了,只能換一種方式了。還可以用什么方式呢,於是我想到了一種可能,我的這些消息,哪怕時間再久遠都能查到,他是怎么存放的呢。
OK,有思路就有方向,最后不斷的搜索發現是存在目錄/Users/herbert/Library/Messages下 的chat.db文件中

用sqlite打開看一下

sqlite3 /Users/herbert/Library/Messages/chat.db

"message_sqlite_tables.png"

查看表結構及表內部分數據

.schema message
select * from message order by ROWID desc limit 5;

最終根據梳理的表結構關系,及傳入我需要查詢的號碼整理SQL

-- 查詢一分鍾內來自指定號碼xxxxxx_number的短信
SELECT text, handle_id,date,datetime(date/1000000000 + strftime('%s','2001-01-01'), 'unixepoch', 'localtime') as date_utc FROM message T1 INNER JOIN chat_message_join T2 ON T1.ROWID=T2.message_id INNER JOIN chat T3 ON T2.chat_id=T3.ROWID AND T3.chat_identifier = 'xxxxxx_number' where datetime(date/1000000000 + strftime('%s','2001-01-01'), 'unixepoch', 'localtime') >  datetime('now','localtime','-1 minute') ORDER BY T1.date;

在查詢數據的時候還發現了一個不常見的日期存儲(我之前沒有見過)一個18位的時間數字
"message_sqlite_date.png"
處理方式:
datetime(date/1000000000 + 978307200,'unixepoch','localtime').
datetime(date/1000000000+ strftime('%s','2001-01-01'),'unixepoch','localtime')

編寫腳本

我們已經拿到我們需要的數據了,接下來就只需要每個一分鍾取一次數據,如果能夠去到數據,那么調用我們發送短信的腳本就OK了

編寫腳本auto_forward.sh

 #/bin/sh
phone="+86xxxx"
call_apple()
{
  	while read line
	do
		osascript /Users/herbert/Documents/AppleScript/ForwardMessage/SendMessage.scpt $line $phone
	done
}

export -f call_apple

sqlite3 /Users/herbert/Library/Messages/chat.db '.read /Users/herbert/Documents/AppleScript/ForwardMessage/select.txt' | call_apple

將我們的sql存在單獨的文件里,方便修改select.txt

SELECT text FROM message T1 INNER JOIN chat_message_join T2 ON T1.ROWID=T2.message_id INNER JOIN chat T3 ON T2.chat_id=T3.ROWID AND T3.chat_identifier = 'from_number' where datetime(date/1000000000 + strftime('%s','2001-01-01'), 'unixepoch', 'localtime') >  datetime('now','localtime','-1 minute') ORDER BY T1.date;

SendMessage.scpt

on run argv
	set msgContent to item 1 of argv
	set phone to item 2 of argv
	sendMsg(msgContent, phone)
end run

on sendMsg(msgContent, phone)
	tell application "Messages"
		send msgContent to buddy phone of service "SMS"
	end tell
end sendMsg

萬事具備,只需要加入crontab中就行了

crontab -e

*/1 * * * * sh /Users/herbert/Documents/AppleScript/ForwardMessage/auto_forward.sh

此處在測試的時候還是沒有達到預期,但是單獨執行腳本的時候能夠成功,推測是cron的問題,於是查看日志
vim /var/mail/herbert
果然發現問題
sh: /Users/herbert/Documents/test/demo.sh: Operation not permitted

此問題會在Mojave 10.14, Catalina 10.15  以及后續版本出現
解決方法:系統偏好設置 -> 安全性與隱私 -> 隱私 -> 完全磁盤訪問權限 -> 添加cron


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM