[英]Determine if software is installed and what version on Mac in Node.js JavaScript
我在一个Electron应用程序中的Mac上的节点环境中,我需要:
在Windows中这一切都非常容易做到。 我在nodejs操作系统模块平台方法上进行了分支,因此如果使用“达尔文”,则需要执行上述操作。
我不是Mac用户,所以我对Mac上的进程了解不多。
如果需要的话,我可以解析.plist
文件,但在用户lib首选项文件夹中戳不出来。 有特定.psp
Photoshop的.psp
首选项文件,但我无法查看其中的内容,而只是检查文件夹中是否有Photoshop文件,这对我来说似乎很草率,而且我需要获取版本。
经过一番研究,我发现了所有Mac操作系统上都存在的mac system profiler实用程序。
使用node
的exec
模块,我得到了所有已安装的应用程序以及每个类似的详细信息
文本编辑:
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,以便于查询。
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以及是否有多个版本(即最新版本)。
这是需要改进的解析方法
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中有几个功能可以用来满足您的要求。 考虑炮击了必要的AppleScript / osascript
通过命令的NodeJS。
首先让我们看一下相关的AppleScript命令...
以下AppleScript代码段返回安装的Photoshop版本的名称(例如Photoshop CS5
, Photoshop CS6
, Photoshop CC
等)。 我们需要使用该名称才能成功启动该应用程序。
tell application "Finder" to get displayed name of application file id "com.adobe.Photoshop"
注意:如果未安装Photoshop,则上面的代码段错误,因此我们也可以利用它来确定是否已安装该应用程序。
以下代码片段获取安装的Photoshop版本:
tell application "Finder" to get version of application file id "com.adobe.Photoshop"
这将返回一个长字符串,指示版本 。 这将是一个虚拟的示例:
19.0.1 (19.0.1x20180407 [20180407.r.1265 2018/04/12:00:00:00) © 1990-2018 Adobe Systems Incorporated
推断已安装PhotoShop后,请考虑利用Bash的open
命令启动该应用程序。 例如:
open -a "Adobe Photoshop CC"
以下要点演示了如何在节点中利用上述命令。
注意:下面的要点是利用shelljs
的exec
命令执行AppleScript / osascript命令。 但是,您可以child_process.execSync()
内置在child_process.execSync()
或child_process.exec()
节点。
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');
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.