From f9fc28d5ec5b911ac008abb2d7ef94f9c8294262 Mon Sep 17 00:00:00 2001 From: yedpodtrzitko Date: Wed, 1 May 2024 15:46:08 +0800 Subject: [PATCH] fix file opening on posix --- tagstudio/src/cli/ts_cli.py | 20 +++++----- tagstudio/src/qt/helpers/file_opener.py | 51 ++++++++++++++++--------- 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/tagstudio/src/cli/ts_cli.py b/tagstudio/src/cli/ts_cli.py index 30e14ae9..a26c5712 100644 --- a/tagstudio/src/cli/ts_cli.py +++ b/tagstudio/src/cli/ts_cli.py @@ -23,6 +23,7 @@ from src.core.ts_core import * from src.core.utils.web import * from src.core.utils.fs import * from src.core.library import * +from src.qt.helpers.file_opener import file_open WHITE_FG = '\033[37m' WHITE_BG = '\033[47m' @@ -352,8 +353,8 @@ class CliDriver: if not os.path.isfile(external_preview_path): temp = self.external_preview_default temp.save(external_preview_path) - if os.path.isfile(external_preview_path): - os.startfile(external_preview_path) + + file_open(external_preview_path) def set_external_preview_default(self) -> None: """Sets the external preview to its default image.""" @@ -1706,8 +1707,7 @@ class CliDriver: args = ['explorer', '/select,', filename] subprocess.call(args) else: - if os.path.isfile(filename): - os.startfile(filename) + file_open(filename) # refresh=False # self.scr_browse_entries_gallery(index) # Add Field ============================================================ @@ -2152,9 +2152,8 @@ class CliDriver: # Open ============================================================= elif (com[0].lower() == 'open' or com[0].lower() == 'o'): for match in self.lib.missing_matches[filename]: - fn = f'{os.path.normpath(self.lib.library_dir + "/" + match + "/" + entry.filename)}' - if os.path.isfile(fn): - os.startfile(fn) + fn = os.path.normpath(self.lib.library_dir + "/" + match + "/" + entry.filename) + file_open(fn) refresh = False # clear() # return self.scr_choose_missing_match(index, clear_scr=False) @@ -2276,8 +2275,8 @@ class CliDriver: # fn = f'{os.path.normpath(self.lib.library_dir + "/" + match + "/" + entry_1.filename)}' # if os.path.isfile(fn): # os.startfile(fn) - os.startfile(dupe[0]) - os.startfile(dupe[1]) + file_open(dupe[0]) + file_open(dupe[1]) # clear() # return self.scr_resolve_dupe_files(index, clear_scr=False) # Mirror Entries =================================================== @@ -2385,8 +2384,7 @@ class CliDriver: # Open with Default Application ======================================== if (com[0].lower() == 'open' or com[0].lower() == 'o'): - if os.path.isfile(filename): - os.startfile(filename) + file_open(filename) # self.scr_edit_entry_tag_box(entry_index, field_index) # return # Close View =========================================================== diff --git a/tagstudio/src/qt/helpers/file_opener.py b/tagstudio/src/qt/helpers/file_opener.py index e6703f11..747144ca 100644 --- a/tagstudio/src/qt/helpers/file_opener.py +++ b/tagstudio/src/qt/helpers/file_opener.py @@ -5,6 +5,8 @@ import logging import os import subprocess +import sys +from os.path import isfile from PySide6.QtWidgets import QLabel @@ -15,7 +17,20 @@ INFO = f'[INFO]' logging.basicConfig(format="%(message)s", level=logging.INFO) -class FileOpenerHelper(): +def file_open(filepath: str, check_first: bool = True): + if check_first and not os.path.isfile(filepath): + logging.error(f'File not found: {filepath}') + return False + + if os.name == 'nt': + os.startfile(filepath) + elif sys.platform == 'darwin': + subprocess.Popen(['open', filepath]) + else: + subprocess.call(["xdg-open", filepath]) + + +class FileOpenerHelper: def __init__(self, filepath:str): self.filepath = filepath @@ -23,26 +38,26 @@ class FileOpenerHelper(): self.filepath = filepath def open_file(self): - if os.path.exists(self.filepath): - os.startfile(self.filepath) - logging.info(f'Opening file: {self.filepath}') - else: - logging.error(f'File not found: {self.filepath}') + logging.info(f'Opening file: {self.filepath}') + file_open(self.filepath) def open_explorer(self): - if os.path.exists(self.filepath): - logging.info(f'Opening file: {self.filepath}') - if os.name == 'nt': # Windows - command = f'explorer /select,"{self.filepath}"' - subprocess.run(command, shell=True) - else: # macOS and Linux - command = f'nautilus --select "{self.filepath}"' # Adjust for your Linux file manager if different - if subprocess.run(command, shell=True).returncode == 0: - file_loc = os.path.dirname(self.filepath) - file_loc = os.path.normpath(file_loc) - os.startfile(file_loc) - else: + if not os.path.exists(self.filepath): logging.error(f'File not found: {self.filepath}') + return + + logging.info(f'Opening file: {self.filepath}') + if os.name == 'nt': # Windows + command = f'explorer /select,"{self.filepath}"' + subprocess.run(command, shell=True) + elif sys.platform == 'darwin': + subprocess.Popen(['open', '-R', self.filepath]) + else: # macOS and Linux + command = f'nautilus --select "{self.filepath}"' # Adjust for your Linux file manager if different + if subprocess.run(command, shell=True).returncode == 0: + file_loc = os.path.dirname(self.filepath) + file_loc = os.path.normpath(file_loc) + os.startfile(file_loc) class FileOpenerLabel(QLabel):