screenshot.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. """
  2. agent67.tools.screenshot — 截屏工具 (macOS)
  3. Lambda: λ options. screencapture(options)
  4. """
  5. import json
  6. import os
  7. import platform
  8. import subprocess
  9. from datetime import datetime
  10. def take_screenshot(input_json: str) -> str:
  11. """
  12. 截取屏幕截图。
  13. 支持: full / area / window
  14. """
  15. try:
  16. data = json.loads(input_json)
  17. shot_type = data.get("type", "full")
  18. except (json.JSONDecodeError, AttributeError):
  19. shot_type = "full"
  20. if platform.system() != "Darwin":
  21. return "⚠️ 截屏目前仅支持 macOS"
  22. timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
  23. filepath = os.path.expanduser(f"~/Desktop/screenshot_{timestamp}.png")
  24. try:
  25. if shot_type == "area":
  26. subprocess.run(["screencapture", "-i", filepath], timeout=30)
  27. elif shot_type == "window":
  28. subprocess.run(["screencapture", "-w", filepath], timeout=30)
  29. else:
  30. subprocess.run(["screencapture", "-x", filepath], timeout=10)
  31. if os.path.exists(filepath):
  32. size_kb = os.path.getsize(filepath) // 1024
  33. return f"📸 截图已保存: {filepath} ({size_kb} KB)"
  34. else:
  35. return "❌ 截图被取消"
  36. except Exception as e:
  37. return f"❌ 截图错误: {e}"