Skip to content

Instantly share code, notes, and snippets.

@luckman212
Created May 18, 2022 21:50
Show Gist options
  • Save luckman212/2d87efeb09cce7058ea42eee846effa3 to your computer and use it in GitHub Desktop.
Save luckman212/2d87efeb09cce7058ea42eee846effa3 to your computer and use it in GitHub Desktop.
python script to pop the top row off Alfred's sqlite clipboard database
#!/usr/bin/env python3
import sqlite3
import os
import sys
import inspect
from contextlib import contextmanager
home_path = os.getenv('HOME')
db_path = os.getenv('db_path') or 'Library/Application Support/Alfred/Databases/'
db_name = os.getenv('db_name') or 'clipboard.alfdb'
db_path = os.path.join(home_path, db_path, db_name)
@contextmanager
def db_ops(db_name):
conn = sqlite3.connect(db_name)
cur = conn.cursor()
yield cur
conn.commit()
conn.close()
def no_bueno(e='?'):
print(f'error deleting clip from db: {e}', file=sys.stderr)
sys.exit(1)
def get_last_clip():
with db_ops(db_path) as cur:
qry = f'SELECT rowid from clipboard WHERE dataType = 0 ORDER BY rowid DESC LIMIT 1;'
try:
res = cur.execute(qry).fetchone()
return res[0]
except:
no_bueno(inspect.stack()[0][3])
def del_clip(rowid):
if not isinstance(rowid, int) or rowid is None:
no_bueno(inspect.stack()[0][3])
with db_ops(db_path) as cur:
qry = f'DELETE FROM clipboard WHERE rowid = {rowid};'
try:
res = cur.execute(qry).rowcount
print(f'deleted row: {rowid}', file=sys.stderr)
except:
no_bueno(inspect.stack()[0][3])
if __name__ == '__main__':
rowid = get_last_clip()
del_clip(rowid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment