Skip to content

Instantly share code, notes, and snippets.

@paul-hammant
Created August 5, 2020 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paul-hammant/39afd8338a220802d46566d2640ab202 to your computer and use it in GitHub Desktop.
Save paul-hammant/39afd8338a220802d46566d2640ab202 to your computer and use it in GitHub Desktop.
import tkinter
from tkinter import messagebox
import platform
import os
import subprocess
import sys
import datetime
import sqlite3
from os.path import expanduser
parent = tkinter.Tk()
parent.overrideredirect(1) # Avoid it appearing and then disappearing quickly
parent.withdraw() # Hide the window as we do not want to see this one
home = expanduser("~")
def app_to_front(parent):
parent.attributes("-topmost", True)
# Hack because some MacOS tkinter dialogs don't open frontmost :(
if platform.system() == 'Darwin':
tmpl = 'tell application "System Events" to set frontmost of every process whose unix id is {} to true'
script = tmpl.format(os.getpid())
output = subprocess.check_call(['/usr/bin/osascript', '-e', script])
parent.after(0, lambda: parent.attributes("-topmost", False))
app_to_front(parent)
def create_table():
try:
c.execute("""CREATE TABLE to_delete
(name, if_after timestamp)""")
except:
pass
def insert_row(name, if_after):
c.execute("INSERT OR REPLACE INTO to_delete (name, if_after) values(?,?)", (name, if_after))
def delete_those_old_enough():
removed = []
sql = "SELECT name, if_after FROM to_delete"
recs = c.execute(sql)
for row in recs:
if datetime.datetime.now() > row[1]:
fname = row[0]
if os.path.exists(fname):
print("delete in FS ", fname)
os.remove(fname)
removed.append(fname)
for fname in removed:
print("deleting from table ", fname)
c.execute('DELETE FROM to_delete WHERE name=?', (fname,))
conn.commit()
conn = sqlite3.connect(home + "/.future_deletes.db", detect_types=sqlite3.PARSE_DECLTYPES)
c = conn.cursor()
create_table()
del sys.argv[0]
for fname in sys.argv:
if os.path.exists(fname):
msgbox_choice = tkinter.messagebox.askquestion('Future File Deletion', 'Delete ' + fname + ' in 24 hours?',
icon='warning')
if msgbox_choice == 'yes':
insert_row(fname, datetime.datetime.now() + datetime.timedelta(days=1))
conn.commit() # commit needed
parent.destroy()
delete_those_old_enough()
c.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment