pkgbuild open URL using default browser

I use pkgbuild to create an Apple installer .pkg.

In my preinstall script, I have the following, which opens in Chrome if I invoke it myself, however when run by pkgbuild script, always opens the above link using Safari:

open "https://apple.stackexchange.com/"

I tried to force it to run as the current user:

sudo -u "$(logname || echo "$SUDO_USER")" open "https://apple.stackexchange.com/"

... but it still uses Safari.

My system default is currently Chrome. I changed it to Firefox, and the same thing occurs.

How do I open this URL using the default browser within a pkgbuild script?


Solution 1:

After echoing some values of preinstall to a temp file and some advice from comments in https://stackoverflow.com/questions/35619036/, I found the following:

  • 🚫$SUDO_USER is empty
  • 🚫logname is root
  • ✅$USER has the correct value

So adopting the script as follows fixes it. The link is opened in the default browser.

sudo -u "$USER" open "https://apple.stackexchange.com/"

As an aside, the following I had expected to work but it DID NOT work. This technique still opens in Safari, unfortunately.

su "$USER" -c "open https://apple.stackexchange.com"

So, use sudo -u "$USER" and it should work.