Skip to content

Commit

Permalink
April24, expose error with setClipboard (#17)
Browse files Browse the repository at this point in the history
* try to get win32 function running... (solved after win32_postinstall ran without errors, cleaning up a lot of pywin32 installs was needed, see dictation-toolbox/natlink#193)
* simplified extenvvars, getFolderFromLibraryName,
* try to specify setClipboard function spurious error, (issue #16)
  • Loading branch information
quintijn authored Apr 27, 2024
1 parent 14899be commit 7b97926
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
40 changes: 38 additions & 2 deletions src/dtactions/unimacro/extenvvars.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']:
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions src/dtactions/unimacro/unimacroactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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__':
Expand Down
7 changes: 3 additions & 4 deletions test/test_extenvvars.py
Original file line number Diff line number Diff line change
Expand Up @@ -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%")
Expand All @@ -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.
Expand Down

0 comments on commit 7b97926

Please sign in to comment.