@tanskong/office-assistant 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanskong/office-assistant",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Office Assistant - AI-powered Office automation via MCP protocol with license protection",
5
5
  "main": "bin/office-assistant.js",
6
6
  "bin": {
package/requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
- fastmcp>=2.3.3
2
- pywin32>=310
3
- Pillow>=10.0.0
1
+ fastmcp>=2.3.3
2
+ pywin32>=310
3
+ Pillow>=10.0.0
4
+ psutil>=5.9.0
package/src/officer.py CHANGED
@@ -147,10 +147,35 @@ class TheOfficer:
147
147
  self.__dict__[app_name_attr] = app
148
148
  return app
149
149
  else:
150
+ # 策略1:优先尝试 GetActiveObject
150
151
  try:
151
152
  app = win32com.client.GetActiveObject(app_full_name)
152
- except pywintypes.com_error:
153
- app = win32com.client.Dispatch(app_full_name)
153
+ # 验证可用性
154
+ _ = app.Visible
155
+ self.__dict__[app_name_attr] = app
156
+ return app
157
+ except (pywintypes.com_error, Exception):
158
+ pass
159
+
160
+ # 策略2:通过进程列表查找当前用户的实例(需要 psutil)
161
+ try:
162
+ import psutil
163
+ current_user = psutil.Process().username()
164
+ for proc in psutil.process_iter(['pid', 'name', 'username']):
165
+ if proc.info['name'].lower() == app_name.lower() + '.exe' and proc.info['username'] == current_user:
166
+ pid = proc.info['pid']
167
+ # 使用动态分派绑定到指定进程
168
+ import win32com.client.dynamic
169
+ moniker = f"!{pid}"
170
+ app = win32com.client.dynamic.Dispatch(moniker)
171
+ _ = app.Visible
172
+ self.__dict__[app_name_attr] = app
173
+ return app
174
+ except Exception:
175
+ pass
176
+
177
+ # 策略3:兜底,创建新实例
178
+ app = win32com.client.Dispatch(app_full_name)
154
179
  self.__dict__[app_name_attr] = app
155
180
  return app
156
181
  except Exception as e:
package/src/server.py CHANGED
@@ -194,10 +194,15 @@ def run_python(code: str, app_name: str = "Excel") -> dict:
194
194
  # ========== System Tools ==========
195
195
 
196
196
  @mcp.tool()
197
- def screenshot(save_name: str = None) -> str:
197
+ def screenshot(save_name: str = None) -> dict:
198
198
  """Capture a screenshot and save to the export folder."""
199
- path = Officer.ScreenShot(save_name)
200
- return path
199
+ try:
200
+ path = Officer.ScreenShot(save_name)
201
+ return {"success": True, "path": path}
202
+ except ImportError as e:
203
+ return {"success": False, "error": "缺少 Pillow 模块,请运行: pip install Pillow", "detail": str(e)}
204
+ except Exception as e:
205
+ return {"success": False, "error": str(e)}
201
206
 
202
207
 
203
208
  @mcp.tool()