V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
find456789
V2EX  ›  分享发现

分享发短信控制 mac 的关机锁屏重启 的代码,顺便求大家改进下,目前无法获取锁屏状态

  •  
  •   find456789 · 2017-12-29 09:37:46 +08:00 · 1659 次点击
    这是一个创建于 2526 天前的主题,其中的信息可能已经有所发展或是发生改变。

    需要 ios+mac, 把代码里的 18988888888 替换为自己的手机号,

    具体可参考 http://blog.csdn.net/u013679882/article/details/61195399,我就是根据这个帖子操作的

    现在有个问题,就是无法获取 是否已经锁屏的状态,目前发送 s 获取的状态似乎不正确,感觉不太踏实,希望牛人可以贴出代码

    using terms from application "Messages"
        on message received theMessage from theBuddy for theChat
            --set serviceBuddy to name of service of theBuddy 求出来的是收件人信息
            set serviceBuddy to handle of theBuddy -- 发件人手机号 +86 189***
            -- 检查发送者是否是我自己
    
    
            if serviceBuddy = "+8618988888888" then
                --display notification "收到指令:" & theMessage & ",发送者:自己" & serviceBuddy
                if theMessage is in {"l", "lock"} then
                    send "Got:" & theMessage to theChat
                    tell application "Terminal"
                        do shell script "pmset displaysleepnow"
                        -- do shell script "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -suspend"
                    end tell
    
                else if theMessage is in {"ln", "locknow"} then
                    send "Got:" & theMessage to theChat
                    tell application "Terminal"
                        do shell script "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -suspend"
                    end tell
    
                else if theMessage is in {"sl", "sleep"} then
                    send "Got:" & theMessage to theChat
                    tell application "Finder"
                        sleep
                    end tell
    
                else if theMessage is in {"sd", "shut down"} then
                    send "Got:" & theMessage to theChat
                    --tell application "Finder"
                    --	shut down
                    --end tell
    
                else if theMessage is in {"r", "restart"} then
                    send "Got:" & theMessage to theChat
                    --tell application "Finder"
                    --	restart
                    --end tell
    
                    --else if theMessage = "log out" then
                    --send " [已忽略-高危] 收到指令:" & theMessage to theChat
                    --tell application "System Events"				
                    --	log out
                    --end tell
    
                else if theMessage is in {"s", "status"} then
                    set runningStatus to "000"
                    tell application "System Events"
                        if (running of screen saver preferences) is true then
                            set runningStatus to "zhen"
                        else if (running of screen saver preferences) is false then
                            set runningStatus to "jia"
                        else
                            set runninnstatues to "fuck"
                        end if
    
                    end tell
    
    
    
                    send "status ok[can not got status]" & runningStatus to theChat
    
                else if theMessage is in {"h", "help"} then
                    send "Welcome!!
    
    l、lock: 锁屏(音乐会继续)
    ln、locknow: 锁屏(音乐停止)
    sl、sleep:睡眠
    sd、shut down:关机[不可用]
    r、restart: 重启[不可用]
    h、help:帮助
    s、status:系统状态			
                    " to theChat
    
                else if theMessage = "1" then
                    send "test ok" to theChat
                end if
            end if
    
        end message received
    
    end using terms from
    
    7 条回复    2017-12-29 13:59:34 +08:00
    xwhxbg
        1
    xwhxbg  
       2017-12-29 09:54:30 +08:00
    呃为啥不做成一个 APP 点按钮控制呢,短信收费还麻烦
    find456789
        2
    find456789  
    OP
       2017-12-29 09:57:59 +08:00
    @xwhxbg imessage 不收费的
    lrannn
        3
    lrannn  
       2017-12-29 10:53:10 +08:00
    如果别人给你发 iMessage 的话,好像会有问题啊。。提示 AppleScript 已经运行很长时间了,LZ 测试过吗
    find456789
        4
    find456789  
    OP
       2017-12-29 12:15:42 +08:00
    @lrannn 我贴出的那段代码,我用了一周了, 没有出现 bug,其他号码的信息不会有动作的
    lrannn
        5
    lrannn  
       2017-12-29 13:01:53 +08:00
    @find456789 好吧。我给加了段代码,控制 QQ 音乐的一些操作,然后配合 iOS 里的 workflow,挺方便的,就是有的时候电脑 iMessage 接受的慢
    123s
        6
    123s  
       2017-12-29 13:13:39 +08:00
    看看
    yuxuan
        7
    yuxuan  
       2017-12-29 13:59:34 +08:00
    mark 一下
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1065 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 20:28 · PVG 04:28 · LAX 12:28 · JFK 15:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.