Skip to content

Instantly share code, notes, and snippets.

@marijnbent
Created October 9, 2023 20:57
Show Gist options
  • Save marijnbent/d43ed9674c0fcad39316518dd38d1f90 to your computer and use it in GitHub Desktop.
Save marijnbent/d43ed9674c0fcad39316518dd38d1f90 to your computer and use it in GitHub Desktop.
Copy Alfred clipboard history as list or save as CSV
import os
import sqlite3
import csv
import subprocess
def export_alfred_clipboard(db_path, export_as):
db_path_expanded = os.path.expanduser(db_path)
conn = sqlite3.connect(db_path_expanded)
cursor = conn.cursor()
query = "SELECT item FROM clipboard"
cursor.execute(query)
rows = cursor.fetchall()
data_list = [row[0] for row in rows]
conn.close()
if export_as == 'csv':
with open('clipboard.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['Clipboard Items'])
for item in data_list:
writer.writerow([item])
print('✅ Saved as clipboard.csv')
elif export_as == 'list':
subprocess.run("pbcopy", text=True, input=str(data_list))
print('✅ Copied to your clipboard.')
else:
print("Invalid export_as value. Use 'csv' or 'list'.")
if __name__ == "__main__":
export_as = input("How would you like to export the data? (list/csv): ").strip().lower()
if export_as not in ['list', 'csv']:
print("Invalid input. Use 'csv' or 'list'.")
else:
db_path = "~/Library/Application Support/Alfred/Databases/clipboard.alfdb"
export_alfred_clipboard(db_path, export_as)
@marijnbent
Copy link
Author

This Python script is designed to export clipboard data stored by the Alfred app on macOS. Specifically, it reads from Alfred's SQLite database and prints out the clipboard items as a list.

Key Components:

  1. SQLite Connection: Uses Python's sqlite3 library to connect to Alfred's clipboard database.

  2. Query Execution: Executes an SQL query to fetch all clipboard items.

  3. List Printing: Prints the clipboard items as a Python list.

Usage:

  • Just run the script. No arguments or user input are required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment