forked from petertzy/markdown-reader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
52 lines (40 loc) · 1.66 KB
/
app.py
File metadata and controls
52 lines (40 loc) · 1.66 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
import tkinter as tk
from markdown_reader.ui import MarkdownReader
import sys
import os
import ttkbootstrap as ttkb
from ttkbootstrap.constants import *
def handle_open_file(event):
"""
Handles file open events from macOS.
:param event event: The open file event from macOS.
"""
file_path = event
if os.path.isfile(file_path) and file_path.lower().endswith(('.md', '.markdown', '.html', '.htm', '.pdf')):
app.load_file(file_path)
if __name__ == "__main__":
try:
from tkinterdnd2 import TkinterDnD
root = TkinterDnD.Tk()
# Apply ttkbootstrap theme to TkinterDnD window
app_style = ttkb.Style(theme="darkly")
print("TkinterDnD enabled - Drag and drop support available")
except ImportError as e:
print(f" Warning: tkinterdnd2 not installed, drag-and-drop will be disabled")
print(f" Error: {e}")
root = ttkb.Window(themename="darkly")
app = MarkdownReader(root)
# Handle file open events from macOS Finder
root.createcommand("::tk::mac::OpenDocument", lambda *args: handle_open_file(args[0]))
# Handle file opening from command line
if len(sys.argv) > 1:
for arg in sys.argv[1:]:
# Skip macOS system parameters (process serial number)
if arg.startswith('-psn'):
continue
# Convert to absolute path
file_path = os.path.abspath(arg)
if os.path.isfile(file_path) and file_path.lower().endswith(('.md', '.markdown', '.html', '.htm', '.pdf')):
app.load_file(file_path)
break # Only open the first file
root.mainloop()