-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpicker.py
More file actions
78 lines (68 loc) · 2.3 KB
/
picker.py
File metadata and controls
78 lines (68 loc) · 2.3 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
import json
import sys
from tkinter import Tk, Toplevel, filedialog
"""
opts :=
title := <dialog title>
type := folder | file (default)
cwd := <cwd to open from>
filetypes := <file types to accept> (example: [["Images", "*.png *.jpg *.jpeg"]] )
multiple := True | False (allow multiple)
save := True | False ('save as' dialog, which lets the user select a file name)
initialfile := In case of "save=True", set the default filename
confirmoverwrite := True | False (if set to true, will warn if the selected file name exists, for save=True type dialogs)
"""
def pick(opts):
type_ = opts.get("type", "file")
cwd = opts.get("cwd", "")
title = opts.get("title", "Select")
filetypes = opts.get("filetypes", [("All Files", "*.*")])
multiple = opts.get("multiple", False)
must_exist = opts.get("must_exist", True)
save = opts.get("save", False)
initialfile = opts.get("initialfile", "")
confirmoverwrite = opts.get("confirmoverwrite", True)
root = Tk()
root.withdraw()
top = Toplevel()
top.attributes('-topmost', True)
top.geometry("1x1+500+300")
top.update()
options = {
"parent": top,
"initialdir": cwd,
"title": title
}
if type_ == "file":
options["filetypes"] = filetypes
if save:
options["initialfile"] = initialfile
options["confirmoverwrite"] = confirmoverwrite
path = filedialog.asksaveasfilename(**options)
elif multiple:
path = filedialog.askopenfilenames(**options)
else:
path = filedialog.askopenfilename(**options)
elif type_ == "folder":
options["mustexist"] = must_exist
path = filedialog.askdirectory(**options)
else:
raise ValueError(f"Unsupported type: {type_}")
top.destroy()
root.destroy()
# Normalize return
if isinstance(path, tuple):
path = list(path)
elif isinstance(path, str):
path = [path] if path else []
return path
def main():
try:
opts = json.load(sys.stdin)
paths = pick(opts)
print(json.dumps({"paths": paths}))
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)
if __name__ == "__main__":
main()