Last active
July 8, 2024 16:57
-
-
Save davidsword/a23ad1fdf8c944f147732c6db44ee1c1 to your computer and use it in GitHub Desktop.
Convert Alfred Snippets to Espanso matches
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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