Jump to content

Snippet Creator - updated to use Universal Actions


Recommended Posts

Instantly create a new Alfred snippet from selected text in MacOS.

 

See 

 for the background.

 

Snippet Creator

adapted from https://github.com/derickfay/import-alfred-snippets

 

There are two keywords, both of which result in the creation of a new snippet:

 

sc - (single step) Required fields: name|keyword|content -- separated by |

sc2 (multi step) - Enter the snippet text, then you'll be prompted for the keyword / name -- in the interest of speed and simplicity the name is set to the keyword.

 

sc2 also has triggers to pre-population with selected text or clipboard contents, and is also available as a Universal Action.

 

set prefsPath in the environment variables to the path to your snippets folder - e.g. "/Users/someuser/Dropbox/Sync/Alfred 4/Alfred.alfredpreferences/snippets/"

and newSnippetFolder to your desired target folder

 

The workflow posts a notification on success to remind you of your newly created snippet.

 

https://www.dropbox.com/s/85nc52o241qobvm/snippet creator.alfredworkflow?dl=0

 

Version 0.3 (2021-08-04):

- updated to use Universal Actions

 

Version 0.2:

- updated to use a pipe | as a delimiter instead of a comma, and to remove the requirement to enclose text in quotes 

 

Version 0.1:

 initial

 

 

Edited by dfay
Link to comment

Thanks so much for making this. As a non-programmer, I need a little guidance on how to set the prefsPath's correctly. I'm assuming this refers to the text next to the prefsPath/prefsFolder bit of the python script. I believe I should set these to the Alfred snippets folder and the subfolder in that folder the snippets should be categorized under, is that right?

I keep my snippets in iCloud Drive, I looked up how to write out the path for that but got a little confused - finder treats iCloud Drive as the root, but I'm guessing it really isn't...

Link to comment

These are in the environment variables for the workflow.  You can set them through the GUI without editing the script - you click the [x] icon in the top right of the workflow screen, and a panel will drop down where you can set them.

 

I believe the devs recommend against using iCloud Drive for Alfred's preferences.  But in any case you should be able to go to your Alfred.alfredpreferences file location (as set up in Settings > Advanced > Syncing > Set preferences folder... ) then add /snippets/ to the end of that path.  

 

 

 

 

Link to comment
2 hours ago, dfay said:

I believe the devs recommend against using iCloud Drive for Alfred's preferences

 

