| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Online-Translator Setup Script |
| 4 | ------------------------------ |
| 5 | This script creates a macOS application bundle for the Online-Translator project. |
| 6 | It handles the creation of the .app structure, installation of dependencies, |
| 7 | and setup of MLX models. |
| 8 | |
| 9 | Copyright (c) 2025 Kiko Cisneros |
| 10 | Licensed under the MIT License (see LICENSE file for details) |
| 11 | """ |
| 12 | |
| 13 | """ |
| 14 | Setup script to create a Mac application bundle for Online-Translator |
| 15 | Enhanced version with better permissions handling |
| 16 | """ |
| 17 | |
| 18 | import os |
| 19 | import sys |
| 20 | import shutil |
| 21 | import plistlib |
| 22 | import subprocess |
| 23 | from pathlib import Path |
| 24 | import json |
| 25 | import stat |
| 26 | import time |
| 27 | import tempfile |
| 28 | import base64 |
| 29 | import zlib |
| 30 | import struct |
| 31 | import uuid |
| 32 | import hashlib |
| 33 | import re |
| 34 | import glob |
| 35 | import fnmatch |
| 36 | import logging |
| 37 | import argparse |
| 38 | import signal |
| 39 | import atexit |
| 40 | import threading |
| 41 | import queue |
| 42 | import traceback |
| 43 | import platform |
| 44 | import socket |
| 45 | import getpass |
| 46 | import pwd |
| 47 | import grp |
| 48 | import pkg_resources |
| 49 | import importlib |
| 50 | import importlib.util |
| 51 | import importlib.machinery |
| 52 | import importlib.abc |
| 53 | import importlib.resources |
| 54 | import importlib.metadata |
| 55 | import requests |
| 56 | import tarfile |
| 57 | |
| 58 | def setup_macos_permissions(app_bundle): |
| 59 | """Setup macOS permissions with improved error handling""" |
| 60 | try: |
| 61 | print("๐ Configuring macOS permissions...") |
| 62 | |
| 63 | # Remove quarantine attributes more thoroughly |
| 64 | commands = [ |
| 65 | ["xattr", "-r", "-d", "com.apple.quarantine", app_bundle], |
| 66 | ["xattr", "-r", "-d", "com.apple.metadata:kMDItemDownloadedDate", app_bundle], |
| 67 | ["xattr", "-r", "-d", "com.apple.metadata:kMDItemWhereFroms", app_bundle], |
| 68 | ] |
| 69 | |
| 70 | for cmd in commands: |
| 71 | try: |
| 72 | result = subprocess.run(cmd, capture_output=True, text=True) |
| 73 | if result.returncode == 0: |
| 74 | print(f" โ
Removed attribute: {cmd[3]}") |
| 75 | else: |
| 76 | print(f" ๐ No attribute to remove: {cmd[3]}") |
| 77 | except Exception as e: |
| 78 | print(f" ๐ Note: {cmd[3]} - {e}") |
| 79 | |
| 80 | # Set executable permissions recursively with better error handling |
| 81 | try: |
| 82 | # Set permissions for the main executable |
| 83 | executable_path = Path(app_bundle) / "Contents" / "MacOS" / "Online-Translator" |
| 84 | if executable_path.exists(): |
| 85 | os.chmod(executable_path, 0o755) |
| 86 | print(" โ
Set executable permissions") |
| 87 | |
| 88 | # Set permissions for all Python files |
| 89 | resources_dir = Path(app_bundle) / "Contents" / "Resources" |
| 90 | if resources_dir.exists(): |
| 91 | for py_file in resources_dir.rglob("*.py"): |
| 92 | os.chmod(py_file, 0o644) |
| 93 | |
| 94 | # Set permissions on directories |
| 95 | for dir_path in resources_dir.rglob("*"): |
| 96 | if dir_path.is_dir(): |
| 97 | os.chmod(dir_path, 0o755) |
| 98 | |
| 99 | print(" โ
Set file permissions") |
| 100 | |
| 101 | except Exception as e: |
| 102 | print(f" โ ๏ธ Permission warning: {e}") |
| 103 | |
| 104 | # Try to make the app trusted |
| 105 | try: |
| 106 | print(" ๐ Attempting to add app to trusted apps...") |
| 107 | subprocess.run([ |
| 108 | "spctl", "--add", "--label", "Online-Translator", os.path.abspath(app_bundle) |
| 109 | ], capture_output=True, text=True) |
| 110 | print(" โ
Added to trusted apps") |
| 111 | except Exception as e: |
| 112 | print(f" ๐ Trust note: {e}") |
| 113 | |
| 114 | # Try to register with Launch Services |
| 115 | try: |
| 116 | lsregister_path = "/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister" |
| 117 | if os.path.exists(lsregister_path): |
| 118 | subprocess.run([ |
| 119 | lsregister_path, "-f", os.path.abspath(app_bundle) |
| 120 | ], capture_output=True) |
| 121 | print(" โ
Registered with Launch Services") |
| 122 | else: |
| 123 | print(" ๐ Launch Services registration skipped (path not found)") |
| 124 | except Exception as e: |
| 125 | print(f" ๐ Launch Services note: {e}") |
| 126 | |
| 127 | print("โ
macOS permissions configured") |
| 128 | |
| 129 | except Exception as e: |
| 130 | print(f"โ ๏ธ Warning: Could not configure all permissions: {e}") |
| 131 | |
| 132 | def create_launch_instructions(): |
| 133 | """Create detailed instructions for launching Online-Translator""" |
| 134 | instructions = """# ๐ Online-Translator - Live Subtitles Launch Guide |
| 135 | |
| 136 | Online-Translator has been successfully created! Here are the different ways to launch it: |
| 137 | |
| 138 | ## โ
Method 1: Terminal (Always Works) |
| 139 | ```bash |
| 140 | ./Online-Translator.app/Contents/MacOS/Online-Translator |
| 141 | ``` |
| 142 | |
| 143 | ## โ
Method 2: Python Direct |
| 144 | ```bash |
| 145 | python3 main.py |
| 146 | ``` |
| 147 | |
| 148 | ## โ
Method 3: Finder (May need permission fix) |
| 149 | 1. Double-click `Online-Translator.app` in Finder |
| 150 | 2. If you see a security warning: |
| 151 | - Right-click on `Online-Translator.app` |
| 152 | - Select "Open" from the menu |
| 153 | - Click "Open" in the security dialog |
| 154 | |
| 155 | ## ๐ง If Online-Translator won't open from Finder: |
| 156 | Run the permission fixer: |
| 157 | ```bash |
| 158 | ./fix_Online-Translator_permissions.sh |
| 159 | ``` |
| 160 | |
| 161 | ## ๐ฑ Using Online-Translator: |
| 162 | - **Audio**: Auto-detects microphone and system audio |
| 163 | - **Translation**: Real-time translation with MLX models |
| 164 | - **Controls**: Double-click to show/hide controls |
| 165 | - **Move**: Drag the window to reposition |
| 166 | - **History**: Click ๐ to see transcription history |
| 167 | |
| 168 | ## โ ๏ธ Security Note: |
| 169 | The error -54 or security warnings are normal for unsigned apps. |
| 170 | This is because Online-Translator is not digitally signed by Apple. |
| 171 | |
| 172 | --- |
| 173 | โค๏ธ Created with love by Kiko Cisneros for his children |
| 174 | """ |
| 175 | |
| 176 | with open("LAUNCH_INSTRUCTIONS.md", "w") as f: |
| 177 | f.write(instructions) |
| 178 | |
| 179 | print("๐ Created LAUNCH_INSTRUCTIONS.md") |
| 180 | |
| 181 | def create_install_script(): |
| 182 | """Create an installation script for dependencies""" |
| 183 | install_script = '''#!/usr/bin/env python3 |
| 184 | """ |
| 185 | Installation script for Online-Translator dependencies |
| 186 | Created with โค๏ธ by Kiko Cisneros for his children |
| 187 | Run this script after moving the app to install required Python packages |
| 188 | """ |
| 189 | |
| 190 | import subprocess |
| 191 | import sys |
| 192 | import os |
| 193 | import time |
| 194 | from pathlib import Path |
| 195 | |
| 196 | def print_header(): |
| 197 | """Print a beautiful header for the installation process""" |
| 198 | print("\\n" + "=" * 60) |
| 199 | print("๐ฏ Online-Translator - Dependencies Installation") |
| 200 | print("=" * 60) |
| 201 | print("โค๏ธ Created with love by Kiko Cisneros for his children") |
| 202 | print("=" * 60 + "\\n") |
| 203 | |
| 204 | def check_python_version(): |
| 205 | """Check if Python version is compatible""" |
| 206 | required_version = (3, 9) |
| 207 | current_version = sys.version_info[:2] |
| 208 | |
| 209 | if current_version < required_version: |
| 210 | print(f"โ Python {required_version[0]}.{required_version[1]} or higher is required") |
| 211 | print(f" Current version: {current_version[0]}.{current_version[1]}") |
| 212 | return False |
| 213 | return True |
| 214 | |
| 215 | def install_ffmpeg(): |
| 216 | """Install ffmpeg if not present""" |
| 217 | print("\\n๐ฅ Checking ffmpeg installation...") |
| 218 | try: |
| 219 | subprocess.run(["ffmpeg", "-version"], check=True, capture_output=True) |
| 220 | print("โ
ffmpeg is already installed!") |
| 221 | return True |
| 222 | except (subprocess.CalledProcessError, FileNotFoundError): |
| 223 | print("๐ฆ ffmpeg not found, attempting to install...") |
| 224 | |
| 225 | # Check if Homebrew is available |
| 226 | try: |
| 227 | subprocess.run(["brew", "--version"], check=True, capture_output=True) |
| 228 | print(" โ
Homebrew is available") |
| 229 | |
| 230 | print(" ๐ฅ Installing ffmpeg using Homebrew...") |
| 231 | result = subprocess.run(["brew", "install", "ffmpeg"], |
| 232 | capture_output=True, text=True) |
| 233 | |
| 234 | if result.returncode == 0: |
| 235 | print("โ
ffmpeg installed successfully!") |
| 236 | return True |
| 237 | else: |
| 238 | print("โ Error installing ffmpeg:") |
| 239 | print(result.stderr) |
| 240 | print("\\nPlease install ffmpeg manually:") |
| 241 | print(" brew install ffmpeg") |
| 242 | return False |
| 243 | except (subprocess.CalledProcessError, FileNotFoundError): |
| 244 | print("โ Homebrew not found. Please install ffmpeg manually:") |
| 245 | print(" 1. Install Homebrew: https://brew.sh") |
| 246 | print(" 2. Run: brew install ffmpeg") |
| 247 | return False |
| 248 | |
| 249 | def install_dependencies(): |
| 250 | """Install required Python dependencies for Online-Translator""" |
| 251 | |
| 252 | print_header() |
| 253 | |
| 254 | # Check Python version |
| 255 | if not check_python_version(): |
| 256 | return False |
| 257 | |
| 258 | # Check if pip is available |
| 259 | print("๐ Checking pip installation...") |
| 260 | try: |
| 261 | subprocess.run([sys.executable, "-m", "pip", "--version"], |
| 262 | check=True, capture_output=True) |
| 263 | print("โ
pip is available") |
| 264 | except subprocess.CalledProcessError: |
| 265 | print("โ pip is not available. Please install pip first.") |
| 266 | print(" You can install pip by running:") |
| 267 | print(" curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py") |
| 268 | print(" python3 get-pip.py") |
| 269 | return False |
| 270 | |
| 271 | # Install requirements |
| 272 | print("\\n๐ฆ Installing Python packages...") |
| 273 | try: |
| 274 | # Upgrade pip first |
| 275 | print(" ๐ Upgrading pip...") |
| 276 | subprocess.run([ |
| 277 | sys.executable, "-m", "pip", "install", "--upgrade", "pip" |
| 278 | ], check=True, capture_output=True) |
| 279 | |
| 280 | # Install requirements |
| 281 | print(" ๐ฅ Installing required packages...") |
| 282 | result = subprocess.run([ |
| 283 | sys.executable, "-m", "pip", "install", "-r", "requirements.txt" |
| 284 | ], capture_output=True, text=True) |
| 285 | |
| 286 | if result.returncode == 0: |
| 287 | print("โ
Python dependencies installed successfully!") |
| 288 | else: |
| 289 | print(f"โ Error installing dependencies: {result.stderr}") |
| 290 | return False |
| 291 | |
| 292 | except subprocess.CalledProcessError as e: |
| 293 | print(f"โ Error during installation: {e}") |
| 294 | return False |
| 295 | |
| 296 | # Install ffmpeg |
| 297 | if not install_ffmpeg(): |
| 298 | print("โ ๏ธ Warning: ffmpeg installation failed. Some features may not work.") |
| 299 | |
| 300 | # Final success message |
| 301 | print("\\n" + "=" * 60) |
| 302 | print("๐ Online-Translator installation complete!") |
| 303 | print("=" * 60) |
| 304 | print("\\nYou can now run Online-Translator using any of these methods:") |
| 305 | print(" 1. Terminal: ./Online-Translator.app/Contents/MacOS/Online-Translator") |
| 306 | print(" 2. Python: python3 main.py") |
| 307 | print(" 3. Finder: Double-click Online-Translator.app") |
| 308 | print("\\nโค๏ธ Enjoy using Online-Translator - made with love for family!") |
| 309 | print("=" * 60 + "\\n") |
| 310 | |
| 311 | return True |
| 312 | |
| 313 | if __name__ == "__main__": |
| 314 | # Change to the directory containing this script |
| 315 | script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 316 | os.chdir(script_dir) |
| 317 | |
| 318 | try: |
| 319 | install_dependencies() |
| 320 | except KeyboardInterrupt: |
| 321 | print("\\n\\nโ Installation cancelled by user") |
| 322 | sys.exit(1) |
| 323 | except Exception as e: |
| 324 | print(f"\\n\\nโ Unexpected error: {e}") |
| 325 | sys.exit(1) |
| 326 | ''' |
| 327 | |
| 328 | with open("install_dependencies.py", "w") as f: |
| 329 | f.write(install_script) |
| 330 | |
| 331 | os.chmod("install_dependencies.py", 0o755) |
| 332 | print("๐ฆ Created install_dependencies.py script for Online-Translator") |
| 333 | |
| 334 | def create_simple_icon(icon_path): |
| 335 | """Create app icon from the custom logoOnline-Translator.png with improved error handling""" |
| 336 | try: |
| 337 | # Look for the custom logo |
| 338 | logo_path = "media/logoCovi.png" |
| 339 | |
| 340 | if os.path.exists(logo_path): |
| 341 | print(f"๐ฑ Using custom Online-Translator logo: {logo_path}") |
| 342 | |
| 343 | # Create iconset directory for proper ICNS creation |
| 344 | iconset_dir = "AppIcon.iconset" |
| 345 | if os.path.exists(iconset_dir): |
| 346 | shutil.rmtree(iconset_dir) |
| 347 | os.makedirs(iconset_dir) |
| 348 | |
| 349 | # Define icon sizes needed for macOS |
| 350 | icon_sizes = [ |
| 351 | (16, "icon_16x16.png"), |
| 352 | (32, "icon_16x16@2x.png"), |
| 353 | (32, "icon_32x32.png"), |
| 354 | (64, "icon_32x32@2x.png"), |
| 355 | (128, "icon_128x128.png"), |
| 356 | (256, "icon_128x128@2x.png"), |
| 357 | (256, "icon_256x256.png"), |
| 358 | (512, "icon_256x256@2x.png"), |
| 359 | (512, "icon_512x512.png"), |
| 360 | (1024, "icon_512x512@2x.png") |
| 361 | ] |
| 362 | |
| 363 | # Generate all required icon sizes |
| 364 | print("๐จ Generating icon sizes...") |
| 365 | for size, filename in icon_sizes: |
| 366 | output_path = os.path.join(iconset_dir, filename) |
| 367 | result = subprocess.run([ |
| 368 | "sips", "-z", str(size), str(size), logo_path, "--out", output_path |
| 369 | ], capture_output=True, text=True) |
| 370 | |
| 371 | if result.returncode != 0: |
| 372 | print(f"โ ๏ธ Warning creating {filename}: {result.stderr}") |
| 373 | else: |
| 374 | print(f"โ
Created {filename}") |
| 375 | |
| 376 | # Convert iconset to ICNS |
| 377 | print("๐ Converting to ICNS format...") |
| 378 | result = subprocess.run([ |
| 379 | "iconutil", "-c", "icns", iconset_dir, "-o", str(icon_path) |
| 380 | ], capture_output=True, text=True) |
| 381 | |
| 382 | if result.returncode == 0: |
| 383 | print("โ
Custom ICNS icon created successfully!") |
| 384 | |
| 385 | # Verify the icon was created properly |
| 386 | if os.path.exists(icon_path) and os.path.getsize(icon_path) > 0: |
| 387 | print(f"โ
Icon file verified: {os.path.getsize(icon_path)} bytes") |
| 388 | |
| 389 | # Set proper permissions on icon file |
| 390 | os.chmod(icon_path, 0o644) |
| 391 | print("โ
Icon permissions set") |
| 392 | else: |
| 393 | print("โ ๏ธ Icon file is empty or missing") |
| 394 | return False |
| 395 | |
| 396 | else: |
| 397 | print(f"โ ๏ธ ICNS creation failed: {result.stderr}") |
| 398 | # Fallback: try simple conversion |
| 399 | print("๐ Trying fallback icon creation...") |
| 400 | fallback_result = subprocess.run([ |
| 401 | "sips", "-s", "format", "icns", "-z", "512", "512", |
| 402 | logo_path, "--out", str(icon_path) |
| 403 | ], capture_output=True, text=True) |
| 404 | |
| 405 | if fallback_result.returncode == 0: |
| 406 | print("โ
Fallback icon created") |
| 407 | os.chmod(icon_path, 0o644) |
| 408 | return True |
| 409 | else: |
| 410 | print(f"โ Fallback icon failed: {fallback_result.stderr}") |
| 411 | return False |
| 412 | |
| 413 | # Clean up temporary iconset |
| 414 | if os.path.exists(iconset_dir): |
| 415 | shutil.rmtree(iconset_dir) |
| 416 | print("๐งน Cleaned up temporary files") |
| 417 | |
| 418 | return True |
| 419 | |
| 420 | else: |
| 421 | print(f"โ ๏ธ Custom logo not found at {logo_path}") |
| 422 | print("๐ Creating fallback icon...") |
| 423 | |
| 424 | # Try to create a simple colored icon as fallback |
| 425 | fallback_script = f'''#!/bin/bash |
| 426 | # Create a simple colored icon as fallback |
| 427 | convert -size 512x512 -background '#2196F3' -fill white -gravity center -pointsize 200 label:'Online-Translator' "{icon_path}" |
| 428 | ''' |
| 429 | try: |
| 430 | # Try ImageMagick first |
| 431 | result = subprocess.run([ |
| 432 | "convert", "-size", "512x512", "-background", "#2196F3", |
| 433 | "-fill", "white", "-gravity", "center", "-pointsize", "200", |
| 434 | "label:Online-Translator", str(icon_path) |
| 435 | ], capture_output=True, text=True) |
| 436 | |
| 437 | if result.returncode == 0: |
| 438 | print("โ
Created fallback text icon") |
| 439 | os.chmod(icon_path, 0o644) |
| 440 | return True |
| 441 | except: |
| 442 | pass |
| 443 | |
| 444 | # Final fallback: copy system icon |
| 445 | try: |
| 446 | subprocess.run([ |
| 447 | "cp", "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ApplicationsFolderIcon.icns", |
| 448 | str(icon_path) |
| 449 | ], capture_output=True) |
| 450 | os.chmod(icon_path, 0o644) |
| 451 | print("โ
Used system fallback icon") |
| 452 | return True |
| 453 | except: |
| 454 | pass |
| 455 | |
| 456 | return False |
| 457 | |
| 458 | except Exception as e: |
| 459 | print(f"โ Icon creation error: {e}") |
| 460 | return False |
| 461 | |
| 462 | def download_models(resources_dir): |
| 463 | """Download and setup required MLX models""" |
| 464 | print("\n๐ค Setting up MLX models...") |
| 465 | models_dir = resources_dir / "models" |
| 466 | models_dir.mkdir(exist_ok=True) |
| 467 | |
| 468 | # Define models to download |
| 469 | models = { |
| 470 | "whisper": { |
| 471 | "url": "https://huggingface.co/mlx-community/whisper-large-v3-mlx/resolve/main/whisper-large-v3-mlx.tar.gz", |
| 472 | "filename": "whisper-large-v3-mlx.tar.gz", |
| 473 | "extract_dir": "whisper" |
| 474 | }, |
| 475 | "translation": { |
| 476 | "url": "https://huggingface.co/mlx-community/opus-mt-en-es-mlx/resolve/main/opus-mt-en-es-mlx.tar.gz", |
| 477 | "filename": "opus-mt-en-es-mlx.tar.gz", |
| 478 | "extract_dir": "translation" |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | for model_name, model_info in models.items(): |
| 483 | print(f" ๐ฅ Downloading {model_name} model...") |
| 484 | model_path = models_dir / model_info["filename"] |
| 485 | extract_path = models_dir / model_info["extract_dir"] |
| 486 | |
| 487 | try: |
| 488 | # Download model |
| 489 | response = requests.get(model_info["url"], stream=True) |
| 490 | response.raise_for_status() |
| 491 | |
| 492 | total_size = int(response.headers.get('content-length', 0)) |
| 493 | block_size = 1024 # 1 Kibibyte |
| 494 | |
| 495 | with open(model_path, 'wb') as f: |
| 496 | for data in response.iter_content(block_size): |
| 497 | f.write(data) |
| 498 | |
| 499 | print(f" โ
Downloaded {model_name} model") |
| 500 | |
| 501 | # Extract model |
| 502 | print(f" ๐ฆ Extracting {model_name} model...") |
| 503 | with tarfile.open(model_path, 'r:gz') as tar: |
| 504 | tar.extractall(path=extract_path) |
| 505 | |
| 506 | # Clean up tar file |
| 507 | os.remove(model_path) |
| 508 | print(f" โ
Extracted {model_name} model") |
| 509 | |
| 510 | except Exception as e: |
| 511 | print(f" โ ๏ธ Error downloading/extracting {model_name} model: {e}") |
| 512 | print(f" ๐ก Please download the model manually from: {model_info['url']}") |
| 513 | return False |
| 514 | |
| 515 | print("โ
MLX models setup complete") |
| 516 | return True |
| 517 | |
| 518 | |
| 519 | def install_dependencies(resources_dir): |
| 520 | """Install required Python dependencies for Online-Translator""" |
| 521 | print("\n๐ฆ Setting up Python environment...") |
| 522 | venv_dir = resources_dir / "venv" |
| 523 | try: |
| 524 | # Create virtual environment |
| 525 | subprocess.run([ |
| 526 | sys.executable, "-m", "venv", str(venv_dir) |
| 527 | ], check=True, capture_output=True) |
| 528 | print(" โ
Virtual environment created") |
| 529 | |
| 530 | # Get the Python interpreter from the virtual environment |
| 531 | if platform.system() == "Darwin": # macOS |
| 532 | venv_python = venv_dir / "bin" / "python3" |
| 533 | else: |
| 534 | venv_python = venv_dir / "Scripts" / "python.exe" |
| 535 | |
| 536 | # Upgrade pip |
| 537 | subprocess.run([ |
| 538 | str(venv_python), "-m", "pip", "install", "--upgrade", "pip" |
| 539 | ], check=True, capture_output=True) |
| 540 | print(" โ
Pip upgraded") |
| 541 | |
| 542 | # Install required packages |
| 543 | required_packages = [ |
| 544 | "mlx", |
| 545 | "mlx-whisper", |
| 546 | "mlx-lm", |
| 547 | "numpy", |
| 548 | "langdetect", |
| 549 | "requests", |
| 550 | "sounddevice", |
| 551 | "pyaudio", |
| 552 | "pydub", |
| 553 | "tqdm", |
| 554 | "colorama" |
| 555 | ] |
| 556 | |
| 557 | print(" ๐ฆ Installing Python packages...") |
| 558 | for package in required_packages: |
| 559 | try: |
| 560 | subprocess.run([ |
| 561 | str(venv_python), "-m", "pip", "install", package |
| 562 | ], check=True, capture_output=True) |
| 563 | print(f" โ
Installed {package}") |
| 564 | except subprocess.CalledProcessError as e: |
| 565 | print(f" โ ๏ธ Failed to install {package}: {e}") |
| 566 | |
| 567 | # Install ffmpeg if not present |
| 568 | print(" ๐ฅ Checking ffmpeg...") |
| 569 | try: |
| 570 | subprocess.run(["ffmpeg", "-version"], check=True, capture_output=True) |
| 571 | print(" โ
ffmpeg is already installed") |
| 572 | except (subprocess.CalledProcessError, FileNotFoundError): |
| 573 | print(" ๐ฆ Installing ffmpeg...") |
| 574 | try: |
| 575 | # Try to install ffmpeg using Homebrew |
| 576 | subprocess.run(["brew", "install", "ffmpeg"], check=True, capture_output=True) |
| 577 | print(" โ
ffmpeg installed successfully") |
| 578 | except (subprocess.CalledProcessError, FileNotFoundError): |
| 579 | print(" โ ๏ธ Could not install ffmpeg automatically") |
| 580 | print(" ๐ก Please install ffmpeg manually: brew install ffmpeg") |
| 581 | |
| 582 | print("โ
Python environment setup complete") |
| 583 | return True |
| 584 | |
| 585 | except Exception as e: |
| 586 | print(f"โ ๏ธ Error setting up Python environment: {e}") |
| 587 | print("๐ก The application will try to install dependencies on first run") |
| 588 | return False |
| 589 | |
| 590 | def create_mac_app(): |
| 591 | """Create a Mac application bundle with improved icon and permission handling""" |
| 592 | |
| 593 | app_name = "Online-Translator" |
| 594 | app_bundle = f"{app_name}.app" |
| 595 | |
| 596 | # Remove existing app bundle if it exists |
| 597 | if os.path.exists(app_bundle): |
| 598 | shutil.rmtree(app_bundle) |
| 599 | print(f"๐๏ธ Removed existing {app_bundle}") |
| 600 | |
| 601 | # Create app bundle structure |
| 602 | contents_dir = Path(app_bundle) / "Contents" |
| 603 | macos_dir = contents_dir / "MacOS" |
| 604 | resources_dir = contents_dir / "Resources" |
| 605 | |
| 606 | contents_dir.mkdir(parents=True) |
| 607 | macos_dir.mkdir() |
| 608 | resources_dir.mkdir() |
| 609 | |
| 610 | # Set up icon |
| 611 | icon_success = False |
| 612 | try: |
| 613 | # Copy the icon file to Resources |
| 614 | icon_source = Path("media/logoCovi.png") |
| 615 | if icon_source.exists(): |
| 616 | # Create a temporary directory for icon conversion |
| 617 | temp_dir = tempfile.mkdtemp() |
| 618 | try: |
| 619 | temp_dir_path = Path(temp_dir) |
| 620 | |
| 621 | # Create iconset directory |
| 622 | iconset_dir = temp_dir_path / "AppIcon.iconset" |
| 623 | iconset_dir.mkdir() |
| 624 | |
| 625 | # Copy the PNG to the iconset directory |
| 626 | shutil.copy2(icon_source, iconset_dir / "icon_512x512.png") |
| 627 | |
| 628 | # Convert to ICNS |
| 629 | icon_dest = resources_dir / "AppIcon.icns" |
| 630 | result = subprocess.run([ |
| 631 | "iconutil", "-c", "icns", |
| 632 | "-o", str(icon_dest), |
| 633 | str(iconset_dir) |
| 634 | ], capture_output=True, text=True) |
| 635 | |
| 636 | if result.returncode == 0: |
| 637 | icon_success = True |
| 638 | print("โ
Application icon created successfully") |
| 639 | else: |
| 640 | print(f"โ ๏ธ Icon conversion failed: {result.stderr}") |
| 641 | finally: |
| 642 | # Clean up temporary directory |
| 643 | try: |
| 644 | shutil.rmtree(temp_dir) |
| 645 | except Exception as e: |
| 646 | print(f"๐ Note: Could not clean up temporary directory: {e}") |
| 647 | except Exception as e: |
| 648 | print(f"โ ๏ธ Error creating application icon: {e}") |
| 649 | icon_success = False |
| 650 | |
| 651 | # Create Info.plist with proper icon reference |
| 652 | info_plist = { |
| 653 | "CFBundleName": "Online-Translator", |
| 654 | "CFBundleDisplayName": "Online-Translator", |
| 655 | "CFBundleIdentifier": "com.online-translator.app", |
| 656 | "CFBundleVersion": "1.0", |
| 657 | "CFBundleShortVersionString": "1.0", |
| 658 | "CFBundlePackageType": "APPL", |
| 659 | "CFBundleSignature": "????", |
| 660 | "CFBundleExecutable": "Online-Translator", |
| 661 | "LSMinimumSystemVersion": "10.13.0", |
| 662 | "NSHighResolutionCapable": True, |
| 663 | "LSUIElement": True, # This makes it a background app |
| 664 | "NSMicrophoneUsageDescription": "Online-Translator needs access to your microphone to transcribe audio.", |
| 665 | "NSScreenCaptureUsageDescription": "Online-Translator needs screen capture access to capture system audio." |
| 666 | } |
| 667 | |
| 668 | # Only add icon reference if icon was created successfully |
| 669 | if icon_success: |
| 670 | info_plist["CFBundleIconFile"] = "AppIcon.icns" |
| 671 | |
| 672 | # Write Info.plist |
| 673 | with open(contents_dir / "Info.plist", "wb") as f: |
| 674 | plistlib.dump(info_plist, f) |
| 675 | print("โ
Info.plist created") |
| 676 | |
| 677 | # Create launcher script with better error handling |
| 678 | launcher_script = '''#!/bin/bash |
| 679 | # Launcher script for Online-Translator - Live Subtitles |
| 680 | # Created with โค๏ธ by Kiko Cisneros for his children |
| 681 | |
| 682 | # Exit on any error |
| 683 | set -e |
| 684 | |
| 685 | # Get the directory where this script is located |
| 686 | SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
| 687 | RESOURCES_DIR="$SCRIPT_DIR/../Resources" |
| 688 | VENV_DIR="$RESOURCES_DIR/venv" |
| 689 | MODELS_DIR="$RESOURCES_DIR/models" |
| 690 | |
| 691 | # Log file for debugging |
| 692 | LOG_FILE="/tmp/Online-Translator_launch.log" |
| 693 | echo "$(date): Online-Translator launch started" > "$LOG_FILE" |
| 694 | |
| 695 | # Check if Resources directory exists |
| 696 | if [ ! -d "$RESOURCES_DIR" ]; then |
| 697 | echo "$(date): ERROR - Resources directory not found: $RESOURCES_DIR" >> "$LOG_FILE" |
| 698 | osascript -e 'display alert "Online-Translator Error" message "Resources directory not found. Please reinstall Online-Translator." as critical' |
| 699 | exit 1 |
| 700 | fi |
| 701 | |
| 702 | # Check if models directory exists |
| 703 | if [ ! -d "$MODELS_DIR" ]; then |
| 704 | echo "$(date): ERROR - Models directory not found: $MODELS_DIR" >> "$LOG_FILE" |
| 705 | osascript -e 'display alert "Online-Translator Error" message "Models directory not found. Please reinstall Online-Translator." as critical' |
| 706 | exit 1 |
| 707 | fi |
| 708 | |
| 709 | # Set up environment |
| 710 | export PYTHONPATH="$RESOURCES_DIR:$PYTHONPATH" |
| 711 | export MLX_MODELS_DIR="$MODELS_DIR" |
| 712 | echo "$(date): PYTHONPATH set to: $PYTHONPATH" >> "$LOG_FILE" |
| 713 | echo "$(date): MLX_MODELS_DIR set to: $MLX_MODELS_DIR" >> "$LOG_FILE" |
| 714 | |
| 715 | # Check if virtual environment exists and activate it |
| 716 | if [ -f "$VENV_DIR/bin/activate" ]; then |
| 717 | echo "$(date): Activating virtual environment..." >> "$LOG_FILE" |
| 718 | source "$VENV_DIR/bin/activate" |
| 719 | PYTHON_CMD="$VENV_DIR/bin/python3" |
| 720 | else |
| 721 | echo "$(date): Virtual environment not found, using system Python" >> "$LOG_FILE" |
| 722 | # Find Python interpreter |
| 723 | PYTHON_CMD=$(which python3) |
| 724 | if [ -z "$PYTHON_CMD" ]; then |
| 725 | echo "$(date): ERROR - Python 3 not found" >> "$LOG_FILE" |
| 726 | osascript -e 'display alert "Python Not Found" message "Python 3 is required to run Online-Translator. Please install Python 3 from python.org" as critical' |
| 727 | exit 1 |
| 728 | fi |
| 729 | fi |
| 730 | |
| 731 | # Check if ffmpeg is available |
| 732 | if ! command -v ffmpeg &> /dev/null; then |
| 733 | echo "$(date): ffmpeg not found, attempting to install..." >> "$LOG_FILE" |
| 734 | if command -v brew &> /dev/null; then |
| 735 | brew install ffmpeg >> "$LOG_FILE" 2>&1 |
| 736 | else |
| 737 | echo "$(date): Homebrew not found, cannot install ffmpeg" >> "$LOG_FILE" |
| 738 | osascript -e 'display alert "ffmpeg Required" message "Online-Translator needs ffmpeg for audio capture. Please install it with: brew install ffmpeg" as warning' |
| 739 | fi |
| 740 | fi |
| 741 | |
| 742 | # Change to resources directory |
| 743 | cd "$RESOURCES_DIR" || { |
| 744 | echo "$(date): ERROR - Cannot change to resources directory" >> "$LOG_FILE" |
| 745 | osascript -e 'display alert "Online-Translator Error" message "Cannot access application resources. Please reinstall Online-Translator." as critical' |
| 746 | exit 1 |
| 747 | } |
| 748 | |
| 749 | # Check if main.py exists |
| 750 | if [ ! -f "main.py" ]; then |
| 751 | echo "$(date): ERROR - main.py not found in resources" >> "$LOG_FILE" |
| 752 | osascript -e 'display alert "Online-Translator Error" message "Main application file not found. Please reinstall Online-Translator." as critical' |
| 753 | exit 1 |
| 754 | fi |
| 755 | |
| 756 | echo "$(date): Starting Online-Translator application..." >> "$LOG_FILE" |
| 757 | |
| 758 | # Run the application |
| 759 | exec "$PYTHON_CMD" main.py |
| 760 | ''' |
| 761 | |
| 762 | # Write launcher script |
| 763 | launcher_path = macos_dir / "Online-Translator" |
| 764 | with open(launcher_path, 'w') as f: |
| 765 | f.write(launcher_script) |
| 766 | |
| 767 | # Make launcher executable with proper permissions |
| 768 | os.chmod(launcher_path, 0o755) |
| 769 | print("โ
Launcher script created") |
| 770 | |
| 771 | # Copy application files to Resources |
| 772 | files_to_copy = [ |
| 773 | 'main.py', |
| 774 | 'src/', |
| 775 | 'requirements.txt', |
| 776 | 'README.md' |
| 777 | ] |
| 778 | |
| 779 | print("๐ Copying application files...") |
| 780 | for file_path in files_to_copy: |
| 781 | if os.path.exists(file_path): |
| 782 | if os.path.isdir(file_path): |
| 783 | shutil.copytree(file_path, resources_dir / file_path) |
| 784 | print(f" โ
Copied directory: {file_path}") |
| 785 | else: |
| 786 | shutil.copy2(file_path, resources_dir / file_path) |
| 787 | print(f" โ
Copied file: {file_path}") |
| 788 | |
| 789 | # Install dependencies |
| 790 | if not install_dependencies(resources_dir): |
| 791 | print("โ ๏ธ Warning: Some dependencies could not be installed") |
| 792 | |
| 793 | # Download and setup models |
| 794 | if not download_models(resources_dir): |
| 795 | print("โ ๏ธ Warning: Some models could not be downloaded") |
| 796 | |
| 797 | # Set up macOS permissions |
| 798 | setup_macos_permissions(app_bundle) |
| 799 | |
| 800 | # Simple refresh without cache clearing - just restart Finder |
| 801 | print("๐ Refreshing Finder...") |
| 802 | try: |
| 803 | subprocess.run(["killall", "Finder"], capture_output=True) |
| 804 | print("โ
Finder refreshed") |
| 805 | except Exception as e: |
| 806 | print(f"๐ Finder refresh note: {e}") |
| 807 | |
| 808 | print("=" * 60) |
| 809 | print(f"๐ Online-Translator Mac application successfully created!") |
| 810 | print("=" * 60) |
| 811 | print(f"๐ฑ App Bundle: {app_bundle}") |
| 812 | print(f"๐ Launch Methods:") |
| 813 | print(f" 1. Terminal: ./{app_bundle}/Contents/MacOS/Online-Translator") |
| 814 | print(f" 2. Python: python3 main.py") |
| 815 | print(f" 3. Finder: Double-click {app_bundle}") |
| 816 | print() |
| 817 | if icon_success: |
| 818 | print("โ
Custom icon applied successfully!") |
| 819 | print() |
| 820 | print("๐ Read README.md for detailed guide") |
| 821 | print("โค๏ธ Created with love by Kiko Cisneros for his children") |
| 822 | print("=" * 60) |
| 823 | |
| 824 | return app_bundle |
| 825 | |
| 826 | if __name__ == "__main__": |
| 827 | create_mac_app() |