Search
« Multi-Resource Presentations | Main | Workflow: Modify Clipboard Character Case »
Saturday
Apr232016

Exporting Snippets from TextExpander (v6)

TextExpander has long used Dropbox to synchronise snippets, but with the advent of the latest version (v6 on Mac), the snippets are now synchronised by a proprietary synchronisation service. Thoughts about the move to a software as a service (SaaS) model aside this then poses the question how can you access your snippets for the purposes of independent backup or sharing with users on earlier TextExpander versions?

Exporting a Snippet Group

In the update Smile did give some consideration to this. There is an option in TextExpander's File menu to export a group of snippets. With a group selected, pressing CMD+S will open a save dialog and allow you to save a snapshot of the group to a .textexpander file.

Exporting all Snippet Groups

That's great for backwards sharing with a user of an earlier version of TextExpander. However it is a bit tedious if you want to create a backup of all of your snippet groups, or simply export them for archival before you remove a set. Given that the sync service is new and you might just sleep that little bit safer at night being in control of your own backup. Plus what if you accidentally make a change and want to revert it whilst you're offline (like on a cross-country UK rail service)? Whilst the TextExpander sync service does include a version history offline access can be a comforting thought.

It could even be that you have trialled the latest version, have developed some new snippets but would like to return to the previous version with Dropbox syncing but armed with your most recent set of snippets.

Within the TextExpander application to export all snippet groups you would have to manually work through each snippet group and export it. All rather repetitive and tedious ... which of course makes it a perfect candidate for some automation.

Automating Snippet Group Exporting

Fortunately TextExpander still includes AppleScript support and so I put together a quick script that gives users of the latest version of TextExpander some options for mass exporting.

The script has three variables at the start that you must configure to your own requirements.

strBaseFolder This variable specifies the path to which the snippet groups will be exported. By specifying this in the script you will then always know where to find the exported snippets and be able to set it to run unattended (either on demand running in the background or on a schedule via launchd, Keyboard Maestro, etc.)

bTimebased This option will export the snippet groups into a new folder within the base folder each time it is run. The sub folder is named as a time stamp in the format yyyy-MM-dd-hh.mm.ss. If you want to keep a range of backups in this way set it as true. If you want to overwrite the snippet groups each time and just retain the last backup, then set this to false.

bZip This final option will create a Zip file containing all of the exported snippets. The Zip file is saved to the base folder and is named with the time stamp.

What the Script Does

The script begins by appending the time stamp to the base path if either the time based or zip options have been enabled. I'm using a tweaked version of a piece of AppleScript/Python created by Gabe Weatherhead (@MacDrifter). The URL to the source is included in the comment in the script and I'd heartily recommend you check out that and all of the other great info over at macdrifter.com.

With the output folder modified if required a shell command is used to ensure all of the requisite folders exist.

The script then tells TextExpander to select each snippet group and one by one export them to the specified folder. Each snippet group is exported using it's name as the file name with a .textexpander file extension.

The script then checks to see if the Zip option has been set. If it has then a Zip file is generated in the base folder from the time stamped folder just generated. It then deletes the folder of snippet files. Both of these activities are carried out by shell commands.

Finally the script outputs a notification indicating how many snippet groups were exported and where to.

The Script

Below is a copy of the script. Copy and paste the content into your script editor and amend it to suit your needs (see points above).

-- The base folder where you want to backup the TextExpander snippets
set strBaseFolder to "/Users/stephen/Dropbox/Temp/TextExpander"
-- Set bTimebased to true to save to a new sub directory on each run
set bTimebased to true
-- Set bZip to archive exported snippet groups
set bZip to true

-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

-- If any time based processing is to be used append a timestamp to the directory
if (bTimebased or bZip) then
    set strBaseFolder to strBaseFolder & "/" & pyTimestamp(current date)
end if

-- Create any required directory structure
do shell script "mkdir -p \"" & strBaseFolder & "/\""

-- Export all TextExpander snippet groups
tell application "TextExpander"
    repeat with objGroup in groups
        set strFilename to strBaseFolder & "/" & (name of objGroup) & ".textexpander"
        save objGroup in strFilename
    end repeat


    -- If the option is set ZIP the resulting folder of files
    if bZip then
        -- Create the ZIP file
        do shell script "zip -jr \"" & strBaseFolder & ".zip\" \"" & strBaseFolder & "/\""
        -- Remove the intermediate directory of files
        do shell script "rm -rf \"" & strBaseFolder & "/\""
        display notification (((count groups) as string) & " snippet groups backed up to 
" & strBaseFolder & ".zip") with title "TextExpander Snippet Backup"
    else
        display notification (((count groups) as string) & " snippet groups backed up to 
" & strBaseFolder) with title "TextExpander Snippet Backup"
    end if
end tell

-- Timestamp generation script based on script published by MacDrifter
-- http://www.macdrifter.com/2011/12/timestamps-in-applescript.html
on pyTimestamp(AS_Date)
    set timeFormat to quoted form of "%Y-%m-%d-%H.%M.%S"
    return (do shell script "/usr/bin/python -c \"import time, dateutil.parser; print dateutil.parser.parse('" & AS_Date & "').strftime(" & timeFormat & "); \"")
end pyTimestamp

Conclusion

If you've read this far then hopefully you'll find this script useful. If you have be sure to let me know by contacting me on Twitter (@sylumer) and/or leaving a comment below.

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>