-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobstask.pyw
More file actions
523 lines (439 loc) · 17.9 KB
/
obstask.pyw
File metadata and controls
523 lines (439 loc) · 17.9 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
"""Obsidian To-Do Creator - GUI app for creating markdown notes."""
import ctypes
import os
import re
import tkinter as tk
from ctypes import wintypes
from datetime import datetime, timedelta, timezone
from pathlib import Path
from tkinter import messagebox, ttk
# Load environment variables from .env file
def load_env():
"""Load environment variables from .env file if it exists."""
env_file = Path(__file__).parent / '.env'
if env_file.exists():
with open(env_file, 'r') as f:
for line in f:
line = line.strip()
if line and not line.startswith('#') and '=' in line:
key, value = line.split('=', 1)
os.environ[key] = value
# Load environment variables at module import
load_env()
class ObsidianTodoCreator:
"""A GUI application for creating Obsidian markdown notes with scheduled dates."""
def __init__(self):
"""Initialize the Obsidian To-Do Creator application."""
self.root = tk.Tk()
self.root.title("Obsidian To-Do Creator")
# self.root.geometry("500x400") # Increased height for new field
self.root.resizable(width=True, height=True)
# Configuration - UPDATE THESE PATHS
self.vault_path = self._get_vault_path()
# Priority options
self.priority_options = [
"1-Today",
"2-ThisWeek",
"3-Soon",
"5-Someday",
"6-Waiting",
]
self.priority_var = tk.StringVar(value="1-Today") # Default to Today
# Calculate date options based on current date
self.date_options = self.calculate_date_options()
self.create_widgets()
# Enhanced entry field focus with multiple techniques
self._focus_title_entry()
def _get_vault_path(self):
"""Get the vault path from environment variable with fallback."""
vault_path = os.environ.get('OBSIDIANPATH')
if not vault_path:
messagebox.showerror(
"Configuration Error",
"VAULT_PATH environment variable not set.\n"
"Please create a .env file with OBSIDIANPATH=/path/to/your/vault/"
)
self.root.quit()
return None
# Ensure the path ends with a separator
if not vault_path.endswith(os.sep):
vault_path += os.sep
return vault_path
def _final_activation(self):
"""Robust final activation and focus attempt (works with pythonw)."""
try:
hwnd = int(self.root.winfo_id())
user32 = ctypes.windll.user32
kernel32 = ctypes.windll.kernel32
fore_hwnd = user32.GetForegroundWindow()
fore_thread = user32.GetWindowThreadProcessId(fore_hwnd, 0)
this_thread = kernel32.GetCurrentThreadId()
# Temporarily attach input to foreground thread
user32.AttachThreadInput(fore_thread, this_thread, True)
# Bring our window forward
user32.ShowWindow(hwnd, 9) # SW_RESTORE
user32.SetForegroundWindow(hwnd)
user32.SetFocus(hwnd)
# Detach
user32.AttachThreadInput(fore_thread, this_thread, False)
except Exception:
pass
# Tkinter fallback
self.root.lift()
self.root.focus_force()
if hasattr(self, "title_entry"):
self.title_entry.focus_set()
def _focus_title_entry(self):
"""Enhanced focus methods for title entry field."""
self.title_entry.focus_set()
self.title_entry.focus_force()
def calculate_date_options(self):
"""Calculate pre-defined date options based on current date."""
# Use Perth timezone (UTC+8) for local date calculations
perth_tz = timezone(timedelta(hours=8))
base_date = datetime.now(tz=perth_tz).replace(
hour=0,
minute=0,
second=0,
microsecond=0,
)
options = []
# Add specific dates (next 6 days)
for i in range(1, 7):
date = base_date + timedelta(days=i)
day_name = date.strftime("%A")
month_abbr = date.strftime("%b")
options.append(
f"{date.strftime('%Y-%m-%d')} ({day_name}, {month_abbr} {date.day})",
)
# Add "Next Week" (next Monday)
next_monday = base_date + timedelta(days=7)
options.append(
f"Next Week (Monday {next_monday.day} {next_monday.strftime('%b')})",
)
# Add "Next Month" (approximately 31 days)
next_month = base_date + timedelta(days=31)
options.append(
f"Next Month (Thursday {next_month.day} {next_month.strftime('%b')})",
)
return options
def on_date_focus_in(self, _event):
"""Clear placeholder text when combobox gets focus."""
if self.date_var.get() == "Select a date or type YYYY-MM-DD":
self.date_var.set("")
def on_date_selected(self, _event):
"""Handle date selection from dropdown."""
if self.date_var.get():
self.priority_var.set("6-Waiting")
else:
self.priority_var.set("1-Today") # Reset to default if date is cleared
# Placeholder for future enhancements
def on_date_changed(self, _event):
"""Update priority when date is selected."""
if self.date_var.get():
self.priority_var.set("6-Waiting")
else:
self.priority_var.set("1-Today") # Reset to default if date is cleared
def extract_date_from_selection(self, date_selection):
"""Extract the actual date (YYYY-MM-DD) from the formatted selection."""
if not date_selection:
return ""
# Check if it's a direct YYYY-MM-DD format
if re.match(r"^\d{4}-\d{2}-\d{2}$", date_selection):
return date_selection
# Extract date from formatted strings like "2025-08-19 (Tuesday, Aug 19)"
date_match = re.search(r"^(\d{4}-\d{2}-\d{2})", date_selection)
if date_match:
return date_match.group(1)
# Handle "Next Week" and "Next Month" options
if "Next Week" in date_selection:
# Calculate next Monday from current date
perth_tz = timezone(timedelta(hours=8))
base_date = datetime.now(tz=perth_tz).replace(
hour=0,
minute=0,
second=0,
microsecond=0,
)
next_monday = base_date + timedelta(days=7)
return next_monday.strftime("%Y-%m-%d")
if "Next Month" in date_selection:
# Calculate next month (approximately 31 days)
perth_tz = timezone(timedelta(hours=8))
base_date = datetime.now(tz=perth_tz).replace(
hour=0,
minute=0,
second=0,
microsecond=0,
)
next_month = base_date + timedelta(days=31)
return next_month.strftime("%Y-%m-%d")
return date_selection
def validate_date(self, date_string):
"""Validate date format (YYYY-MM-DD)."""
if not date_string:
return True # Empty date is optional
# Simple regex validation to avoid timezone issues
if not re.match(r"^\d{4}-\d{2}-\d{2}$", date_string):
return False
try:
# Just check if the date is valid without creating a datetime object
year, month, day = map(int, date_string.split("-"))
datetime(year, month, day, tzinfo=timezone.utc)
except ValueError:
return False
else:
return True
def parse_natural_date_to_yyyy_mm_dd(self, date_text):
"""Parse natural date words to a YYYY-MM-DD string.
Supports: "today", "tomorrow", "next week", or direct "YYYY-MM-DD".
Returns empty string if input isn't recognized/valid.
"""
if not date_text:
return ""
normalized = date_text.strip().lower()
# Perth timezone base date (midnight)
perth_tz = timezone(timedelta(hours=8))
base_date = datetime.now(tz=perth_tz).replace(
hour=0,
minute=0,
second=0,
microsecond=0,
)
if normalized == "today":
return base_date.strftime("%Y-%m-%d")
if normalized == "tomorrow":
return (base_date + timedelta(days=1)).strftime("%Y-%m-%d")
if normalized == "next week":
# Match existing app behavior: next Monday approximation (+7 days from base)
return (base_date + timedelta(days=7)).strftime("%Y-%m-%d")
# If it's already a valid YYYY-MM-DD
if self.validate_date(date_text):
return date_text
return ""
def parse_natural_language_title(self):
"""If title starts with priority like '1,', parse CSV to prefill fields.
Format: "<priority_digit>, <note title>, <next action>[, <scheduled>]"
"""
raw_title_input = self.title_var.get().strip()
if not raw_title_input:
return
# Must begin with one of the supported priority digits followed by a comma
if not re.match(r"^[12356]\s*,", raw_title_input):
return
# Split into at most 4 parts: priority, title, next, scheduled?
parts = [p.strip() for p in raw_title_input.split(",", 3)]
if len(parts) < 2:
return
priority_digit = parts[0]
priority_map = {
"1": "1-Today",
"2": "2-ThisWeek",
"3": "3-Soon",
"5": "5-Someday",
"6": "6-Waiting",
}
# Update priority
mapped_priority = priority_map.get(priority_digit)
if not mapped_priority:
return
self.priority_var.set(mapped_priority)
# Update title
note_title = parts[1]
self.title_var.set(note_title)
# Update next action if provided
if len(parts) >= 3 and parts[2]:
self.action_var.set(parts[2])
# Update scheduled date if provided and recognized
if len(parts) == 4 and parts[3]:
parsed_date = self.parse_natural_date_to_yyyy_mm_dd(parts[3])
if parsed_date:
self.date_var.set(parsed_date)
self.priority_var.set("6-Waiting")
def on_title_focus_out(self, _event):
"""Handle leaving the title field by attempting natural language parsing."""
self.parse_natural_language_title()
def create_widgets(self):
"""Create and configure the GUI widgets."""
# Main frame
main_frame = ttk.Frame(self.root, padding="20")
main_frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
# Title field
ttk.Label(
main_frame,
text="Note Title (natural: Priority, title, next, date): ",
).grid(
row=0,
column=0,
sticky=tk.W,
pady=(0, 5),
)
self.title_var = tk.StringVar()
self.title_entry = ttk.Entry(main_frame, textvariable=self.title_var, width=50)
self.title_entry.grid(
row=1,
column=0,
columnspan=2,
sticky=(tk.W, tk.E),
pady=(0, 15),
)
# Immediate focus attempt and delayed enhanced focus
self.title_entry.focus_set()
self._focus_title_entry()
# Parse natural language input when leaving the title field
self.title_entry.bind("<FocusOut>", self.on_title_focus_out)
# Next action field
ttk.Label(main_frame, text="Next Action:").grid(
row=2,
column=0,
sticky=tk.W,
pady=(0, 5),
)
self.action_var = tk.StringVar()
self.action_entry = ttk.Entry(
main_frame,
textvariable=self.action_var,
width=50,
)
self.action_entry.grid(
row=3,
column=0,
columnspan=2,
sticky=(tk.W, tk.E),
pady=(0, 15),
)
# Scheduled date field
ttk.Label(main_frame, text="Scheduled Date (optional - YYYY-MM-DD):").grid(
row=6,
column=0,
sticky=tk.W,
pady=(0, 5),
)
self.date_var = tk.StringVar()
self.date_combobox = ttk.Combobox(
main_frame,
textvariable=self.date_var,
values=self.date_options,
width=47,
)
self.date_combobox.grid(
row=7,
column=0,
columnspan=2,
sticky=(tk.W, tk.E),
pady=(0, 15),
)
# Allow manual entry in the combobox
self.date_combobox.configure(state="normal")
# Bind events to clear placeholder text and update priority
self.date_combobox.bind("<FocusIn>", self.on_date_focus_in)
self.date_combobox.bind("<<ComboboxSelected>>", self.on_date_selected)
self.date_combobox.bind("<KeyRelease>", self.on_date_changed)
# Priority field
ttk.Label(main_frame, text="Priority:").grid(
row=8,
column=0,
sticky=tk.W,
pady=(0, 5),
)
self.priority_frame = ttk.Frame(main_frame)
self.priority_frame.grid(
row=9,
column=0,
columnspan=2,
sticky=(tk.W, tk.E),
pady=(0, 15),
)
for i, option in enumerate(self.priority_options):
ttk.Radiobutton(
self.priority_frame,
text=option,
value=option,
variable=self.priority_var,
command=lambda opt=option: self.priority_var.set(opt),
).grid(row=0, column=i, sticky=tk.W)
# Buttons frame
button_frame = ttk.Frame(main_frame)
button_frame.grid(row=10, column=0, columnspan=2, pady=(10, 0))
ttk.Button(button_frame, text="Create Note", command=self.create_note).pack(
side=tk.LEFT,
padx=(0, 10),
)
ttk.Button(button_frame, text="Cancel", command=self.root.quit).pack(
side=tk.LEFT,
)
# Bind Enter key to create note
self.root.bind("<Return>", lambda _: self.create_note())
self.root.bind("<Escape>", lambda _: self.root.quit())
# Configure grid weights
self.root.columnconfigure(0, weight=1)
self.root.rowconfigure(0, weight=1)
main_frame.columnconfigure(0, weight=1)
def sanitize_filename(self, filename):
"""Remove or replace characters that aren't valid in filenames."""
# Replace invalid characters with underscores
filename = re.sub(r'[<>:"/\\|?*]', "_", filename)
# Remove any trailing periods or spaces
return filename.strip(". ")
def create_note(self):
"""Create a new Obsidian markdown note with the provided information."""
# Ensure any natural-language title input is parsed before validation
self.parse_natural_language_title()
title = self.title_var.get().strip()
action = self.action_var.get().strip()
priority = self.priority_var.get()
scheduled_date_selection = self.date_var.get().strip()
# Validate inputs
if not title:
messagebox.showerror("Error", "Note title is required!")
self.title_entry.focus()
return
if not action:
messagebox.showerror("Error", "Next action is required!")
self.action_entry.focus()
return
# Extract and validate the actual date
scheduled_date = self.extract_date_from_selection(scheduled_date_selection)
if scheduled_date and not self.validate_date(scheduled_date):
messagebox.showerror("Error", "Date must be in YYYY-MM-DD format!")
self.date_combobox.focus()
return
# Create full path
full_todo_path = Path(self.vault_path)
full_todo_path.mkdir(parents=True, exist_ok=True)
# Create filename
safe_filename = self.sanitize_filename(title)
filepath = full_todo_path / f"{safe_filename}.md"
# Check if file already exists
if filepath.exists() and not messagebox.askyesno(
"File Exists",
f"File '{safe_filename}.md' already exists. Overwrite?",
):
return
# Create frontmatter
frontmatter = "---\n"
frontmatter += f"Next: {action}\n"
if scheduled_date:
frontmatter += f"Scheduled: {scheduled_date}\n"
frontmatter += "---\n"
# Create note content
content = frontmatter + f"#Home - [[{priority}]]\n"
if scheduled_date:
content += "Scheduled: `INPUT[date:Scheduled]`\n"
content += "# Next: `INPUT[text(placeholder(''), class('my-mb-h1')):Next]`\n\n"
try:
with filepath.open("w", encoding="utf-8") as f:
f.write(content)
# Close the application on success
self.root.destroy()
except OSError as e:
messagebox.showerror("Error", f"Failed to create note:\n{e!s}")
def run(self):
"""Start the GUI application main loop."""
# Final activation attempt before starting mainloop
self.root.after(150, self._final_activation)
self.root.mainloop()
if __name__ == "__main__":
# Update the vault_path in the __init__ method before running
app = ObsidianTodoCreator()
app.run()