确定Node.js JavaScript中是否已安装软件以及Mac上的版本 [英] Determine if software is installed and what version on Mac in Node.js JavaScript

查看:111
本文介绍了确定Node.js JavaScript中是否已安装软件以及Mac上的版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Electron应用程序中的Mac上的节点环境中,我需要:

I am in a node environment on Mac within a Electron application and I am needing to:

  1. 测试是否已安装Photoshop
  2. 获取已安装的Photoshop版本
  3. 启动Photoshop

这一切在Windows中都非常容易做到.我在nodejs操作系统模块平台方法上进行了分支,因此如果使用达尔文",则需要执行上述操作.

This was all extremely easy to do in windows. I branch on nodejs os modules platform method so if 'Darwin' I need to do the above things.

我不是Mac用户,所以我对Mac上的进程了解不多.

I am not a Mac user so I do not know much about the processes on Mac.

如果需要,我可以解析.plist文件,但是在用户lib首选项文件夹中显示的内容很少.有特定于Photoshop的.psp首选项文件,但是我无法查看其中的内容,而只是检查文件夹中是否有Photoshop文件,这似乎让我显得草率,而且我需要获取版本.

I can parse .plist files if need be but poking around users lib preference folder hasn't showed up much. There are Photoshop specific .psp preference files but I have no way to see whats inside them and merely checking to see if there is a Photoshop file located in folder seems way to sloppy to me plus I need to get the version.

经过一番研究,我发现了 mac 系统分析器实用程序,该实用程序似乎存在于所有 mac 操作系统上.

After some research I came across the mac system profiler utility which seems to exist on all mac operating systems.

使用nodeexec模块,我获得了所有已安装的应用程序以及有关每个应用程序的一些详细信息

using node's exec module I get all installed applications and some details about each that look like

TextEdit:

Text

 Version: 1.13
 Obtained from: Apple
 Last Modified: 6/29/18, 11:19 AM
 Kind: Intel
 64-Bit (Intel): Yes
 Signed by: Software Signing, Apple Code Signing Certification Authority, Apple Root CA
 Location: /Applications/TextEdit.app

现在,我只需要编写一个简单的解析器,即可将包含335个以上应用程序的大型结果解析为json,以便于查询.

now I just needed to write a simple parser to parse the results which were large with over 335 applications into json for easy querying.

import { exec } from 'child_process';

let proc = exec( 'system_profiler SPApplicationsDataType -detailLevel mini' );
let results = '';
proc.stdout.on( 'data', ( data ) => { results += `${ data }`; } );
proc.on( 'close', async ( code ) =>
{
    let parsed = await this.sysProfileTxtToJson( results );
} );

sysProfileTxtToJson是我的小解析方法

现在parsed是我查询的json对象,以确定是否已安装photoshop以及是否有多个版本(最新版本).

now parsed is a json object that I query to determine if photoshop is installed and if multiple version which is the latest version.

这是需要改进的解析方法

here is the parsing method of which needs improved

sysProfileTxtToJson ( data: string )
{
    return new Promise<any>( ( res ) =>
    {
        let stream = new Readable();
        stream.push( data );
        stream.push( null );

        let lineReader = createInterface( stream );
        let apps = { Applications: [] };
        let lastEntry = '';
        let appPrefix = '    ';
        let appPropertyPrefix = '      ';
        let lastToggle, props = false;
        lineReader.on( 'line', ( line: string ) =>
        {
            if ( line == '' && !lastToggle )
            {
                props = false;
                return;
            }

            if ( line.startsWith( appPrefix ) && !props )
            {
                lastEntry = line.trim().replace( ':', '' );
                lastToggle = true;
                let current = {};
                current[ "ApplicationName" ] = lastEntry
                apps.Applications.push( current );
                props = true;
                return;
            }

            if ( line.startsWith( appPropertyPrefix ) && props )
            {
                lastToggle = false;
                let tokens = line.trim().split( ':' );
                let last = apps.Applications[ apps.Applications.length - 1 ];
                last[ tokens[ 0 ] ] = tokens[ 1 ].trim();
            }
        } );

        lineReader.on( 'close', () =>
        {
            res( apps );
        } );
    } );
}

推荐答案

AppleScript中有几个功能可以用来满足您的要求.考虑通过nodejs 掏空必要的AppleScript/ osascript 命令

There are several features in AppleScript which can be utilized to achieve your requirement. Consider shelling out the necessary AppleScript/osascript commands via nodejs.

首先让我们看一下相关的AppleScript命令...

Let's firstly take a look at the pertinent AppleScript commands...

  1. 以下AppleScript代码段返回安装的Photoshop版本的名称(例如Photoshop CS5Photoshop CS6Photoshop CC等).我们需要名称才能成功启动该应用程序.

  1. The following AppleScript snippet returns the name of whichever version of Photoshop is installed (E.g. Photoshop CS5, Photoshop CS6, Photoshop CC, etc ...). We'll need the name to be able to successfully launch the application.

tell application "Finder" to get displayed name of application file id "com.adobe.Photoshop"

注意:如果未安装 ,则上面的代码段会出错,因此我们还可以利用它来确定是否已安装该应用程序.

Note: The snippet above errors if Photoshop is not installed, so we can also utilize this to determine if the application is installed or not.

以下代码段获取安装的Photoshop版本:

The following snippet obtains whichever version of Photoshop is installed:

tell application "Finder" to get version of application file id "com.adobe.Photoshop"

这将返回一个长字符串,指示版本.这将是一个虚构的示例:

This returns a long String indicating the version. It will be something like this fictitious example:

19.0.1 (19.0.1x20180407 [20180407.r.1265 2018/04/12:00:00:00) © 1990-2018 Adobe Systems Incorporated


启动Photoshop:

推断出已安装PhotoShop之后,请考虑使用Bash的 open 命令来启动应用程序.例如:


Launching Photoshop:

After it has been inferred that PhotoShop is installed consider utilizing Bash's open command to launch the application. For instance:

open -a "Adobe Photoshop CC"


节点应用示例:

以下要点演示了如何在节点中利用上述命令.


Example node application:

The following gist demonstrates how the aforementioned commands can be utilized in node.

注意:下面的要点是利用 shelljs child_process.execSync()

Note: The gist below is utilizing shelljs's exec command to execute the AppleScript/osascript commands. However you could utilize nodes builtin child_process.execSync() or child_process.exec() instead.

const os = require('os');
const { exec } = require('shelljs');

const APP_REF = 'com.adobe.Photoshop';
const isMacOs = os.platform() === 'darwin';

/**
 * Helper function to shell out various commands.
 * @returns {String} The result of the cmd minus the newline character.
 */
function shellOut(cmd) {
  return exec(cmd, { silent: true }).stdout.replace(/\n$/, '');
}


if (isMacOs) {

  const appName = shellOut(`osascript -e 'tell application "Finder" \
      to get displayed name of application file id "${APP_REF}"'`);

  if (appName) {
    const version = shellOut(`osascript -e 'tell application "Finder" \
        to get version of application file id "${APP_REF}"'`).split(' ')[0];

    console.log(version); // Log the version to console.

    shellOut(`open -a "${appName}"`); // Launch the application.
  } else {
    console.log('Photoshop is not installed');
  }
}

这篇关于确定Node.js JavaScript中是否已安装软件以及Mac上的版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