skip to Main Content

please let me know a working way to auto-launch an application after installing the "pkg" package on macOS.
I’ve tried this: http://macinstallers.blogspot.com/2012/07/scripting-in-installer-packages.html
its doesnt work. Script doesnt work after installation.

package.json:

"name": "test",
"version": "1.0.0",
"description": "test",
"main": "main.js",
build": {
"mac": {
"target": [
  {
    "target": "pkg",
    "arch": [
      "x64"
    ]
  }
]
},
"pkg": {
"scripts": "./scripts/postinstall.sh"
}
}

postinstall.sh:

#!/bin/sh
open -a test
exit 0

2

Answers


  1. Chosen as BEST ANSWER

    @Ridham Patel,

    Tried to your "Use a postinstall script" method, unfortunately its doesn't work.

    Heres my package.json:

        "description": "test",
        "main": "main.js",
        "build": {
        "mac": {
          "target": [
            {
              "target": "pkg",
              "arch": [
                "x64"
              ]
            }
          ]
        },
        "pkg": {
          "scripts": {
            "postinstall": "./scripts/postinstall.sh"
          }
        }
      }
    
    Heres my postinstall.sh:
    #!/bin/sh
    open -a test
    
    Made my scripts executable (`chmode +x build/scripts/postinstall.sh`)
    
    Got error:
       configuration.pkg.scripts should be:
       null | string
       -> The scripts directory, relative to `build` (build resources directory).
       The scripts can be in any language so long as the files are marked 
       executable and have the appropriate shebang indicating the path to the 
       interpreter.
       Scripts are required to be executable (`chmod +x file`).
        
    

  2. Use a launch agent

    1. Create a .plist file in ~/Library/LaunchAgents with the path to your app executable and desired launch parameters.

    2. Set the plist to load on login by running:

      launchctl load ~/Library/LaunchAgents/com.yourcompany.appname.plist

    This will launch your app automatically when a user logs in.

    Use a postinstall script

    The postinstall script approach you tried can work, but the script needs to be executable. To fix:

    1. Add postinstall_script.sh to your Electron project

    2. Make sure it has a shebang line #!/bin/sh

    3. Make it executable with chmod +x postinstall_script.sh

    4. Reference it in your package.json:

      "scripts": {
      "postinstall": "./postinstall_script.sh"
      }

    5. Script should contain:

      open -a YourApp

    Use an install phase script

    1. Add a shell script to run your app to resources/installer.scpt

    2. Reference it in your .pkg definition:

      resources/installer.scpt

    This will launch your app when the install completes.

    So in summary, you can auto-launch an Electron app on install using launch agents, postinstall scripts, or install phase scripts in your .pkg. The launch agent approach runs it on login, while the others launch once on install.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search