forked from maragall/ndviewer_light
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcreate_shortcut.py
More file actions
172 lines (144 loc) · 4.96 KB
/
Copy pathcreate_shortcut.py
File metadata and controls
172 lines (144 loc) · 4.96 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
171
172
#!/usr/bin/env python3
"""Create a desktop shortcut for NDViewer Light."""
import importlib.metadata
import os
import platform
import shutil
import subprocess
import sys
from pathlib import Path
APP_NAME = "NDViewer Light"
COMMAND_NAME = "ndviewer-light"
MODULE_NAME = "ndviewer_light"
BUNDLE_ID = "com.cephla.ndviewer-light"
def _get_version():
try:
return importlib.metadata.version(MODULE_NAME)
except importlib.metadata.PackageNotFoundError:
return "0.0.0"
def _resolve_launch_command():
"""Return (exe_path, None) if installed, else (python_path, module_name)."""
exe = shutil.which(COMMAND_NAME)
if exe:
return exe, None
return sys.executable, MODULE_NAME
def create_macos_app(exe, module):
app_dir = Path.home() / "Applications" / f"{APP_NAME}.app"
contents = app_dir / "Contents"
macos = contents / "MacOS"
macos.mkdir(parents=True, exist_ok=True)
(contents / "Resources").mkdir(parents=True, exist_ok=True)
if module:
launch_cmd = f'exec "{exe}" -m {module} "$@"'
else:
launch_cmd = f'exec "{exe}" "$@"'
# Source user's shell profile so PATH includes conda/venv/pip environments
launcher = macos / COMMAND_NAME
launcher.write_text(
"#!/bin/bash\n"
'[ -f "$HOME/.bash_profile" ] && source "$HOME/.bash_profile"\n'
'[ -f "$HOME/.zprofile" ] && source "$HOME/.zprofile"\n'
'[ -f "$HOME/.zshrc" ] && source "$HOME/.zshrc"\n'
f"{launch_cmd}\n"
)
os.chmod(launcher, 0o755)
version = _get_version()
plist = contents / "Info.plist"
plist.write_text(
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>{version}</string>
<key>CFBundleExecutable</key>
<string>{COMMAND_NAME}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>LSMinimumSystemVersion</key>
<string>10.15</string>
</dict>
</plist>
"""
)
print(f"Created: {app_dir}")
print("You can drag it to the Dock from ~/Applications.")
def _ps_escape(value):
"""Escape a string for safe interpolation into a PowerShell double-quoted string."""
return str(value).replace("`", "``").replace('"', '`"').replace("$", "`$")
def create_windows_shortcut(exe, module):
args = f"-m {module}" if module else ""
desktop = Path.home() / "Desktop"
shortcut_path = desktop / f"{APP_NAME}.lnk"
ps_script = (
"$ws = New-Object -ComObject WScript.Shell\n"
f'$sc = $ws.CreateShortcut("{_ps_escape(shortcut_path)}")\n'
f'$sc.TargetPath = "{_ps_escape(exe)}"\n'
f"$sc.Arguments = '{args}'\n"
f'$sc.WorkingDirectory = "{_ps_escape(Path.home())}"\n'
f'$sc.Description = "{APP_NAME} - 5D Image Viewer"\n'
"$sc.Save()\n"
)
subprocess.run(
["powershell", "-NoProfile", "-Command", ps_script],
check=True,
)
print(f"Created: {shortcut_path}")
def create_linux_desktop_entry(exe, module):
if module:
exec_line = f"{exe} -m {module}"
else:
exec_line = exe
entry_content = (
"[Desktop Entry]\n"
f"Name={APP_NAME}\n"
f"Exec={exec_line}\n"
"Type=Application\n"
"Terminal=false\n"
f"Comment={APP_NAME} - 5D Image Viewer\n"
"Categories=Science;ImageProcessing;\n"
)
# Add to application menu
apps_dir = Path.home() / ".local" / "share" / "applications"
apps_dir.mkdir(parents=True, exist_ok=True)
apps_path = apps_dir / "ndviewer-light.desktop"
apps_path.write_text(entry_content)
os.chmod(apps_path, 0o755)
print(f"Created: {apps_path}")
# Add to desktop
desktop_dir = Path.home() / "Desktop"
if desktop_dir.is_dir():
desktop_path = desktop_dir / "ndviewer-light.desktop"
desktop_path.write_text(entry_content)
os.chmod(desktop_path, 0o755)
# Mark as trusted on GNOME (Ubuntu); skip if gio is unavailable
try:
subprocess.run(
["gio", "set", str(desktop_path), "metadata::trusted", "true"],
capture_output=True,
)
except FileNotFoundError:
pass
print(f"Created: {desktop_path}")
def main():
exe, module = _resolve_launch_command()
system = platform.system()
if system == "Darwin":
create_macos_app(exe, module)
elif system == "Windows":
create_windows_shortcut(exe, module)
elif system == "Linux":
create_linux_desktop_entry(exe, module)
else:
print(f"Desktop shortcut creation is not supported on {system}.")
print(f"You can run the viewer with: {COMMAND_NAME}")
sys.exit(1)
if __name__ == "__main__":
main()