Mac Python实用工具开发记录

1. Python在Mac上弹出消息提示
  1. 首先需要知道Mac上面弹出消息的方式:

    • 调用通知中心弹出消息
      osascript -e 'display notification "内容" with title "标题"'
    • 调用某应用弹出消息
      osascript -e 'tell app "Mail" to display dialog "该发周报了!" with title "提示"
      其中Mail可以改为对应的app名称(非Launchpad上显示的名称,而是 ‘xxx.app’中’.app’前面的名称)
  2. 然后在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 表示服务可能出现问题,需要检查是否正确

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值