From 9d8c9fdf511002b49db51b4c357303a962cd0199 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sat, 25 Jan 2020 00:39:18 -0800 Subject: [PATCH] Support searching of windows .lnk file fields. I'm currently migrating my shortcuts that point to Py 3.7 to 3.8 so this saves a lot of time! --- search.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/search.py b/search.py index d98a12d..05a9844 100644 --- a/search.py +++ b/search.py @@ -6,6 +6,10 @@ import re import stat import sys import traceback +try: + import winshell +except ImportError: + winshell = None from voussoirkit import clipext from voussoirkit import expressionmatch @@ -59,6 +63,29 @@ def search_contents_generic(filepath, content_args): yield from results yield '' +def search_contents_windows_lnk(filepath, content_args): + try: + shortcut = winshell.Shortcut(filepath.absolute_path) + except Exception: + return + + text = [ + f'Target: {shortcut.path}', + f'Start In: {shortcut.working_directory}', + f'Comment: {shortcut.description}', + ] + text = '\n'.join(text) + content_args['text'] = text + + results = search(**content_args) + results = list(results) + if not results: + return + + yield filepath.absolute_path + yield from results + yield '' + def search( *, yes_all=None, @@ -147,7 +174,10 @@ def search( if not filepath.is_file: continue - yield from search_contents_generic(filepath, content_args) + if filepath.extension.lower() == 'lnk' and winshell: + yield from search_contents_windows_lnk(filepath, content_args) + else: + yield from search_contents_generic(filepath, content_args) def argparse_to_dict(args): text = args.text