Created
October 9, 2023 20:57
-
-
Save marijnbent/d43ed9674c0fcad39316518dd38d1f90 to your computer and use it in GitHub Desktop.
Copy Alfred clipboard history as list or save as CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
SQLite Connection: Uses Python's
sqlite3
library to connect to Alfred's clipboard database.Query Execution: Executes an SQL query to fetch all clipboard items.
List Printing: Prints the clipboard items as a Python list.
Usage: