图文并茂教你用iphone发短信控制自己的mac锁屏、关机等操作(更新)

时隔一年来更新一下,解决一些问题,附最新代码,在文章最后。

前言

首先,不知道大家有没有发现,在mac的信息里
这里写图片描述
重要的前提
这里写图片描述
也就是说,在”信息”里有脚本处理程序,里面已经有了一些系统预制的一些脚本,当然,都是AppleScript编写的,但是看起来貌似都能看懂,现在要做的就是自己写脚本,然后在这里设置,比如上图里的脚本wjp.applescript就是我自己的。

先实现功能

1,新建一个文本文件,名称随意,后缀为applescript。
2,将下边的代码复制到上边新建的文本文件中,将代码中你的账号替换成你的账号即可,然后保存。代码逻辑很简单,判断+执行。(主要代码已更新,解决无错误报警导致失效的问题)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using terms from application "Messages"
on message received theMessage from theBuddy for theChat
set serviceBuddy to name of service of theBuddy
-- 检查发送者是否是我自己
if serviceBuddy = "你的账号" then
if theMessage = "锁屏" then
send "已接收" to theChat
tell application "Terminal"
do shell script "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -suspend"
end tell
else if theMessage = "睡眠" then
send "已接收" to theChat
tell application "Finder"
sleep
end tell
else if theMessage = "关机" then
send "已接收" to theChat
tell application "Finder"
shut down
end tell
else if theMessage = "重启" then
send "已接收" to theChat
tell application "Finder"
restart
end tell
else if theMessage = "注销" then
send "已接收" to theChat
tell application "System Events"
log out
end tell
end if
else
display dialog "不是自己账号" & serviceBuddy
end if

end message received

end using terms from

3.点击下图标注位置,打开存放脚本的文件夹,然后把刚刚的脚本放进去,然后再回来这个页面选中刚刚添加进去的脚本,就大功告成了
这里写图片描述

注意

  1. “信息”要处于打开状态,可以设置开机自启动”信息”;
  2. 代码中对应的汉字命令,大家可以随意改成自己喜欢的字符串;
  3. 功能还可以扩展很多很多,因为可以调用终端执行shell script,大家自己去探索喽;
  4. 本人写applescript的经验也不多,就业余爱好,如有错误,非常非常的欢迎指正,感谢,最新代码
显示 Gitment 评论