-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_app.py
More file actions
170 lines (147 loc) · 5.61 KB
/
Copy pathbuild_app.py
File metadata and controls
170 lines (147 loc) · 5.61 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python3
"""Build a macOS .app bundle for Usage Inspector."""
import os
import stat
import shutil
import subprocess
from pathlib import Path
APP_DIR = Path(__file__).parent.resolve()
APP_NAME = "Usage Inspector"
BUNDLE_ID = "com.usage-inspector.app"
# App bundle structure
APP_BUNDLE = APP_DIR / f"{APP_NAME}.app"
CONTENTS = APP_BUNDLE / "Contents"
MACOS = CONTENTS / "MacOS"
RESOURCES = CONTENTS / "Resources"
def create_icns(png_path, icns_path):
"""Convert PNG to ICNS format."""
iconset_path = png_path.parent / "AppIcon.iconset"
iconset_path.mkdir(exist_ok=True)
# First create a square 1024x1024 base image
square_path = png_path.parent / "icon_square.png"
subprocess.run([
"sips", "-Z", "1024", "--padToHeightWidth", "1024", "1024",
str(png_path), "--out", str(square_path)
], capture_output=True)
# Icon sizes needed for macOS
sizes = [16, 32, 128, 256, 512]
for size in sizes:
# Standard resolution
out_file = iconset_path / f"icon_{size}x{size}.png"
subprocess.run([
"sips", "-z", str(size), str(size),
str(square_path), "--out", str(out_file)
], capture_output=True)
# Retina resolution
retina_size = size * 2
if retina_size <= 1024:
out_file = iconset_path / f"icon_{size}x{size}@2x.png"
subprocess.run([
"sips", "-z", str(retina_size), str(retina_size),
str(square_path), "--out", str(out_file)
], capture_output=True)
# Convert iconset to icns
subprocess.run(["iconutil", "-c", "icns", str(iconset_path), "-o", str(icns_path)])
# Cleanup
if square_path.exists():
square_path.unlink()
# Cleanup iconset
shutil.rmtree(iconset_path)
def create_bundle():
# Clean existing
if APP_BUNDLE.exists():
shutil.rmtree(APP_BUNDLE)
# Create directories
MACOS.mkdir(parents=True)
RESOURCES.mkdir(parents=True)
# Create app icon
logo_path = APP_DIR / "logo.png"
if logo_path.exists():
icns_path = RESOURCES / "AppIcon.icns"
create_icns(logo_path, icns_path)
print("Created app icon")
# Create Info.plist
info_plist = f"""<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleName</key>
<string>{APP_NAME}</string>
<key>CFBundleDisplayName</key>
<string>{APP_NAME}</string>
<key>CFBundleIdentifier</key>
<string>{BUNDLE_ID}</string>
<key>CFBundleVersion</key>
<string>0.3.0</string>
<key>CFBundleShortVersionString</key>
<string>0.3.0</string>
<key>CFBundleExecutable</key>
<string>launcher</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>LSMinimumSystemVersion</key>
<string>10.15</string>
<key>LSUIElement</key>
<true/>
<key>NSHighResolutionCapable</key>
<true/>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>CFBundleIconFile</key>
<string>AppIcon</string>
</dict>
</plist>
"""
(CONTENTS / "Info.plist").write_text(info_plist)
# Create launcher script
launcher_script = f"""#!/bin/bash
# Usage Inspector Launcher
# This script sets up the environment and runs the menu bar app
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
APP_DIR="$(cd "$SCRIPT_DIR/../Resources" && pwd)"
VENV_DIR="$APP_DIR/.venv"
PYTHON="$VENV_DIR/bin/python"
MAIN_SCRIPT="$APP_DIR/usage_inspector.py"
# First run: setup venv with uv
if [ ! -f "$PYTHON" ]; then
# Find uv in common locations (PATH is minimal when launched from Finder)
UV=""
for candidate in uv "$HOME/.local/bin/uv" "$HOME/.cargo/bin/uv" /usr/local/bin/uv /opt/homebrew/bin/uv; do
if "$candidate" --version &> /dev/null 2>&1; then
UV="$candidate"
break
fi
done
if [ -z "$UV" ]; then
osascript -e 'display dialog "uv is not installed.\\n\\nInstall it with:\\ncurl -LsSf https://astral.sh/uv/install.sh | sh" buttons {{"OK"}} default button "OK" with icon stop with title "Usage Inspector"'
exit 1
fi
# Create venv
cd "$APP_DIR"
LOG="$APP_DIR/.setup.log"
if ! "$UV" venv "$VENV_DIR" >>"$LOG" 2>&1; then
osascript -e 'display dialog "Failed to create virtual environment.\\n\\nSee .setup.log for details." buttons {{"OK"}} default button "OK" with icon stop with title "Usage Inspector"'
exit 1
fi
if ! VIRTUAL_ENV="$VENV_DIR" "$UV" pip install rumps pyobjc-framework-Cocoa Pillow >>"$LOG" 2>&1; then
osascript -e 'display dialog "Failed to install dependencies.\\n\\nSee .setup.log for details." buttons {{"OK"}} default button "OK" with icon stop with title "Usage Inspector"'
exit 1
fi
fi
# Run the app (don't exec — stay alive so we can catch crashes)
"$PYTHON" "$MAIN_SCRIPT" 2>"$APP_DIR/.run.log"
EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]; then
osascript -e 'display dialog "Usage Inspector crashed on startup.\\n\\nSee .run.log for details." buttons {{"OK"}} default button "OK" with icon stop with title "Usage Inspector"'
fi
"""
launcher_path = MACOS / "launcher"
launcher_path.write_text(launcher_script)
launcher_path.chmod(launcher_path.stat().st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
# Copy main script into bundle
shutil.copy2(APP_DIR / "usage_inspector.py", RESOURCES / "usage_inspector.py")
print(f"Created: {APP_BUNDLE}")
print(f"\nTo run: open '{APP_BUNDLE}'")
print(f"Or double-click '{APP_NAME}.app' in Finder")
if __name__ == "__main__":
create_bundle()