diff --git a/src/pyflowx/conditions.py b/src/pyflowx/conditions.py index e904296..178239b 100644 --- a/src/pyflowx/conditions.py +++ b/src/pyflowx/conditions.py @@ -9,6 +9,7 @@ from __future__ import annotations import os import shutil import sys +from pathlib import Path from typing import Callable # 条件判断函数类型 @@ -86,6 +87,11 @@ class BuiltinConditions: _check.__name__ = f"HAS_INSTALLED({app_name!r})" return _check + @staticmethod + def DIR_EXISTS(dir: Path) -> Condition: + """路径是否存在.""" + return dir.exists + @staticmethod def ENV_VAR_EXISTS(var_name: str) -> Condition: """检查环境变量是否存在. diff --git a/src/pyflowx/tasks/system.py b/src/pyflowx/tasks/system.py index 6150117..e7a4b5b 100644 --- a/src/pyflowx/tasks/system.py +++ b/src/pyflowx/tasks/system.py @@ -8,8 +8,10 @@ from __future__ import annotations import os import subprocess +from pathlib import Path import pyflowx as px +from pyflowx import BuiltinConditions from pyflowx.conditions import Constants @@ -34,6 +36,7 @@ def reset_icon_cache() -> list[px.TaskSpec]: px.TaskSpec( "delete_icon_cache", fn=lambda: subprocess.run(["del", "/a", "/q", r"%localappdata%\IconCache.db"], check=False), + conditions=(BuiltinConditions.DIR_EXISTS(Path(r"%localappdata%\IconCache.db")),), verbose=True, ), px.TaskSpec( @@ -41,11 +44,13 @@ def reset_icon_cache() -> list[px.TaskSpec]: fn=lambda: subprocess.run( ["del", "/a", "/q", r"%localappdata%\Microsoft\Windows\Explorer\iconcache*"], check=False ), + conditions=(BuiltinConditions.DIR_EXISTS(Path(r"%localappdata%\Microsoft\Windows\Explorer")),), verbose=True, ), px.TaskSpec( "restart_explorer", - fn=lambda: subprocess.run(["cmd", "/c", "start", "explorer.exe"], check=False), + fn=lambda: subprocess.run(["explorer.exe"], check=False), + conditions=(BuiltinConditions.HAS_INSTALLED("explorer.exe"),), verbose=True, ), ]