-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset-default-app.py
More file actions
executable file
·47 lines (35 loc) · 1.35 KB
/
set-default-app.py
File metadata and controls
executable file
·47 lines (35 loc) · 1.35 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
#!/usr/bin/python3
import objc
from AppKit import NSWorkspace
from Foundation import NSURL
workspace = NSWorkspace.sharedWorkspace()
def get_app_path(app_name: str) -> NSURL:
"""
Returns a URL (filesystem path prefixed with 'file://' scheme) to an
application specified by name, absolute path or bundle ID.
:param str app: name, absolute filesystem path or bundle ID to look up the URL for
:raises:
ApplicationNotFound: when no matching application was found
"""
path = workspace.fullPathForApplication_(app_name)
if path is None:
raise ValueError(
"Could not find an application named '{}'.".format(app_name)
)
return NSURL.fileURLWithPath_(path)
def set_default_scheme(scheme: str, app: str) -> None:
"""
Sets a default handler for a specific URL scheme.
:param str scheme: URL scheme to set the default handler for
:param str app: absolute filesystem path, name or bundle ID of the handler
"""
path = get_app_path(app)
workspace.setDefaultApplicationAtURL_toOpenURLsWithScheme_completionHandler_( # noqa: E501
path, scheme, objc.nil
)
if __name__ == '__main__':
import sys
scheme = sys.argv[4]
app = sys.argv[5]
print("Setting default handler for scheme {} to app {}".format(scheme, app)) # noqa: E501
set_default_scheme(scheme, app)