Gemini Cli实现“选中文字添加日程”的右键功能
前言
近来我在摸索将Gemini Cli等终端agents融入工作流,目前总体感觉是——这类工具确实能够将脑力和精力从“杂活”和“需要但不感兴趣”的事情中解放出来,效率提升很多。这篇短文将介绍一个最近的实践案例。
作为博士生,我经常需要做的一件事是手动将电子邮件中的会议、报告、答辩等信息添加到Apple日历中。手动设置事件的标题、时间、地点、提前提醒时间、线上开会链接等内容属实麻烦。在ChatGPT出圈后,我一直想要开发一个功能——选中文字后直接在右键菜单中点击“添加日程”,程序就能够调用LLM去解析文字,生成Apple日历事件并导入到日历中。
然而,这个想法在脑海中,一直也没有去实现。最近,需要添加的事件尤其多,今晚吃饭回来后又想到了这一茬儿,于是打开Gemini Cli,给出需求,让它设计方案并实现,十分顺利。
现在我只需要在邮件中选中文字,点击右键,然后选择“添加日程”:
稍等片刻,就能在日历中看到对应的事项了:
代码仓库:https://github.com/brant-ruan/text2agenda
实践
为了提高成功率,我在Gemini Cli中将模型设定为gemini-3-pro-preview。初始需求描述如下:
我想要在当前目录下开发一个软件或者借助Mac上已有的automator等,它的功能是给我的macbook添加一个右键菜单“添加日程”,
使用场景是选中一段文字后,比如在Outlook中选中一个关于会议日程信息通知邮件的文字部分,右键,选择“添加日程”,
然后自动调用OpenAI/DeepSeek/Gemini等大模型的API,将其解析成.ics(苹果日程格式),然后自动导入到我的系统日历中。
这个项目允许用户通过配置文件配置API KEY等配置项。
很快,Gemini给出了实现方案:
I will design a solution using a Python script as the core logic engine (handling the API call and Calendar interaction) and use macOS Automator to trigger it via the right-click menu.
Here is the development plan:
1. Project Setup & Configuration
* Structure: Create a directory for the project.
* Dependencies: Use Python's requests library to communicate with LLM APIs.
* Config: Create a config.json file where you can define your API Key, Base URL (allowing use of OpenAI, DeepSeek, or Gemini via compatible endpoints), and Model name.
2. Core Logic Implementation (remind.py)
* Input: The script will accept text arguments (the selected text from your email/document).
* Context: It will automatically detect the current system date/time so that relative dates like "next Friday" are parsed correctly.
* LLM Processing: It will send the text to the configured API with a prompt to extract event details (Title, Start Time, End Time, Location, Notes) in a strict JSON format.
* Calendar Automation: Instead of just generating an .ics file (which requires manual clicking to import), I will use AppleScript (executed via Python) to directly insert the event into your macOS Calendar app.
3. Verification
* I will create a dummy test to ensure the script runs and attempts to reach the API (handling errors gracefully if the API key is not yet set).
4. Integration (The "Right-Click" Menu)
* Since I cannot interact with your macOS GUI to create the Automator workflow, I will generate the Python script and then provide you with exact, simple steps to create the Automator Quick Action that runs this script.
I will start by setting up the project structure and the configuration file.
方案不错,批准了,请执行!很快,Gemini大哥就完成实现,并且给出了配置指南:
You now have a fully functional backend to parse text into Calendar events using an LLM.
Project Files Created
* remind.py: The main logic script.
* config.json: Configuration file for your API Key.
* run_remind.sh: A helper script for Automator to run the Python code easily.
* venv/: A Python virtual environment with necessary libraries installed.
Step 1: Configure API Key
Open the configuration file and paste your OpenAI (or compatible) API Key.
open config.json
Change `YOUR_API_KEY_HERE` to your actual key.
Step 2: Create the "Right-Click" Menu (Automator)
Since this part interacts with the macOS UI, you need to perform these steps manually once:
1. Open Automator (cmd+space, type "Automator").
2. Choose New Document -> Quick Action.
3. At the top, configure:
* Workflow receives current Text in Any Application.
4. In the left sidebar, search for Run Shell Script and drag it to the right.
5. Configure the script action:
* Shell: /bin/bash
* Pass input: as arguments (Important!)
* Paste the following command (using the absolute path to your project):
/ABSOLUTE_PATH/run_remind.sh "$1"
6. Press Cmd+S to save. Name it Add Event (or "添加日程").
How to Use
1. Select any text containing a date/time (e.g., in an email or website).
2. Right-click the text.
3. Go to Services (or just look at the bottom of the menu) -> Add Event.
4. The script will run in the background, send the text to the LLM, and automatically add the event to your macOS Calendar.
Troubleshooting:
If nothing happens, check the log file created by the script:
cat /tmp/remind_log.txt
按照它的提示配置完(文字解析使用的是物美价廉的gpt-5-nano),我测试了一下,检查日志,发现了问题,再次交互:
遇到了问题:Analyzing text...
Error calling API: 400 Client Error: Bad Request for url: https://api.openai.com/v1/chat/completions
Failed to extract event data.
我建议你使用litellm来调用,而非自己手写request
Gemini很快完成了基于LiteLLM的实现。我再次提出需求:
现在就不必在配置文件中添加base_url了,不过我希望配置文件中支持设置提前几分钟提醒。
Gemini再次完成实现。现在,整个流程已经很流畅了。不过,我还希望更方便一些:
我怎么才能将这个右键菜单从“Services”中移出来到一级菜单?
Gemini大哥不厌其烦地给出指点:
在 macOS 系统中,原生不支持用户直接将 Automator (自动操作) 创建的“快速操作”强行固定在右键的一级菜单中。macOS
会根据你启用的服务数量,自动决定是将它们折叠进“服务 (Services)”子菜单,还是显示在菜单底部。
要想实现“在一级菜单直接点击”,通常有以下两种方案:
方案一:清理“服务”菜单(原生,但不保证 100% 成功)
macOS 的逻辑是:如果启用的“服务”很少,它有时会将它们直接显示在右键菜单底部,而不折叠进子菜单。你可以尝试禁用不用的服务:
1. 打开 系统设置 (System Settings) -> 键盘 (Keyboard)。
2. 点击 键盘快捷键 (Keyboard Shortcuts) -> 服务 (Services)。
3. 在“文本 (Text)”分类下,取消勾选你不常用的所有服务,只保留刚才创建的“Add Event”。
4. 重启应用(如浏览器或邮件)看是否生效。
...
我再次尝试,发现了一个问题——设置的提前15分钟变成了15小时,于是再次发问:
现在似乎填写的提前15分钟最终在日历中变成了提前15小时,请改正。另外,我希望生成的事件能够体现出事件的本质特征。
例如,对于下面的文字,我希望生成的事件标题为Anonymous's PH.D Defense之类,而不是他的defense题目(...):
PH.D DEFENCE - PUBLIC SEMINAR
Title: ...
Speaker: Mr. Anonymous
Date/Time: 1 February 2026, Monday, 03:00 PM to 04:30 PM
Venue: XXXX, XXXX XX-XX
Advisor: Dr XXX, XXX, School of Computing
Gemini再次更新代码中的prompt。至此,这个小玩意儿已经完全满足了我的使用需求。