电子-MAC保持在基座中 [英] Electron - MAC keep in dock

查看:62
本文介绍了电子-MAC保持在基座中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有办法将我开发的Electron应用程序保留在扩展坞中?我的目标是让用户下载.app文件,启动该文件(它会自动执行),然后在关闭它们后在MAC"Keep In Dock"上保留该文件.我知道可以使用ockerutil完成此操作,但是我需要一种在应用程序中完成此操作的方法.

Is there a way to keep my Electron app I developed in the dock? My goal is to have the user download the .app file, launch it (which it does automatically) and then on the MAC "Keep In Dock" after they close it. I know this can be done with dockutil, but I need a way to do it within the application.

推荐答案

以下是一些改进的

Here is some improved code which performs an initial test (defaults read piped to grep) to ensure that only one instance of the app gets permanently kept in the Dock. It has been successfully tested on both macOS Yosemite and El Capitan...

const electron = require ('electron');
const app = electron.app || electron.remote.app;
const path = require ('path');
const url = require ('url');
const { spawnSync } = require ('child_process');
//
function isAppInDock (appURL)
{
    let isInDock = false;
    let defaults = spawnSync ('defaults', [ 'read', 'com.apple.dock', 'persistent-apps' ], { encoding: 'utf8' });
    if (!defaults.error)
    {
        let pattern = `"_CFURLString" = "${appURL}"`;
        let grep = spawnSync ('grep', [ '-F', pattern ], { input: defaults.stdout, encoding: 'utf8' });
        if (!grep.error)
        {
            if (grep.stdout.length)
            {
                isInDock = true;
            }
        }
    }
    return isInDock;
}
//
function keepAppInDock (appURL)
{
    let entry = `<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>${appURL}</string><key>_CFURLStringType</key><integer>15</integer></dict></dict></dict>`;
    let defaults = spawnSync ('defaults', [ 'write', 'com.apple.dock', 'persistent-apps', '-array-add', entry ], { encoding: 'utf8' });
    if (!defaults.error)
    {
        let killall = spawnSync ('killall', [ 'Dock' ], { encoding: 'utf8' });
    }
}
//
let appPackagePath = path.join (app.getPath ('exe'), '..', '..', '..');
let appPackageURL = encodeURI (url.format ({ protocol: 'file', slashes: true, pathname: appPackagePath })) + '/';
//
if (!isAppInDock (appPackageURL))
{
    keepAppInDock (appPackageURL)
}

这篇关于电子-MAC保持在基座中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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