Skip to content

Commit 4b94d14

Browse files
authored
enh: add macos registration for idcbrowser:// protocol
1 parent 2e95e5e commit 4b94d14

File tree

1 file changed

+110
-2
lines changed

1 file changed

+110
-2
lines changed

IDCBrowser/IDCHandlerModule.py

Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import slicer
1818
import ctk
1919
from idc_index import index
20+
import sys
2021

2122
class IDCRequestHandler(BaseRequestHandler):
2223

@@ -222,7 +223,114 @@ def registerCustomProtocol(self):
222223

223224
print("IDC Browser URL protocol has been registered on Windows.")
224225
except Exception as e:
225-
print(f"Failed to register IDC Browser URL protocol on Windows: {e}")
226+
print(f"Failed to register IDC Browser URL protocol on Windows: {e}")
227+
228+
elif platform.system() == "Darwin":
229+
slicer_exec_dir = os.path.dirname(sys.executable)
230+
231+
# Construct the path to PythonSlicer.exe in the same directory
232+
python_path = os.path.join(slicer_exec_dir, "bin", "PythonSlicer")
233+
234+
current_dir = os.path.dirname(os.path.realpath(__file__))
235+
python_script_path = os.path.join(current_dir,'Resources', 'resolver.py')
236+
237+
def check_macos_slicer_protocol_registration():
238+
plist_path = os.path.expanduser("/Applications/slicer-app.app/Contents/Info.plist")
239+
return os.path.exists(plist_path)
240+
241+
if check_macos_slicer_protocol_registration():
242+
print("Slicer URL protocol is already registered.")
243+
return
244+
245+
# Create the AppleScript
246+
applescript_path = os.path.expanduser("~/slicer.applescript")
247+
with open(applescript_path, "w") as applescript_file:
248+
applescript_file.write(f"""
249+
on open location this_URL
250+
do shell script "{python_path} {python_script_path} " & quoted form of this_URL
251+
end open location
252+
""")
253+
254+
255+
# Compile the AppleScript into an app
256+
os.system(f"osacompile -o /Applications/slicer-app.app {applescript_path}")
257+
258+
259+
# Create or modify the plist file
260+
plist_content = """
261+
<?xml version="1.0" encoding="UTF-8"?>
262+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
263+
<plist version="1.0">
264+
<dict>
265+
<key>CFBundleAllowMixedLocalizations</key>
266+
<true/>
267+
<key>CFBundleDevelopmentRegion</key>
268+
<string>en</string>
269+
<key>CFBundleExecutable</key>
270+
<string>applet</string>
271+
<key>CFBundleIconFile</key>
272+
<string>applet</string>
273+
<key>CFBundleInfoDictionaryVersion</key>
274+
<string>6.0</string>
275+
<key>CFBundleName</key>
276+
<string>slicer-app</string>
277+
<key>CFBundlePackageType</key>
278+
<string>APPL</string>
279+
<key>CFBundleSignature</key>
280+
<string>aplt</string>
281+
<key>LSMinimumSystemVersionByArchitecture</key>
282+
<dict>
283+
<key>x86_64</key>
284+
<string>10.6</string>
285+
</dict>
286+
<key>LSRequiresCarbon</key>
287+
<true/>
288+
<key>NSAppleEventsUsageDescription</key>
289+
<string>This script needs to control other applications to run.</string>
290+
<key>NSAppleMusicUsageDescription</key>
291+
<string>This script needs access to your music to run.</string>
292+
<key>NSCalendarsUsageDescription</key>
293+
<string>This script needs access to your calendars to run.</string>
294+
<key>NSCameraUsageDescription</key>
295+
<string>This script needs access to your camera to run.</string>
296+
<key>NSContactsUsageDescription</key>
297+
<string>This script needs access to your contacts to run.</string>
298+
<key>NSHomeKitUsageDescription</key>
299+
<string>This script needs access to your HomeKit Home to run.</string>
300+
<key>NSMicrophoneUsageDescription</key>
301+
<string>This script needs access to your microphone to run.</string>
302+
<key>NSPhotoLibraryUsageDescription</key>
303+
<string>This script needs access to your photos to run.</string>
304+
<key>NSRemindersUsageDescription</key>
305+
<string>This script needs access to your reminders to run.</string>
306+
<key>NSSiriUsageDescription</key>
307+
<string>This script needs access to Siri to run.</string>
308+
<key>NSSystemAdministrationUsageDescription</key>
309+
<string>This script needs access to administer this system to run.</string>
310+
<key>OSAAppletShowStartupScreen</key>
311+
<false/>
312+
<key>CFBundleIdentifier</key>
313+
<string>slicer.protocol.registration</string>
314+
<key>CFBundleURLTypes</key>
315+
<array>
316+
<dict>
317+
<key>CFBundleURLName</key>
318+
<string>idcbrowser</string>
319+
<key>CFBundleURLSchemes</key>
320+
<array>
321+
<string>idcbrowser</string>
322+
</array>
323+
</dict>
324+
</array>
325+
</dict>
326+
</plist>
327+
"""
328+
329+
plist_path = os.path.expanduser("/Applications/slicer-app.app/Contents/Info.plist")
330+
with open(plist_path, "w") as plist_file:
331+
plist_file.write(plist_content)
332+
333+
print("Slicer URL protocol registered successfully.")
334+
226335
else:
227336
print("IDC Browser URL protocol registration is not supported on this operating system.")
228-

0 commit comments

Comments
 (0)