Skip to content

Commit 34657be

Browse files
committed
using apple shortcuts
1 parent 38326d7 commit 34657be

File tree

4 files changed

+51
-54
lines changed

4 files changed

+51
-54
lines changed

README.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44

55
- [ChatGPT macOS app](https://openai.com/chatgpt/download/)
66
- [uv](https://docs.astral.sh/uv/getting-started/installation/)
7+
- Install the ["Ask ChatGPT on Mac" shortcut](https://www.icloud.com/shortcuts/6ae86a39a31e4ec5938abad953ecfd64)
78

8-
## Required Permissions
9-
10-
Before using the server, you need to grant the following permissions:
11-
12-
1. Open System Settings > Privacy & Security > Accessibility
13-
2. Click the "+" button and add your mcp client (cursor, chatwise, etc.)
14-
3. Make sure the checkbox next to your mcp client app is checked
9+
## Required Shortcuts
1510

11+
1. Download and install the ["Ask ChatGPT on Mac" shortcut](https://www.icloud.com/shortcuts/6ae86a39a31e4ec5938abad953ecfd64)
12+
2. Make sure the shortcut is named exactly "Ask ChatGPT on Mac"
1613

1714
## Usage
1815

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "mcp-server-chatgpt-app"
3-
version = "0.1.3"
3+
version = "0.1.4"
44
description = "MCP Server for ChatGPT via AppleScript"
55
readme = "README.md"
66
requires-python = ">=3.10"

src/mcp_server_chatgpt/server.py

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,74 @@
11
import subprocess
2+
import os
23
from typing import Optional, Dict, Any
34
from mcp.server.fastmcp import FastMCP
45

56
mcp = FastMCP("ChatGPT")
67

7-
def run_applescript(script: str) -> Dict[str, Any]:
8-
"""Run an AppleScript and return its output and status."""
8+
def run_applescript(script: str, wait_for_output: bool = True) -> Dict[str, Any]:
9+
"""
10+
Run an AppleScript and return its output and status.
11+
12+
Args:
13+
script: The AppleScript to run
14+
wait_for_output: Whether to wait for and capture output
15+
"""
916
try:
10-
result = subprocess.run(
11-
['osascript', '-e', script],
12-
capture_output=True,
13-
text=True,
14-
check=True
15-
)
16-
return {
17-
"success": True,
18-
"output": result.stdout.strip()
19-
}
20-
except subprocess.CalledProcessError as e:
21-
error_msg = e.stderr.strip()
22-
help_msg = ""
23-
24-
if "is not allowed to send keystrokes" in error_msg:
25-
help_msg = (
26-
"Permission Error: The script needs accessibility permissions.\n"
27-
"1. Open System Settings > Privacy & Security > Accessibility\n"
28-
"2. Add and enable your terminal application\n"
29-
"3. Also check System Settings > Privacy & Security > Automation"
17+
if wait_for_output:
18+
# Run synchronously and capture output
19+
result = subprocess.run(
20+
['osascript', '-e', script],
21+
capture_output=True,
22+
text=True,
23+
check=True
3024
)
31-
elif "not allowed to send apple events to" in error_msg.lower():
32-
help_msg = (
33-
"Permission Error: The script needs automation permissions.\n"
34-
"1. Open System Settings > Privacy & Security > Automation\n"
35-
"2. Enable permissions for your terminal to control 'ChatGPT' and 'System Events'"
25+
return {
26+
"success": True,
27+
"output": result.stdout.strip()
28+
}
29+
else:
30+
# Use Popen for non-blocking execution
31+
subprocess.Popen(
32+
['osascript', '-e', script],
33+
stdout=subprocess.DEVNULL,
34+
stderr=subprocess.DEVNULL,
35+
start_new_session=True
3636
)
37-
37+
return {
38+
"success": True,
39+
"output": ""
40+
}
41+
except Exception as e:
42+
error_msg = str(e)
3843
return {
3944
"success": False,
4045
"error": error_msg,
41-
"help": help_msg
4246
}
4347

4448
@mcp.tool()
45-
def ask_chatgpt(prompt: str) -> Dict[str, Any]:
49+
def ask_chatgpt(prompt: str, wait_for_output: bool = False) -> Dict[str, Any]:
4650
"""
47-
Send a prompt to ChatGPT macOS app and optionally wait for a response.
51+
Send a prompt to ChatGPT macOS app using Shortcuts.
4852
4953
Args:
5054
prompt: The text to send to ChatGPT
51-
55+
wait_for_output: Whether to wait for ChatGPT to respond
5256
Returns:
5357
Dict containing operation status
5458
"""
59+
# Escape double quotes in the prompt for AppleScript
60+
escaped_prompt = prompt.replace('"', '\\"')
61+
5562
script = f'''
56-
tell application "ChatGPT"
57-
activate
58-
delay 1
59-
60-
tell application "System Events"
61-
tell process "ChatGPT"
62-
keystroke "{prompt}"
63-
delay 0.5
64-
keystroke return
65-
end tell
66-
end tell
63+
set shortcutName to "Ask ChatGPT on Mac"
64+
set shortcutInput to "{escaped_prompt}"
65+
66+
tell application "Shortcuts Events"
67+
run shortcut shortcutName with input shortcutInput
6768
end tell
6869
'''
6970

70-
result = run_applescript(script)
71+
result = run_applescript(script, wait_for_output=wait_for_output)
7172

7273
if result["success"]:
7374
return {
@@ -80,7 +81,6 @@ def ask_chatgpt(prompt: str) -> Dict[str, Any]:
8081
"operation": "ask_chatgpt",
8182
"status": "error",
8283
"message": result["error"],
83-
"help": result["help"]
8484
}
8585

8686
def main():

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)