showFileInFolder

showFileInFolder

function showFileInFolder(filePath) {
    require('electron').ipcRenderer.send("siyuan-open-folder", filePath);
}

运行本地命令

function runCmd(command, callback) {
    const { exec } = require('child_process');
    exec(command, (error, stdout, stderr) => {
        if (error) {
            console.error(`执行错误: ${error.message}`);
            return;
        }
        if (stderr) {
            console.error(`stderr: ${stderr}`);
            return;
        }
        if (typeof callback === 'function') {
            callback(stdout);
        }
    });
}

打开本地文件

function open(target) {
    const { exec } = require('child_process');
    if (process.platform === 'win32') {
        // Windows
        exec(`start "" "${target}"`, (error, stdout, stderr) => {
            if (error) console.error(`执行错误: ${error.message}`);
            if (stderr) console.error(`stderr: ${stderr}`);
        });
    } else if (process.platform === 'darwin') {
        // macOS
        exec(`open -a "${target}"`, (error, stdout, stderr) => {
            if (error) console.error(`执行错误: ${error.message}`);
            if (stderr) console.error(`stderr: ${stderr}`);
        });
    } else {
        // Linux/其他
        exec(`xdg-open "${target}"`, (error, stdout, stderr) => {
            if (error) console.error(`执行错误: ${error.message}`);
            if (stderr) console.error(`stderr: ${stderr}`);
        });
    }
}
// 示例调用
open('C:\\path\\to\\file.txt'); // Windows
open('/Applications/Preview.app'); // macOS
open('/path/to/file.pdf'); // Linux
image.png

留下你的脚步
推荐阅读