Typora 每次粘贴图片时都跳出 PicGo 窗口的解决方案
问题说明
Typora 使用 PicGo.app 作为图片上传服务时,每次粘贴图片上传,PicGo 主窗口都会弹出,打断写作。即使将 PicGo 设为静默启动,仍可能出现这一现象。
本方案将 Typora 的上传服务改为 自定义命令,通过脚本直接调用 PicGo 的本地上传服务,使图片在后台上传,避免重复启动 PicGo 导致主窗口弹出。
以下步骤默认已安装 PicGo,并已配置好图床及所需插件。
本地环境
| 项目 | 版本或配置 |
|---|---|
| 操作系统 | macOS 26.6.2 |
| 架构 | Apple Silicon(arm64) |
| Typora | 1.14.10 |
| PicGo | 3.0.3,已安装于 /Applications/PicGo.app |
| Python | 3.9.6,解释器路径为 /usr/bin/python3 |
| 上传服务 | PicGo-Server,127.0.0.1:36677 |
脚本保存路径
text
~/Library/Application Support/Typora/typora-picgo-background.py
完整路径示例(请替换用户名):
text
/Users/你的用户名/Library/Application Support/Typora/typora-picgo-background.py
配置步骤
- 在 PicGo 设置中,将启动模式设为 静默启动 ,关闭 显示 Dock 栏图标 ,保留 显示顶部栏图标。
- 开启 PicGo-Server ,监听地址设为
127.0.0.1,端口设为36677。 - 在终端执行以下命令,创建目录并打开脚本文件:
sh
mkdir -p "$HOME/Library/Application Support/Typora"
nano "$HOME/Library/Application Support/Typora/typora-picgo-background.py"
- 粘贴下方完整脚本代码,按
Control + O、回车保存,按Control + X退出。 - 打开 Typora → 偏好设置 → 图像 ,将 插入图片时... 设为 上传图片 ,勾选 对本地位置的图片应用上述规则。
- 将 上传服务 设为 自定义命令,填写:
sh
/usr/bin/python3 "/Users/你的用户名/Library/Application Support/Typora/typora-picgo-background.py"
将 你的用户名 替换为自己的 macOS 用户名,保留路径两侧的双引号。需要有可用的 Python 3;若解释器安装在其他位置,将 /usr/bin/python3 替换为其完整路径。
脚本代码
python
#!/usr/bin/python3
"""Upload through the existing PicGo server without activating its window."""
import json
import sys
import time
import subprocess
import urllib.request
import urllib.error
ENDPOINT = "http://127.0.0.1:36677"
HTTP = urllib.request.build_opener(urllib.request.ProxyHandler({}))
def ready():
try:
HTTP.open(ENDPOINT, timeout=1).close()
return True
except urllib.error.HTTPError:
return True
except (urllib.error.URLError, OSError):
return False
def main():
paths = sys.argv[1:]
if not paths:
raise RuntimeError("No image paths supplied; clipboard upload is disabled.")
if not ready():
subprocess.run(["/usr/bin/open", "-g", "-a", "/Applications/PicGo.app"], check=True)
for _ in range(100):
if ready():
break
time.sleep(0.2)
else:
raise RuntimeError("PicGo server did not start on port 36677.")
request = urllib.request.Request(ENDPOINT + "/upload",
data=json.dumps({"list": paths}).encode(),
headers={"Content-Type": "application/json"}, method="POST")
with HTTP.open(request, timeout=180) as response:
result = json.load(response)
urls = result.get("result", [])
if not result.get("success") or len(urls) != len(paths):
raise RuntimeError("PicGo upload failed: " + str(result.get("message", result.get("msg", "Check PicGo logs"))))
if not all(isinstance(url, str) and url.startswith(("https://", "http://")) for url in urls):
raise RuntimeError("PicGo returned invalid image URLs.")
print("\n".join(urls))
if __name__ == "__main__":
try:
main()
except Exception as error:
print(str(error), file=sys.stderr)
sys.exit(1)