From 5f2f76a7d3dc3e8c56324e548140c82607ee131f Mon Sep 17 00:00:00 2001 From: salman Date: Sat, 13 Jun 2026 16:03:28 +0200 Subject: [PATCH] Allow loading and running cfg file from argument --- TrimSPelec.js | 26 +++++++++++++++++++------- main.js | 27 +++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/TrimSPelec.js b/TrimSPelec.js index cc10fd7..b10d17b 100644 --- a/TrimSPelec.js +++ b/TrimSPelec.js @@ -120,6 +120,19 @@ function fallbackBrowseForFolder() { picker.click(); } +function loadConfigFile(filename, autoRun = false) { + console.log('Open file ' + filename); + fs.readFile(filename.toString(), function read(err, data) { + if (err) { + throw err; + } + setValues(data); + if (autoRun) { + setTimeout(() => startSim(), 0); + } + }); +} + function firstLoad() { document.getElementById("trimPath").value = process.cwd(); if (typeof syncStatusBar === 'function') { @@ -140,13 +153,12 @@ function firstLoad() { // Catch calls for open file ipcRenderer.on('openFile', function(event, filename) { - console.log('Open file '+filename); - fs.readFile(filename.toString(), function read(err, data) { - if (err) { - throw err; - } - setValues(data); - }); + loadConfigFile(filename, false); + }); + + ipcRenderer.on('openFileAndMaybeRun', function(event, payload) { + if (!payload || !payload.filename) { return; } + loadConfigFile(payload.filename, !!payload.autoRun); }); // Catch calls for selectfolder diff --git a/main.js b/main.js index b7dea73..0415fe0 100644 --- a/main.js +++ b/main.js @@ -2,9 +2,27 @@ const path = require('path'); const { app, BrowserWindow, Menu, dialog, ipcMain, fs } = require('electron'); let currentWorkPath = process.cwd(); +const startupOptions = parseStartupOptions(process.argv); app.setName('TrimSP'); +function parseStartupOptions(argv) { + const options = { + configPath: null, + autoRun: false, + }; + + for (const arg of argv.slice(1)) { + if (arg === '--run') { + options.autoRun = true; + } else if (!arg.startsWith('--') && options.configPath == null) { + options.configPath = path.resolve(arg); + } + } + + return options; +} + function showAboutDialog(parentWindow) { const detail = [ 'TRIM.SP simulation GUI for low-energy ion implantation studies.', @@ -67,6 +85,15 @@ function createWindow () { } }) + win.webContents.on('did-finish-load', () => { + if (startupOptions.configPath) { + win.webContents.send('openFileAndMaybeRun', { + filename: startupOptions.configPath, + autoRun: startupOptions.autoRun, + }); + } + }); + let template = [ {