-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_personal_voice_auth.py
More file actions
110 lines (86 loc) · 3.9 KB
/
request_personal_voice_auth.py
File metadata and controls
110 lines (86 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python3
"""
Request Personal Voice authorization for macOS Sonoma.
This script uses PyObjC to access the AVFoundation framework and request
Personal Voice permissions, similar to the C++ solution but in Python.
After running this script and granting permission, you can use Personal Voice
with the `say` command like: say -v "YourVoiceName" "Hello world"
"""
import sys
import time
from Foundation import NSRunLoop, NSDate
from AVFoundation import AVSpeechSynthesizer
def request_personal_voice_authorization():
"""Request Personal Voice authorization from macOS."""
print("Requesting Personal Voice authorization...")
print("A system dialog should appear asking for permission.")
authorization_completed = [False] # Use list for closure
def completion_handler(status):
"""Handle the authorization response."""
status_names = {
0: "NotDetermined",
1: "Denied",
2: "Restricted",
3: "Authorized"
}
status_name = status_names.get(status, f"Unknown({status})")
print(f"Authorization status: {status_name}")
if status == 3: # Authorized
print("✅ Personal Voice authorization granted!")
print("You can now use Personal Voice with the 'say' command.")
print("Example: say -v 'YourVoiceName' 'Hello world'")
elif status == 1: # Denied
print("❌ Personal Voice authorization denied.")
print("You can change this in System Settings > Privacy & Security > Personal Voice")
elif status == 2: # Restricted
print("⚠️ Personal Voice is restricted on this system.")
else:
print("ℹ️ Authorization status not determined yet.")
authorization_completed[0] = True
# Request authorization
AVSpeechSynthesizer.requestPersonalVoiceAuthorizationWithCompletionHandler_(
completion_handler
)
# Run the event loop until authorization completes
print("Waiting for user response...")
run_loop = NSRunLoop.currentRunLoop()
# Wait up to 60 seconds for user response
timeout = 60
start_time = time.time()
while not authorization_completed[0] and (time.time() - start_time) < timeout:
# Run the loop for a short time to process events
run_loop.runUntilDate_(NSDate.dateWithTimeIntervalSinceNow_(0.1))
if not authorization_completed[0]:
print("⏰ Timeout waiting for authorization response.")
return False
return True
def check_current_authorization():
"""Check the current Personal Voice authorization status."""
try:
# This might not be available in all macOS versions
# The main approach is to request authorization which will show current status
print("Checking current authorization status...")
return request_personal_voice_authorization()
except Exception as e:
print(f"Could not check authorization status: {e}")
return False
if __name__ == "__main__":
print("macOS Personal Voice Authorization Tool")
print("=" * 40)
try:
success = request_personal_voice_authorization()
if success:
print("\n🎉 Authorization process completed!")
print("\nNext steps:")
print("1. Find your Personal Voice name with: say -v '?'")
print("2. Test it with: say -v 'YourVoiceName' 'Hello, this is my personal voice'")
else:
print("\n❌ Authorization process failed or timed out.")
except ImportError as e:
print(f"❌ Error: Missing required dependencies: {e}")
print("\nTo install PyObjC:")
print("pip install pyobjc-framework-AVFoundation pyobjc-framework-Foundation")
sys.exit(1)
except Exception as e:
print(f"❌ Unexpected error: {e}")
sys.exit(1)