1. Python在Mac上弹出消息提示
首先需要知道Mac上面弹出消息的方式:
- 调用通知中心弹出消息
osascript -e 'display notification "内容" with title "标题"'
- 调用某应用弹出消息
osascript -e 'tell app "Mail" to display dialog "该发周报了!" with title "提示"
其中Mail可以改为对应的app名称(非Launchpad上显示的名称,而是 ‘xxx.app’中’.app’前面的名称)
- 调用通知中心弹出消息
然后在python中使用如下命令便可弹出提示
import os
os.system("""<osascript command>""")
2.在Mac上定时执行.py文件
这里需要用的是 ‘launchctl’
launchctl help
cd ~/Library/LaunchAgents
sudo touch com.example.hello.plist
sudo chmod 777 com.tim.shadow.autoruner.plist
在 com.tim.shadow.autoruner.plist 输入如下内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>StartCalendarInterval</key>
<dict>
<key>Minute</key>
<integer>30</integer>
<key>Hour</key>
<integer>16</integer>
</dict>
<key>ProgramArguments</key>
<array>
<string>/YourPath/Example.py</string>
</array>
<key>KeepAlive</key>
<false/>
<key>Label</key>
<string>com.example.hello.plist</string>
<key>StandardOutPath</key>
<string>/YourLogPath/examplelog.log</string>
<key>StandardErrorPath</key>
<string>/YourLogPath/exampleerrlog.log</string>
</dict>
</plist>
这里的内容是说,在每天的16:40时执行/YourPath/Example.py
文件,并将日志输入到/YourLogPath/examplelog.log
将错误日志输出到/YourLogPath/exampleerrlog.log
plist的日志文件必须为可以读写,否则会出现错误,导致python 脚本不能正确执行
plist详细的配置说明请参考官方说明文档
plist官方说明文档请点击
加载服务
launchctl load com.example.hello.plist
launchctl start com.example.hello.plist
停止服务可以使用:
launchctl stop com.example.hello.plist
launchctl unload com.example.hello.plist
查看当前服务状态:
launchctl list | grep com.example.hello.plist
若Status为0 Pid为 ‘-‘表示服务已经成功启动,但是暂未运行
若Status为0 Pid为数字 表示服务已经启动,正在运行
若Status为非0 表示服务可能出现问题,需要检查是否正确