node-clipboard-event icon indicating copy to clipboard operation
node-clipboard-event copied to clipboard

`clipboard-event-handler-mac` does not start properly on Mac

Open ZiuChen opened this issue 2 years ago • 7 comments

clipboard-event-handler-mac does not start properly on Mac.

Processes are automatically killed when executed in the terminal.

If you use the following terminal command before executing:

chmod +x ./clipboard-event-handler-mac

you will get an error: clipboard-event-handler-mac will harm your computer and you should move it to the Trash.

Other than that, it executes fine under Linux and Win.

I don't know how to fix this, if anyone could provide a little advice, I'd be grateful! 😢

ZiuChen avatar Sep 21 '22 07:09 ZiuChen

You can use the source file clipboard-event-handler-mac.swift to compile the executable file then execute in the terminal.

NPCRay avatar Oct 11 '22 12:10 NPCRay

I am also facing a similar issue. Manually compiling clipboard-event-handler-mac.swift using command swiftc -o clipboard-event-handler-mac clipboard-event-handler-mac.swift works in development but I am not sure how to go with this in production.

maulik9898 avatar Oct 30 '22 08:10 maulik9898

使用命令 sudo codesign --force --deep --sign - clipboard-event-handler-mac 签名即可运行了.

  • tips: 上述命令依赖 xcode ,可使用 xcode-select --install 安装 xcode

Jonnyan404 avatar Nov 01 '22 11:11 Jonnyan404

使用命令sudo codesign --force --deep --sign - clipboard-event-handler-mac签名即可运行了。

  • tips: 上记命命令依赖xcode ,可用xcode-select --install安装xcode

请问是要把 clipboard-event-handler-mac 提到自己项目目录内吗 能请求一份示例吗,我代码如下: ` const child = exec('sudo codesign --force --deep --sign - clipboard-event-handler-ma');

if (child.stdout) { child.stdout.on('data', (data) => { console.log('node-clip-event@data:', data);

    if (data.trim() === 'CLIPBOARD_CHANGE') {
      console.log('text:', data)
    }
  });
}

` 仍然无法正常启动。

Akira0705 avatar Jan 10 '23 06:01 Akira0705

I fixed this when running locally by using what others said above: chmod +x ./clipboard-event-handler-mac

When packaging for production with electron-builder, I saw this error when attempting to run the Mac executable directly post-packaging:

Error: ENOENT, dist/main/platform/clipboard-event-handler-mac not found in /Users/alec/Desktop/alec-dev/<app_name>/packages/desktop/release/build/mac/<app_name>.app/Contents/Resources/app.asar
    at createError (node:electron/js2c/asar_bundle:2:1265)
    at func (node:electron/js2c/asar_bundle:2:1984)
    at execFile (node:electron/js2c/asar_bundle:2:2336)
    at EventEmitter.startListening (/Users/alec/Desktop/alec-dev/<app_name>/packages/desktop/release/build/mac/<app_name>.app/Contents/Resources/app.asar/dist/main/webpack:/desktop/node_modules/clipboard-event/index.cjs:21:20)
    at /Users/alec/Desktop/alec-dev/<app_name>/packages/desktop/release/build/mac/<app_name>.app/Contents/Resources/app.asar/dist/main/webpack:/desktop/src/main/main.ts:193:23 {
  code: 'ENOENT',
  errno: -2
}

Meaning the packaged build can't resolve this file, which I believe is the root cause here.

To fix this, make sure your executable files are referenced in your package.json's extraResources:

{
  ...
  "build": {
    "extraResources": [ "PATH/TO/EXECUTABLE/FILE", ... ]
  }
  ...
}

After this make sure ClipboardListener.js is referencing the correct file path to these executables and for me, the packaged build finally runs successfully.

Note:

  • I have not code signed anything yet
  • I copied clipboard-listener.js and all executable files into my main package, outside of my node_modules to edit the path they were referencing (for me "extraResources": ["./assets/**"], then I use path.join(process.resourcesPath, 'assets', clipboard-event-handler-mac) in clipboard-listener.js
  • There may be a smarter way to do this that references node_module
  • I have not tested on Windows/Linux

AlecKriebel avatar Jun 14 '23 15:06 AlecKriebel

@AlecKriebel Thank you. It works for me on yarn package and yarn make now, but still doesn't work on yarn start, but half of the problem is gone.

marisukukise avatar Jun 28 '23 13:06 marisukukise

Hours and hours of debuging finally with the response of @AlecKriebel I could execute on production with electron-builder.

My files: image

My package.json:

"build": {
    "appId": "com.XXXX.XXXXX",
    "icon": "img/icon",
    "extraResources": [
      "./clipboard-event/**",
      "./assets/**"
    ],
    "win": {
      "certificateFile": "certificados/windows/certificate.pfx",
      "certificatePassword": "123",
      "target": [
        "nsis"
      ]
    },
    "nsis": {
      "oneClick": false,
      "perMachine": false,
      "allowElevation": true,
      "createDesktopShortcut": "always",
      "createStartMenuShortcut": true,
      "runAfterFinish": true
    },
    "mac": {
      "target": [
        "dmg",
        "zip"
      ],
      "hardenedRuntime": true,
      "gatekeeperAssess": false,
      "entitlements": "./certificados/macos/entitlements.plist"
    },
    "linux": {
      "target": [
        "AppImage"
      ]
    }
  }

My index.js of Clipboard-event dependency downloaded as part of my code:

startListening() {
        try {
            const { platform } = process;
            if (platform === 'win32') {
                this.child = execFile(path.join(__dirname, 'platform/clipboard-event-handler-win32.exe'));
            } else if (platform === 'linux') {
                this.child = execFile(path.join(__dirname, 'platform/clipboard-event-handler-linux'));
            } else if (platform === 'darwin') {
                const filePath = path.join(process.resourcesPath, 'assets', 'clipboard-event-handler-mac');
                logger.info('filePath:', filePath);
                fs.chmodSync(filePath, '755');
                this.child = execFile(filePath);
            } else {
                throw new Error('Unsupported platform: ' + platform);
            }

            this.child.stdout.on('data', (data) => {
                if (data.trim() === 'CLIPBOARD_CHANGE') {
                    this.emit('change');
                }
            });

            this.child.on('error', (error) => {
                throw new Error('Error with the child process: ' + error.message);
            });

        } catch (error) {
            console.error('Error in startListening:', error.message);
        }
    }

adrianpisabarrogarcia avatar Oct 09 '23 08:10 adrianpisabarrogarcia