首页
学习
活动
专区
工具
TVP
发布

如何自动开启微信通知?

如何自动开启微信通知?

背景

因为Mac上开了微信,手机上可以设置不提示,但我暂时离开的时候总是忘记开启微信通知。

购买了米动手表就是为了避免这样的事情发生,但是偶尔忘记打开手机上的通知会错过重要的事情。

当然,我已经将不重要的通知全部设置为消息免打扰了。

需求

因此需要在我离开笔记本之前设置手机开启通知,在我回到笔记本旁边后关闭手机通知。

分析

整个过程是如何呢?

我离开电脑,合盖/锁定;

微信进入手机通知状态(退出);

我来到电脑前,打开/解锁;

微信进入手机静音状态(登录);

那么关键的步骤就在于:

如何判断电脑锁定解锁/睡眠唤醒了呢?

如何关闭微信或者退出微信?

如何启动微信或者登录微信?

如何关闭/启动程序

查了下,下面这条命令可以通知Chrome正常关闭。

osascript-e'tell app "/Applications/WeChat.app" to quit'

而下面这条命令可以启动Chrome。

open"/Applications/WeChat.app"

如何判断电脑睡眠

至于如何在离开电脑和回到电脑前进行操作,在参考5中看到了这么一句话:

You can schedule your Macintosh to wake up in the Energy Saver preference panel. See the Schedule button.

也就是说在电源管理中,有对应的计划任务可以管理。

在Alfred中输入,打开面板,然而,并没有这个按钮。

在不进行其他程序安装的情况下,貌似这条路堵死了。

如何判断屏幕锁定

上面的内容是在5月31日更新的,今天9月14日,继续。

考虑了一下,实际上我需要在电脑锁屏后就自动转入微信通知,这样手表可以收到具体的通知信息。

那么,怎么来获取到屏幕锁定呢?在stackoverflow上找到了这么一条信息:

#!/usr/bin/python

importQuartz

d=Quartz.CGSessionCopyCurrentDictionary()

ifd.get("CGSSessionScreenIsLocked",):

print'Screen locked.'

else:

print'Screen unocked.'

上面的代码基本上就可以判定了,那么后面的解决方案就很好办了。

解决方案

将下面的内容保存为:

#!/bin/env python

# -*- coding=utf-8 -*-

importsys,os,subprocess

importlogging,logging.handlers

defmain():

importtime

fn=sys._getframe().f_code.co_name

logger.debug(u"%s()."%fn)

whileTrue:

ifis_system_locked():

ifis_app_running(app_name):

control_app(app_name,'stop')

continue

ifnotis_app_running(app_name):

control_app(app_name,'start')

time.sleep(10)

defis_system_locked():

fn=sys._getframe().f_code.co_name

logger.debug(u"%s()."%fn)

importQuartz

system_locked=False

d=Quartz.CGSessionCopyCurrentDictionary()

result=dand\

d.get("CGSSessionScreenIsLocked",) ==and\

d.get("kCGSSessionOnConsoleKey",) ==1

logger.debug(u'%s: CGSessionCopyCurrentDictionary is %s.'%(fn,d))

logger.debug(u'%s: result is %s.'%(fn,result))

ifresult==:system_locked=True

logger.debug(u'%s: system is %slocked.'%(fn,

''ifsystem_lockedelse'not '))

returnsystem_locked

defis_app_running(app_name=None):

fn=sys._getframe().f_code.co_name

logger.debug(u"%s()."%fn)

importcommands

ifapp_name:

output=commands.getoutput('pgrep %s'%app_name)

ifoutput:

logger.debug(u'%s: app %s is running.'%(fn,app_name))

returnTrue

else:

logger.debug(u'%s: app %s is not running.'%(fn,app_name))

returnFalse

else:

logger.error(u'%s: params is not set.'%fn)

defcontrol_app(app_name=None,action=None):

fn=sys._getframe().f_code.co_name

logger.debug(u"%s()."%fn)

ifapp_nameandaction:

ifaction=='start':

ifapp_name=='WeChat':

p=subprocess.Popen(

['osascript','%s%swechat_login.scpt'%(os.getcwd(),os.sep)],

stdout=subprocess.PIPE)

else:

p=subprocess.Popen(

['osascript''-e','tell "%s" to activate'%app_path],

stdout=subprocess.PIPE)

logger.debug(u'%s: %s started.'%(fn,app_name))

ifaction=='stop':

p=subprocess.Popen(

['osascript','-e','tell application "%s" to quit'%app_path]

,stdout=subprocess.PIPE)

logger.debug(u'%s: wechat stopped.'%fn)

else:

logger.error(u'%s: params is not set.'%fn)

definit_log(log_name='custom',log_level='info',log_files=3):

'''

Initialize the logger.

'''

log_file=u"%s%s%s.log"%(os.getcwd(),os.sep,log_name)

logger=logging.getLogger(log_name)

logger.setLevel(log_level.upper())

handler=logging.handlers.RotatingFileHandler(log_file,\

maxBytes=1024*1024*50,backupCount=log_files)

fmt='%(asctime)s %(levelname)-6s %(filename)s:%(lineno)s - '\

+'%(message)s'

formatter=logging.Formatter(fmt)

handler.setFormatter(formatter)

logger.addHandler(handler)

returnlogger

prog_name='wechat_auto'

log_level='debug'

logger=init_log(prog_name,log_level)

app_name='WeChat'

app_path='/Applications/WeChat.app'

main()

因为微信启动时需要登录,为了方便,我们借助的强大功能,去帮助我们点击这个按钮。

tellapplication"WeChat"toactivate

delay1

tellapplication"System Events"

tellapplicationprocess"WeChat"

clickbutton"登录"ofwindow"Window"

endtell

endtell

随后在中执行下述命令:

python wechat_auto.py &

如果执行正常的话,脚本中的就可以修改为'info'了。

其他

在使用过程中遇到了两个问题,解决如下,其实是很简单的问题。

无法打开指定文件

/usr/bin/python: can't open file './system_locked.py': [Errno 2] No such file or directory

解决很简单,就是在脚本中使用绝对路径就好了。

微信报错

昨天在使用上述命令进行微信的启动和关闭测试时,会报一个错,然后微信关闭。

但是在后续测试中无法重现,因此这里只是做一个记录。

无法获取CGSessionCopyCurrentDictionary

因为当前进程没有对应的,比如我们通过执行就会出现这种问题。

参考

How can I quit the application opened from terminal in Mac OS X?

Possible to run scripts on sleep and wake?

How to Run an AppleScript On Wake Up

Open an application after wake from sleep

OSX: check if the screen is locked

How to check in Mac OSX with Python if a process is RUNNING or not

python subprocess: how to run an app on OS X?

Automating the User Interface

手把手教你用 AppleScript 模拟鼠标键盘操作,实现 macOS 系统的自动化操作

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20180922G1GKFA00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券