It's always a case of hoping Apple don't make invisible changes that affect iCloud, but at this time, if you disable Optimised Storage (https://support.apple.com/en-us/HT207236) on your Macs, it may be sufficiently reliable for syncing. We always recommend keeping an up to date Time Machine as a backup :)

 

Cheers,
Vero

Link to comment
On 10/3/2019 at 10:11 PM, dfay said:

See 

 for the background.

 

Snippet Creator

adapted from https://github.com/derickfay/import-alfred-snippets

 

There are two keywords, both of which result in the creation of a new snippet:

 

sc - (single step) Required fields: 'name','keyword','content' -- all of these must be in single quotes and comma separated

sc2 (multi step) - Enter the snippet text, then you'll be prompted for the keyword / name -- in the interest of speed and simplicity the name is set to the keyword.

sc2 also has triggers to pre-population with selected text or clipboard contents

 

set prefsPath in the environment variables to the path to your snippets folder - e.g. "/Users/someuser/Dropbox/Sync/Alfred 4/Alfred.alfredpreferences/snippets/"

and newSnippetFolder to your desired target folder

 

The workflow posts a notification on success to remind you of your newly created snippet.

The presence of a single dumb quote (i.e. ' but not ‘ ) will cause it to fail silently.  So if you're going to be creating a lot of code snippets this probably isn't for you.  Unless you feel like sorting out how to escape everything that might cause trouble in which case I'd be grateful.

 

Version 0.1: https://www.dropbox.com/s/85nc52o241qobvm/snippet creator.alfredworkflow?dl=0

 

It would be good to be able to choose between the different target folders; Thank you

 

Link to comment

I was focused on speed of entry hence the fixed folder.  Otherwise there’s either a fourth string to include in the text or an additional entry box.  By that point you might as well open preferences and do it natively.  It might be possible with a file filter, though - let me look.

Edited by dfay
Link to comment
19 hours ago, dfay said:

Estaba enfocado en la velocidad de entrada, de ahí la carpeta fija. De lo contrario, hay una cuarta cadena para incluir en el texto o un cuadro de entrada adicional. En ese punto, también podría abrir las preferencias y hacerlo de forma nativa. Sin embargo, podría ser posible con un filtro de archivos: déjame mirar.

Thank you very much. I don't know anything about computers (I'm a lawyer) but I love Alfred and his workflows that I use regularly. I thank all the developers for the selfless work you do. Again, thank you very much.

 

Link to comment
  • 5 weeks later...

Hi there,

 

I've tried to enable this a few times with no luck. I tried to move my preferences folder to my HD rather than iCloud but no luck. Can anyone advise on how to set the variables? I think maybe I am writing the wrong path for the snippets folder...

Link to comment
  • 5 months later...
  • dfay changed the title to Snippet Creator - updated to use Universal Actions
  • 1 year later...

Hi @dfay thanks for the script. I downloaded it and tried to run it, and got an error.

 

I ran `sc test|test|test` in alfred dialog, and got the following logs in workflow debug:

 

[20:47:00.551] Logging Started...
[20:47:06.044] Snippet Creator[Keyword] Processing complete
[20:47:06.045] Snippet Creator[Keyword] Passing output 'test|test|test' to Run Script
[20:47:06.104] ERROR: Snippet Creator[Run Script] Traceback (most recent call last):
  File "<string>", line 22, in <module>
IOError: [Errno 2] No such file or directory: 'Created/test [7002563bc740875ed3fa914d3cb4d8].json'
[20:47:06.108] Snippet Creator[Run Script] Processing complete
[20:47:06.109] Snippet Creator[Run Script] Passing output '' to Post Notification

 

It looks like one directory name is wrong.

I'm using macos Monterey and Alfred 4.

 

Would you be able to fix the issue or to review a pull request that fixes the issue?

 

Thanks!

 

Link to comment
  • 4 weeks later...

Hi, I'd love to use this plugin but I'm getting an error about my Python version. Is that something that I am able to resolve?

 

BUILD FAILED (OS X 13.2.1 using python-build 20180424)

Inspect or clean up the working tree at /var/folders/kg/pwnnpb3d6gjgmwxb08c7drnr0000gn/T/python-build.20230429153841.51566

 

I'm on Mac Ventura 13.2.1 and using Alfred 4.8

 

Link to comment
  • 9 months later...

I've been trying to get a very helpful-looking workflow to work, but it appears to not be made for Python 3. I get the following error when running it: "Task '/usr/bin/python' failed with reason 'launch path not accessible'." I believe the author is no longer updating it. Could someone help me out with whether it's possible for me to manually update the script to work with Python 3 and if that would be a relatively minor update or too complex for a non-coder? 

Link to comment

Thanks, this looks like a great workflow! However, it probably doesn't quite match my use case: I'm trying to really quickly make temporary snippets. I just want to have one snippet keyword that is flexible, and I can swap out different content for it over time as the need arises. So I'd prefer to avoid the trouble of a full CSV file if possible.

Link to comment

Looking at the workflow, it might work if you simply change the Language dropdown in the Run Script to /usr/bin/python3. That is assuming it’s not using features which changed in Python 3, but it’s small enough it might work.

Link to comment

Thanks, that was a great idea. It doesn't seem to work, though. I get this in the bug report. If you immediately recognize the problem from this (seems traceable to one Python 3 file?), I'd really appreciate knowing!

 

[09:34:37.043] ERROR: Snippet Creator[Run Script] Traceback (most recent call last):
  File "<string>", line 20, in <module>
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 234, in dumps
    return cls(
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/encoder.py", line 201, in encode
    chunks = list(chunks)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/encoder.py", line 431, in _iterencode
    yield from _iterencode_dict(o, _current_indent_level)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/encoder.py", line 405, in _iterencode_dict
    yield from chunks
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/encoder.py", line 405, in _iterencode_dict
    yield from chunks
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/encoder.py", line 438, in _iterencode
    o = _default(o)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable

Link to comment
  • 3 weeks later...

@vitor, I ultimately decided to use your Snippet Transformer workflow, and can't seem to get it to work. I made the snippets csv file (depicted below with a single snippet, using the name, keyword, snippet format), and can locate it using the CSV to Alfred Snippet Collection option in the Alfred Window. But once I select the file, nothing happens. I get no notification, and the snippets don't show up. The Alfred window just disappears. Any thoughts on what I might be doing wrong? image.thumb.png.2cbcd3ca01fed1b00ac4bef920f5713a.png

Link to comment

Thank you, and sorry for providing insufficient info! I was able to figure out part of the problem (it seemed that the workflow had created a csv file on my Desktop that was not uploading to Alfred, and it was blocking the workflow from running on subsequent occasions, because it recognized that a .alfredsnippets file already existed with the relevant name). But now I have two further questions, and I'll provide more background this time. 

 

Background: I downloaded the workflow via the link you provided above. I am running Alfred 5.5, MacOS Sonoma 14.3, workflow 2023.2. I have the workflow set to read csvs with the following order: name, keyword, snippet and to open the csv upon completion. When I run the workflow now, I get a popup asking to import the .alfredsnippets file that has been created, I click yes, and then the snippet in the file is in fact added to Alfred. However, the snippet is not immediately active: I must go in to the snippets section of Alfred and tick the activate box.

 

My remaining questions: (1) Is it possible to skip the popup window step and automatically import to Alfred? (2) Is there any way to automatically make the snippets from the spreadsheet active, so I don't have to go back to Alfred settings and tick to activate? 

 

Why I want to know this: I would like to incorporate this workflow into another one that does all of this a bit faster, because I am trying to just add a single snippet very quickly. So I am hoping to automate insertion of my snippet into the csv file and then immediately run this workflow on the updated csv file. Removing these added steps would be super helpful.

Link to comment

I finally found an updated workflow that does what the original workflow here used to (and perfectly!). See the Dropbox link at this thread. Created by @lehcar3

 

If your preferences are in Dropbox, as mine are, you will need to alter a single line in the zFolder script filter within the workflow, to the following: 

const targetDir = '~/Dropbox/Alfred.alfredpreferences/snippets';

 

For my own preferences: I also deleted the final step that shows the snippet json file in Finder, and altered the Alfred restart script at the end by deleting the final two commands (to open Alfred and the search window--just reactivating Alfred is enough for my purposes). 

 

And that means I actually don't need the Snippet Transformer workflow! Thank you, though, @vitor.

Edited by macrospect
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...