Add a common function to open file paths externally

This commit is contained in:
Veronica Berglyd Olsen
2023-11-23 16:43:56 +01:00
parent 4f9aab3c81
commit 30b4e5511a
4 changed files with 23 additions and 20 deletions
+14 -1
View File
@@ -33,8 +33,11 @@ from typing import Any, Literal
from pathlib import Path
from datetime import datetime
from configparser import ConfigParser
from urllib.parse import urljoin
from urllib.request import pathname2url
from PyQt5.QtCore import QCoreApplication
from PyQt5.QtGui import QDesktopServices
from PyQt5.QtCore import QCoreApplication, QUrl
from PyQt5.QtWidgets import QWidget, qApp
from novelwriter.enum import nwItemClass, nwItemType, nwItemLayout
@@ -495,6 +498,16 @@ def getFileSize(path: Path) -> int:
return -1
def openExternalPath(path: Path) -> bool:
"""Open a path by passing it to the desktop environment."""
if Path(path).exists():
QDesktopServices.openUrl(
QUrl(urljoin("file:", pathname2url(str(path))))
)
return True
return False
# =============================================================================================== #
# Other Functions
# =============================================================================================== #