def run(self): try: if self.mode != "dir": args = {} if self.path: args["InitialDir"] = os.path.dirname(self.path) path = os.path.splitext(os.path.dirname(self.path)) args["File"] = path[0] args["DefExt"] = path[1] args["Title"] = self.title if self.title else "Pick a file..." args["CustomFilter"] = 'Other file types\x00*.*\x00' args["FilterIndex"] = 1 filters = "" for f in self.filters: if type(f) == str: filters += (f + "\x00") * 2 else: filters += f[0] + "\x00" + ";".join(f[1:]) + "\x00" args["Filter"] = filters flags = (win32con.OFN_EXTENSIONDIFFERENT | win32con.OFN_OVERWRITEPROMPT) if self.multiple: flags |= win32con.OFN_ALLOWmultiple | win32con.OFN_EXPLORER if self.show_hidden: flags |= win32con.OFN_FORCESHOWHIDDEN args["Flags"] = flags if self.mode == "open": self.fname, _, _ = win32gui.GetOpenFileNameW(**args) elif self.mode == "save": self.fname, _, _ = win32gui.GetSaveFileNameW(**args) if self.fname: if self.multiple: seq = str(self.fname).split("\x00") dir_n, base_n = seq[0], seq[1:] self.selection = [os.path.join(dir_n, i) for i in base_n] else: self.selection = str(self.fname).split("\x00") else: # From http://goo.gl/UDqCqo pidl, name, images = browse( # pylint: disable=unused-variable win32gui.GetDesktopWindow(), None, self.title if self.title else "Pick a folder...", 0, None, None ) self.selection = [str(get_path(pidl))] return self.selection except (RuntimeError, pywintypes.error): return None
def run(self): self.selection = [] try: if self.mode != "dir": args = {} if self.path: if isdir(self.path): args["InitialDir"] = self.path else: args["InitialDir"] = dirname(self.path) _, ext = splitext(self.path) args["File"] = self.path args["DefExt"] = ext and ext[1:] # no period args["Title"] = self.title if self.title else "Pick a file..." args["CustomFilter"] = 'Other file types\x00*.*\x00' args["FilterIndex"] = 1 # e.g. open_file(filters=['*.txt', '*.py']) filters = "" for f in self.filters: if type(f) == str: filters += (f + "\x00") * 2 else: filters += f[0] + "\x00" + ";".join(f[1:]) + "\x00" args["Filter"] = filters flags = win32con.OFN_OVERWRITEPROMPT flags |= win32con.OFN_HIDEREADONLY if self.multiple: flags |= win32con.OFN_ALLOWMULTISELECT flags |= win32con.OFN_EXPLORER if self.show_hidden: flags |= win32con.OFN_FORCESHOWHIDDEN args["Flags"] = flags try: if self.mode == "open": self.fname, _, _ = win32gui.GetOpenFileNameW(**args) elif self.mode == "save": self.fname, _, _ = win32gui.GetSaveFileNameW(**args) except pywintypes.error as e: # if canceled, it's not really an error if not e.winerror: self._handle_selection(self.selection) return self.selection raise if self.fname: if self.multiple: seq = str(self.fname).split("\x00") if len(seq) > 1: dir_n, base_n = seq[0], seq[1:] self.selection = [join(dir_n, i) for i in base_n] else: self.selection = seq else: self.selection = str(self.fname).split("\x00") else: # dir mode BIF_EDITBOX = shellcon.BIF_EDITBOX BIF_NEWDIALOGSTYLE = 0x00000040 # From http://goo.gl/UDqCqo pidl, name, images = browse( # pylint: disable=unused-variable win32gui.GetDesktopWindow(), None, self.title if self.title else "Pick a folder...", BIF_NEWDIALOGSTYLE | BIF_EDITBOX, None, None) # pidl is None when nothing is selected # and e.g. the dialog is closed afterwards with Cancel if pidl: self.selection = [str(get_path(pidl).decode('utf-8'))] except (RuntimeError, pywintypes.error, Exception): # ALWAYS! let user know what happened import traceback traceback.print_exc() self._handle_selection(self.selection) return self.selection
def run(self): self.selection = [] try: if self.mode != "dir": args = {} if self.path: args["InitialDir"] = dirname(self.path) _, ext = splitext(self.path) args["File"] = self.path args["DefExt"] = ext args["Title"] = self.title if self.title else "Pick a file..." args["CustomFilter"] = 'Other file types\x00*.*\x00' args["FilterIndex"] = 1 # e.g. open_file(filters=['*.txt', '*.py']) filters = "" for f in self.filters: if type(f) == str: filters += (f + "\x00") * 2 else: filters += f[0] + "\x00" + ";".join(f[1:]) + "\x00" args["Filter"] = filters flags = win32con.OFN_EXTENSIONDIFFERENT flags |= win32con.OFN_OVERWRITEPROMPT if self.multiple: flags |= win32con.OFN_ALLOWMULTISELECT flags |= win32con.OFN_EXPLORER if self.show_hidden: flags |= win32con.OFN_FORCESHOWHIDDEN args["Flags"] = flags # GetOpenFileNameW, GetSaveFileNameW will raise # pywintypes.error: (0, '...', 'No error message is available') # which is most likely due to incorrect type handling from the # win32gui side; return empty list in that case after exception if self.mode == "open": self.fname, _, _ = win32gui.GetOpenFileNameW(**args) elif self.mode == "save": self.fname, _, _ = win32gui.GetSaveFileNameW(**args) if self.fname: if self.multiple: seq = str(self.fname).split("\x00") dir_n, base_n = seq[0], seq[1:] self.selection = [ join(dir_n, i) for i in base_n ] else: self.selection = str(self.fname).split("\x00") else: # dir mode # From http://goo.gl/UDqCqo pidl, name, images = browse( # pylint: disable=unused-variable win32gui.GetDesktopWindow(), None, self.title if self.title else "Pick a folder...", 0, None, None ) # pidl is None when nothing is selected # and e.g. the dialog is closed afterwards with Cancel if pidl: self.selection = [str(get_path(pidl).decode('utf-8'))] except (RuntimeError, pywintypes.error): # ALWAYS! let user know what happened import traceback traceback.print_exc() self._handle_selection(self.selection) return self.selection