Skip to content

Instantly share code, notes, and snippets.

@davidsword
Last active November 8, 2022 00:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidsword/a23ad1fdf8c944f147732c6db44ee1c1 to your computer and use it in GitHub Desktop.
Save davidsword/a23ad1fdf8c944f147732c6db44ee1c1 to your computer and use it in GitHub Desktop.
Convert Alfred Snippets to Espanso matches
<?php
/**
* Convert Alfred Snippets to Espanso matches
*
* @see https://www.alfredapp.com/help/features/snippets/
* @see https://espanso.org/
*/
define('SNIPPETS_DIR', '/path/to/export/of/Alfred/snippets/');
$alfred_snippet_files = get_json_files_from_dir( SNIPPETS_DIR );
$alfred_snippets = [];
$espanso_yaml = '';
foreach ( $alfred_snippet_files as $snippet_file )
$alfred_snippets[] = json_decode( file_get_contents( $snippet_file ), JSON_OBJECT_AS_ARRAY )['alfredsnippet'];
foreach ( $alfred_snippets as $alfred_snippet ) {
// remove any ":" triggers already existing in Alfred to prevent doubling
$keyword = str_replace(':', '', $alfred_snippet['keyword']);
// fix escaping
$snippet = str_replace('\\',"\\\\", $snippet);
$snippet = str_replace('"',"\"", $snippet);
// no new line support in YAML, break down here
$snippet = str_replace(["\r\n", "\r", "\n"], "\\n", $alfred_snippet['snippet']);
// https://espanso.org/docs/matches/
$snippet = str_replace('{cursor}', '$|$', $snippet);
$snippet = str_replace('{clipboard}', '{{clipboard}}', $snippet);
// @TODO there's probably more with date, but I didn't use any.
$espanso_yaml .= "
- trigger: \":{$keyword}\"
replace: \"{$snippet}\"
";
}
echo $espanso_yaml; // or just write this to ~/.config/espanso/default.yaml
function get_json_files_from_dir($dir, $list = []){
$ffs = scandir($dir);
foreach ( $ffs as $ff ){
if ( $ff == '.' || $ff == '..' )
continue;
if( is_dir($dir.'/'.$ff) )
$list = get_json_files_from_dir($dir.$ff, $list);
if ( substr($ff, -4) == 'json' )
$list[] = $dir.'/'.$ff;
}
return $list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment