Jump to content

[SOLVED] How to write the output of snippets to a text file and quickview it?


Recommended Posts

There are some ways to create and use the snippets in Alfred.

 

1. Using snip command . 

We can only see the command name, and copy the snippets but cant see the contents.

 

2. Using clipboard manager

we can search and see the contents of snippets and copy it, but can not see the preview (eg. using shift key)

 

Here, I would like to get help creating a workflow which will write the snippets to a file and quickview it.

 

idea:

command:   qsnip hello

effect: copies the snippet called hello and paste the contents "hello world" to a file called "mysnip.txt" in the directory of workflow.

effect: qlmanage -p mysnip.txt

 

Problem:

I am unware how to save the snippet to the file "mysnip.txt"

 

I have shared the minimal workflow here:

https://github.com/bhishanpdl/Shared/blob/master/Alfred_questions/qsnip.alfredworkflow?raw=true

 

 

Edited by Bhishan
Link to post
19 hours ago, Bhishan said:

I am unware how to save the snippet to the file "mysnip.txt"

 

Start by extracting them from the snippets.alfdb (SQLite3) file they're stored in.


I'm not sure I'd bother saving them to a file and using Quick Look. It's Alfred's Large Type no good?

Link to post

@vitor Thanks a lot. However, I also created workflow using python, following the idea of @deanishe.

 

import sys
import sqlite3
import os

db= r"/Users/USERNAME/Library/Application Support/Alfred 3/Databases/snippets.alfdb"
conn = sqlite3.connect(db)
q = "SELECT snippet FROM snippets WHERE keyword='{} '".format(sys.argv[1])
c = conn.cursor()
c.execute(q)
ans = c.fetchone()[0]
with open('snips.txt','w') as fo:
    fo.write(ans)

os.system('qlmanage -p snips.txt')

 

Edited by Bhishan
Link to post

Just a couple of Python tips:

 

It's a good idea to use os.expanduser('~/Library/path/to/something'). Then it Just Works for every user on every Mac.

q = "SELECT snippet FROM snippets WHERE keyword='{} '".format(sys.argv[1])

Perhaps you already know this, but you should never insert user input directly into SQL. You've basically just demonstrated exactly how SQL injection security holes are made.


It doesn't matter here, as the only person who can abuse it is you, and the only data you can ruin or delete are your own, but generally do not ever do this.

 

 

Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...