首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何检测应用程序是否从dmg文件中运行?

如何检测应用程序是否从dmg文件中运行?
EN

Stack Overflow用户
提问于 2020-12-17 18:10:27
回答 1查看 565关注 0票数 5

我们的一些用户似乎从dmg文件中运行该应用程序,并且从未将其移动到他们的/Applications文件夹中:

这或许可以解释为什么我们在内部日志中看到这个消息:

代码语言:javascript
运行
复制
Cannot update while running on a read-only volume. The application is on a read-only volume. Please move the application and try again. If you're on macOS Sierra or later, you'll need to move the application out of the Downloads directory. See https://github.com/Squirrel/Squirrel.Mac/issues/182 for more information.

(我访问了GitHub问题,但没有发现任何有用的东西。)

我肯定可以通过将dmg、cd挂载到其中并从命令行运行应用程序来再现此错误消息:

代码语言:javascript
运行
复制
# I mounted the dmg and then cd into it, see:
$ pwd
/Volumes/**REDACTED**2.42.0-beta.5

# Then I launched the app from the command line, see:
$ ./**REDACTED**.app/Contents/MacOS/**REDACTED**
Found version 2.42.1-beta.0 (url: **REDACTED**-2.42.1-beta.0.zip, **REDACTED**-2.42.1-beta.0.dmg, **REDACTED**-2.42.1-beta.0.dmg)
Downloading update from **REDACTED**-2.42.1-beta.0.zip, **REDACTED**-2.42.1-beta.0.dmg, **REDACTED**-2.42.1-beta.0.dmg
updater cache dir: /Users/**REDACTED**/Library/Application Support/Caches/**REDACTED**-updater
Update has already been downloaded to /Users/**REDACTED**/Library/Application Support/Caches/**REDACTED**-updater/pending/**REDACTED**-2.42.1-beta.0.zip).
/ requested
[Error: Cannot update while running on a read-only volume. The application is on a read-only volume. Please move the application and try again. If you're on macOS Sierra or later, you'll need to move the application out of the Downloads directory. See https://github.com/Squirrel/Squirrel.Mac/issues/182 for more information.] {
  code: 8,
  domain: 'SQRLUpdaterErrorDomain'
}

当应用程序运行时,后台进程会检查更新情况。我们可以看到它找到了一个并下载了它。但是它不能应用它。

我们定期发布更新,但这些用户无法获得它们。我怀疑他们中的一些人甚至没有意识到他们没有“正确”安装这个应用程序。

,我们有办法用电子或Node.js?来检测吗?

EN

Stack Overflow用户

发布于 2022-03-17 19:09:03

这个答案来自我的电子食谱库:

https://customcommander.github.io/electron-survival-guide/

TL;DR

您可以使用app.isInApplicationsFolder()方法。

主要过程

渲染程序进程可以通过询问Applications IPC通道来询问应用程序是否从in-app-folder文件夹中运行。主要进程将在同一渠道作出反应。

代码语言:javascript
运行
复制
const {app, BrowserWindow, ipcMain} = require('electron');
const path = require('path');

app.whenReady().then(async () => {
  const bwin = new BrowserWindow({
    width: 300,
    height: 300,
    webPreferences: {
      nodeIntegration: false,
      contextIsolation: true,
      preload: path.resolve(__dirname, 'preload.js')
    }
  });

  // Must be registered **before** the renderer process starts.
  ipcMain.handle('in-app-folder', () => app.isInApplicationsFolder());

  await bwin.loadFile('renderer.html');
  bwin.show();
});

预加载脚本

预加载脚本在MY_APP命名空间下公开一个方法,呈现程序进程可以使用该方法来知道应用程序是否从Applications文件夹运行。

代码语言:javascript
运行
复制
const {contextBridge, ipcRenderer} = require('electron');

contextBridge.exposeInMainWorld('MY_APP', {
  async isInApplicationsFolder() {
    return ipcRenderer.invoke('in-app-folder');
  }
});

渲染器

MY_APP命名空间在预加载脚本中定义,并包含一个查询应用程序位置的方法。

代码语言:javascript
运行
复制
<html>
  <head>
    <style>
      body {background-color:black;color:limegreen}
    </style>
  </head>
  <body>
    <h1>Are you running this app from the Applications folder?</h1>
    <div id="response"></div>
    <script>
      MY_APP.isInApplicationsFolder().then(yes => {
        if (yes) {
          document.querySelector('#response').innerHTML = 'Yes';
        } else {
          document.querySelector('#response').innerHTML = 'No';
        }
      });
    </script>
  </body>
</html>

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65346127

复制
相关文章

相似问题

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