├── LICENSE.md ├── README.md └── importSnippets.py /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Derick Fay 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # import-alfred-snippets 2 | Import snippets to Alfred 3 or 4 from a .csv file 3 | 4 | (Combine this with https://github.com/derickfay/te-to-alfredCSV to convert TextExpander 5 snippets to Alfred snippets) 5 | 6 | The csv needs to be named snippets.csv and must contain exactly three fields: the snippet name, the abbreviation, and the snippet text itself. For example: 7 | 8 | ```bash 9 | 10 | "any","Pan","PropTypes.any" 11 | "array","Parr","PropTypes.array" 12 | "arrayOf","Paro","PropTypes.arrayOf()" 13 | "bool","Pbo","PropTypes.bool" 14 | "element","Pel","PropTypes.element" 15 | "func","Pfn","PropTypes.func" 16 | "instanceOf","Pio","PropTypes.instanceOf()" 17 | "node","Pno","PropTypes.node" 18 | "number","Pnu","PropTypes.number" 19 | "object","Pobj","PropTypes.object" 20 | "objectOf","Pobo","PropTypes.objectOf()" 21 | "oneOf","Poo","PropTypes.oneOf([])" 22 | "oneOfType","Pot","PropTypes.oneOfType" 23 | "shape","Psh","PropTypes.shape({})" 24 | "string","Pst","PropTypes.string" 25 | 26 | ``` 27 | 28 | ## Usage 29 | 30 | Save the file **importSnippets.py** to a location of your choice - this should probably be a temporary folder because 31 | when the script is run, it will generate a file for each snippet. 32 | 33 | Save your snippets in a csv file called **snippets.csv** as descibed above, in the same directory where you put the script. 34 | 35 | To run the script, open a Terminal window in the same directory where you put the script, then at the prompt, type: 36 | 37 | **python ./importSnippets.py** 38 | 39 | If all goes well, you should then have individual files in the same directory with names like *any [8760badff15c594b6308564f4460e7].json* , where the text in brackets is a random string generated as the uid (used by Alfred to track usage). 40 | 41 | Move the newly generated files to .../Alfred.alfredpreferences/snippets/*groupname* where groupname is the group where you want the snippets. Creating a new folder will create a new group in Alfred. After a few seconds, the new snippets willl appear in Alfred's preferences. (To view this folder in Finder, you'll need to find your Alfred.alfredpreferences file, right-click and click "Show Package Contents"). 42 | 43 | You can then delete or replace **snippets.csv** and start again. 44 | 45 | I have occasionally had errors where the csv library used by the script thinks there are line breaks midway through a field -- the easiest fix I've found is to open snippets.csv in Excel, then save it as csv. 46 | 47 | **DISCLAIMERS**: This is largely untested, and doesn't escape any special characters at the moment. As far as I can tell, it causes no problems for Alfred, but you use it at your own risk. 48 | 49 | Released under MIT License 50 | -------------------------------------------------------------------------------- /importSnippets.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | # importSnippets.py 4 | # 5 | # generates Alfred 3 json snippets from a csv file 6 | # 7 | # written for python 2.7.10 8 | 9 | 10 | import csv 11 | import json 12 | import os, binascii 13 | 14 | sourceFile = 'snippets.csv' 15 | fieldNames = ['name','keyword','content'] 16 | 17 | with open (sourceFile, 'rt') as csvfile: 18 | reader = csv.DictReader(csvfile, fieldnames=fieldNames) 19 | for row in reader: 20 | uid = binascii.b2a_hex(os.urandom(15)) 21 | output = json.dumps({"alfredsnippet" : {"snippet" : row['content'], "uid": uid, "name" : row['name'], "keyword" : row['keyword']}}, sort_keys=False, indent=4, separators=(',', ': ')) 22 | outputFile = row['name']+" ["+uid+"].json" 23 | f = open(outputFile, 'w') 24 | f.write(output) 25 | f.close() 26 | --------------------------------------------------------------------------------