首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在NWJS应用程序中按一下按钮启动单独的NWJS应用程序

在NWJS应用程序中按一下按钮启动单独的NWJS应用程序
EN

Stack Overflow用户
提问于 2022-05-26 21:39:32
回答 2查看 43关注 0票数 2

我正在为软件开发一个发射应用程序。无法seam找到如何在我的NWJS应用程序中点击一个按钮启动另一个应用程序。我以前曾在HTA上做过这件事,当时是为了推出我的电子游戏,但我不能让seam在这里找到答案。在hta上,我必须在单击时生成一个bat文件,然后bat脚本运行程序。

我做的纽扣:

代码语言:javascript
复制
<span name="SystemWrapper3" id="SystemWrapper3" class="SystemWrapper3">
    <input type="button" value="Calculator"    name="SystemInput3" id="SystemInput3" class="SystemInput3" />
    <i class="fa-solid fa-square-root-variable fa-lg" id="SystemIcon4"></i>
</span>

我一直在调查这件事我对这件事还是很陌生的。我尝试过几种解决方案,我在网上找到了一些东西,但它们都没能奏效。如果它是版本不兼容还是什么?我使用的是NWJS0.64.1 Win64 --它可能是也可能不是我正在启动的NWJS应用程序,它可能只是一个普通的exe。我不确定两者是否有不同的要求。

编辑:

我已经找到了这个,但我不知道如何调用这个代码点击我的按钮?或者让它成为一种功能?然后用onclick="myfunc()“调用函数名?

代码语言:javascript
复制
var exec = require('child_process').execFile;

exec('C:/asd/test.exe', function(err, data) {  
    console.log(err)
    console.log(data.toString());                       
});

EDIT2:

因此,我尝试了这个和几个变体,但仍然没有运气。我用onclick=从我的按钮上调用它“launchbat()”。

代码语言:javascript
复制
<script type="text/javascript">
    function launchbat() {
        var exec = require('child_process').execFile;

        exec('"C:\Users\Dustin Angeletti\OneDrive\Documents\bat.bat"', function(err, data) {  
            console.log(err)
            console.log(data.toString());                       
        });
    }
</script>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-06-04 00:29:48

弄明白了。非常简单,我的第一种方法和第二种方法工作得很好。我的问题一直是在真正的文件路径中使用反斜杠。你必须使用正斜杠。很奇怪。这适用于相对路径和绝对路径。

代码语言:javascript
复制
function AppLaunch7() {
    const path = require('path');
    nw.Shell.openItem(path.resolve('D:/SECURE DRIVE/SYSTEM CORE/SECURE CALC/SECURE CALC.exe'));
}

<div name="SystemWrapper1" id="SystemWrapper1" class="SystemWrapper1" onclick="AppLaunch7()">
    <input type="button" value="Documents" name="SystemInput1" id="SystemInput1" class="SystemInput1" />
    <span name="SystemIcon1" id="SystemIcon1" class="material-icons">description</span>
</div>
票数 0
EN

Stack Overflow用户

发布于 2022-05-27 03:23:07

这应该能行。尽管取决于您想要的内容,但是您可能希望使用require('child_process').spawn而不是require('child_process').exec

Vanilla JS:,您需要设置每个按钮(重复)并更新数组。

代码语言:javascript
复制
<button id="calculator">
  <i class="fa-solid fa-square-root-variable fa-lg"></i>
  Open Calculator
</button>

<button id="photoshop">
  <i class="fa-solid fa-aperture fa-lg"></i>
  Open Adobe Photoshop
</button>

<script>
const exec = require('child_process').exec;
const path = require('path');

const appMap = [
  {
    buttonId: 'calculator',
    executable: 'calc.exe'
  },
  {
    buttonId: 'photoshop',
    executable: path.join('C:', 'Program Files (x86)', 'Adobe', 'Photoshop', 'photoshop.exe')
  }
];

function executionCallback (err, stdout, stderr) {
  if (err) {
    console.log({ err });
  }
  if (stdout) {
    console.log({ stdout });
  }
  if (stderr) {
    console.log({ stderr });
  }
}

appMap.forEach(function (app) {
  const button = document.getElementById(app.buttonId)
  button.addEventListener('click', function () {
    exec(app.executable, {}, executionCallback);
  });
});
</script>

Vue.js:您可以使用Vue代替,这里您需要更新的只是数组,Vue将处理其他所有内容。

代码语言:javascript
复制
<div id="app">
  <button v-for="app in apps" @click="run(app.executable)">
    <i class="fa-solid fa-lg" :class="app.icon"></i>
    {{ app.label }}
  </button>
</div>

<script src="https://unpkg.com/vue@3.2.36/dist/vue.global.prod.js"></script>
<script>
const exec = window.nw.require('child_process').exec;
const path = window.nw.require('path');

const app = Vue.createApp({
  data: function () {
    return {
      apps: [
        {
          icon: 'fa-square-root-variable',
          label: 'Open Calculator',
          executable: 'calc.exe'
        },
        {
          icon: 'fa-aperture',
          label: 'Open Adobe Photoshop',
          executable: path.join('C:', 'Program Files (x86)', 'Adobe', 'Photoshop', 'photoshop.exe')
        }
      ]
    }
  },
  methods: {
    run: function (appExecutable) {
      const options = {};
      exec(appExecutable, options, function (err, stdout, stderr) {
        if (err) {
          console.log({ err });
        }
        if (stdout) {
          console.log({ stdout });
        }
        if (stderr) {
          console.log({ stderr });
        }
      });
    }
  }
}).mount('#app');
</script>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72398292

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档