【问题标题】:How to get app name / metadata from file path如何从文件路径获取应用程序名称/元数据
【发布时间】:2020-01-10 05:04:55
【问题描述】:

我有应用程序/可执行文件路径,我想从中获取友好的应用程序名称。想象一个像这样的getAppName 函数:

const result = getAppName('C:\\Program Files\\Mozilla Firefox\\firefox.exe');
console.log(result); // Firefox

例如,在Windows上,如果您右键单击该应用并单击“属性”,则可以在常规>描述和详细信息>文件描述中看到此名称;

我在寻找 npm 包、系统命令等方面没有运气。最后,我需要一个适用于 Linux、Mac 和 Windows 的解决方案。理想情况下,有一种方法是通过内置的 Node.js 包或 npm 上的其他东西,但我可能不得不拼凑一个解决方案,使用我从 Node.js 执行的三个特定于操作系统的系统命令。

我找到了适用于 Windows 的 WMIC(在 How to check file properties through WMI command line 中),但我无法从中得到我想要的;

wmic datafile where name="C:\\Program Files\\Mozilla Firefox\\firefox.exe" get Description

Description
C:\Program Files\Mozilla Firefox\firefox.exe
wmic datafile where name="C:\\Program Files\\Mozilla Firefox\\firefox.exe" get Name

Name
C:\Program Files\Mozilla Firefox\firefox.exe
wmic datafile where name="C:\\Program Files\\Mozilla Firefox\\firefox.exe" get File Description

Invalid GET Expression.
wmic datafile where name="C:\\Program Files\\Mozilla Firefox\\firefox.exe" get File Name

Invalid GET Expression.
wmic datafile where name="C:\\Program Files\\Mozilla Firefox\\firefox.exe" get FileName

FileName
firefox
wmic datafile where name="C:\\Program Files\\Mozilla Firefox\\firefox.exe" get FileDescription

Node - XPS
ERROR:
Description = Invalid query
wmic datafile where name="C:\\Program Files\\Mozilla Firefox\\firefox.exe" get ProductName

Node - XPS
ERROR:
Description = Invalid query
wmic datafile where name="C:\\Program Files\\Mozilla Firefox\\firefox.exe"

AccessMask  Archive  Caption                                       Compressed  CompressionMethod  CreationClassName  CreationDate               CSCreationClassName   CSName  Description                                   Drive  EightDotThreeFileName                         Encrypted  EncryptionMethod  Extension  FileName  FileSize  FileType     FSCreationClassName  FSName  Hidden  InstallDate                InUseCount  LastAccessed               LastModified               Manufacturer         Name                                          Path                             Readable  Status  System  Version      Writeable
1179817     TRUE     C:\Program Files\Mozilla Firefox\firefox.exe  FALSE                          CIM_LogicalFile    20190906213704.287820+060  Win32_ComputerSystem  XPS     C:\Program Files\Mozilla Firefox\firefox.exe  c:     c:\program files\mozilla firefox\firefox.exe  FALSE                        exe        firefox   576032    Application  Win32_FileSystem     NTFS    FALSE   20190906213704.287820+060              20190907195815.492271+060  20190906213710.011611+060  Mozilla Corporation  C:\Program Files\Mozilla Firefox\firefox.exe  \program files\mozilla firefox\  TRUE      OK      FALSE   69.0.0.7178  TRUE
C:\Users\adaml>wmic datafile where name="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe" get "file system name"

FSName
NTFS
C:\Users\adaml>wmic datafile where name="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe" get "file system class name"

FSCreationClassName
Win32_FileSystem

我不想做任何事情,比如采用无扩展名的文件名并将第一个字符大写。我想要向用户显示的准确/真实姓名。

【问题讨论】:

  • 对于 macOS,使用 Objective-C 或 Swift,您将在路径(或 URL)处获得项目的“显示名称”。例如。 [[NSFileManager defaultManager] displayNameAtPath:path]。 (这就是 Finder 和 Dock 中显示的内容。请注意,当应用程序运行时,菜单栏中的应用程序菜单标题可能会显示不同的名称。)

标签: node.js linux windows macos desktop-application


【解决方案1】:

我不确定这是否涵盖了所有内容,但这是迄今为止我所获得的最好的。 exiftool-vendored 包允许你得到一个 ProductName “标签”,这正是我要找的;

const exiftool = require('exiftool-vendored').exiftool;

exiftool
  .read('C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe')
  .then(tags =>
    console.log(tags.ProductName))
  .catch(err => console.error('Something terrible happened: ', err));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-25
    • 2013-06-06
    • 2015-05-03
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    相关资源
    最近更新 更多