From 7b9792615db3bf0b03e7fdf9dac2beadfea9d48d Mon Sep 17 00:00:00 2001 From: Quintijn Hoogenboom Date: Sat, 27 Apr 2024 15:53:02 +0200 Subject: [PATCH] April24, expose error with setClipboard (#17) * try to get win32 function running... (solved after win32_postinstall ran without errors, cleaning up a lot of pywin32 installs was needed, see https://github.com/dictation-toolbox/natlink/issues/193) * simplified extenvvars, getFolderFromLibraryName, * try to specify setClipboard function spurious error, (issue https://github.com/dictation-toolbox/dtactions/issues/16) --- src/dtactions/unimacro/extenvvars.py | 40 +++++++++++++++++++++-- src/dtactions/unimacro/unimacroactions.py | 6 ++-- test/test_extenvvars.py | 7 ++-- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/dtactions/unimacro/extenvvars.py b/src/dtactions/unimacro/extenvvars.py index c852843..7a5c6a9 100644 --- a/src/dtactions/unimacro/extenvvars.py +++ b/src/dtactions/unimacro/extenvvars.py @@ -17,8 +17,10 @@ from os.path import normpath, isfile, isdir, join import copy import re +import ctypes from pathlib import Path -from win32com.shell import shell, shellcon +from win32com.shell import shellcon + import platformdirs try: import natlink @@ -118,6 +120,32 @@ def getFolderFromLibraryName(self, name): return platformdirs.windows.get_win_folder("CSIDL_COMMON_APPDATA") if name in ['LOCAL_APPDATA']: return platformdirs.windows.get_win_folder("CSIDL_LOCAL_APPDATA") + + # # General case, try via shellcon! TODO: QH + # try: + # CSIDL_variable = 'CSIDL_%s'% name + # shellnumber = getattr(shellcon,CSIDL_variable, -1) + # print(f'getFolderFromLibraryName, shellnumber of "{CSIDL_variable}": {shellnumber}') + # except: + # print('getExtendedEnv, cannot find CSIDL_variable for: "%s"'% name) + # return '' + # if shellnumber < 0: + # # on some systems have SYSTEMROOT instead of SYSTEM: + # print('getExtendedEnv, cannot find CSIDL_variable for (returns -1): "%s"'% name) + # try: + # csidl_const = shellnumber + # # copied from platformdirs/windows.py: + # buf = ctypes.create_unicode_buffer(1024) + # windll = getattr(ctypes, "windll") # noqa: B009 # using getattr to avoid false positive with mypy type checker + # windll.shell32.SHGetFolderPathW(None, csidl_const, None, 0, buf) + # result = buf.value + # # # result = shell.SHGetFolderPath (0, shellnumber, 0, 0) + # # result = ctypes.windll.shell32.SHGetFolderPathW(0, shellnumber, 0, 0) + # print(f'result from SHGetFolderPathW: {result}') + # except: + # print('getFolderFromLibraryName, cannot path for CSIDL: "%s"'% name) + # return '' + ## extra cases: # if name in ['Quick access', 'Snelle toegang']: @@ -280,7 +308,15 @@ def getExtendedEnv(self, var, noCache=False, displayMessage=True): return self.getExtendedEnv('SYSTEMROOT') return '' try: - result = shell.SHGetFolderPath (0, shellnumber, 0, 0) + csidl_const = shellnumber + # copied from platformdirs/windows.py: + buf = ctypes.create_unicode_buffer(1024) + windll = getattr(ctypes, "windll") # noqa: B009 # using getattr to avoid false positive with mypy type checker + windll.shell32.SHGetFolderPathW(None, csidl_const, None, 0, buf) + result = buf.value + # # result = shell.SHGetFolderPath (0, shellnumber, 0, 0) + # result = ctypes.windll.shell32.SHGetFolderPathW(0, shellnumber, 0, 0) + print(f'result from SHGetFolderPathW: {result}') except: if displayMessage: print('getExtendedEnv, cannot find in os.environ or CSIDL: "%s"'% var) diff --git a/src/dtactions/unimacro/unimacroactions.py b/src/dtactions/unimacro/unimacroactions.py index 828d345..8123e0d 100644 --- a/src/dtactions/unimacro/unimacroactions.py +++ b/src/dtactions/unimacro/unimacroactions.py @@ -1132,7 +1132,6 @@ def do_SCLIP(*s, **kw): # for i, t in enumerate(s): # print "SCLIP:", i, t total = total.replace("{enter}", "\n") - # print 'SCLIP: %s type: %s'% (total, type(total)) unimacroutils.setClipboard(total, format=13) unimacroutils.Wait() #print 'send through clipboard: %s'% total5N @@ -2310,9 +2309,12 @@ def try_SCLIP(): ''' try by running this script, ensure cursor is on a safe place (bottom, or here after a # ''' do_SCLIP('hello') - do_SCLIP('9123456789') + print('try first number:') + do_SCLIP('9123454321') # now comes the error: + print('try second number:') do_SCLIP('123456789') + print('ready') if __name__ == '__main__': diff --git a/test/test_extenvvars.py b/test/test_extenvvars.py index 9661224..b94740a 100644 --- a/test/test_extenvvars.py +++ b/test/test_extenvvars.py @@ -36,9 +36,9 @@ def test_getNatlinkEnvVariables(): result = ext.getExtendedEnv("%Unimacro%") assert len(result) assert Path(result).is_dir() - result = ext.getExtendedEnv("%VocolaUserDirectory%") - assert Path(result).is_dir() - assert len(result) + # result = ext.getExtendedEnv("%VocolaUserDirectory%") + # assert Path(result).is_dir() + # assert len(result) # AHK maybe not installed: result1 = ext.getExtendedEnv("%AhkUser%") @@ -61,7 +61,6 @@ def test_otherEnvVariables(): assert len(result) > 0 assert isdir(result) - def test_windows_Library_dirs(): """most of them with platformdirs.windows, some special treatment.