1

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?

3
  • 1
    The installer context doesn’t know about your default browser and will always use Safari because that is all it knows about – John Keates Mar 18 '20 at 5:58
  • 1
    Does the package require administrator rights? – Graham Miln Mar 18 '20 at 8:26
  • 1
    @GrahamMiln yes, the .pkg will run as root by default. What wasn't obvious was the logname and $SUDO_USER were not working. Adapting some hints on another thread, I was able to find that sudo -u $USER did the trick. The solution is posted below,. – tresf Mar 18 '20 at 17:18
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .