Multiple user accounts in JOSM
Posted by M!dgard on 5 July 2023 in English. Last updated on 6 July 2023.I started taking on mapping commissions and I want to do those with a different user account. JOSM doesn’t have built-in support for multiple user accounts, so I created a script for it that lets me switch without pain. It works on a typical Linux setup or other *NIXes such as macOS, not on Windows.
It modifies JOSM’s preferences.xml file to change your credentials, and then launches JOSM. You create one copy of this script for each OSM user account, and run those instead of launching JOSM the normal way.
I created two files with this script in a directory in my PATH and made them executable: once as ~/.local/bin/josm and once as ~/.local/bin/josm_commissioned. For Linux desktops: If you want, you can also create .desktop files in ~/.local/share/applications so you can run these scripts easily from your main menu or launcher. Feel free to share those in the comments!
Find the OAuth key and secret in JOSM’s preferences XML file, and fill them in in the script. Especially for macOS, you may need to change the JOSM_PREFS and JOSM_EXECUTABLE paths.
#!/bin/bash
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n'
OAUTH_KEY=changeme3G45hi67Jkl8mn901op2qR3st4U5vw8X
OAUTH_SECRET=changeme3G45hi67Jkl8mn901op2qR3st4U5vw8X
JOSM_PREFS="$HOME/.josm/preferences.xml"
JOSM_EXECUTABLE="/usr/bin/josm"
notify() {
echo "$1" >&2
if which wl-overlay >/dev/null 2>&1; then
# https://git.sr.ht/~midgard/wl-overlay/
wl-overlay --time=1000 "" "$1"
elif which notify-send >/dev/null 2>&1; then
# freedesktop.org
notify-send -t 1000 "$1"
elif which osascript >/dev/null 2>&1; then
# macOS
osascript -e "display alert \"$1\""
fi
}
if ps ax | grep -i '[j]ava -jar .*josm'; then
notify "JOSM still running"
exit 1
fi
sed "
s+<tag https://wiki.openstreetmap.org/wiki/Tag:key='oauth\\.access-token\\.key' https://wiki.openstreetmap.org/wiki/Tag:value='[0-9a-zA-Z]*'/>+<tag https://wiki.openstreetmap.org/wiki/Tag:key='oauth.access-token.key' https://wiki.openstreetmap.org/wiki/Tag:value='$OAUTH_KEY'/>+
s+<tag https://wiki.openstreetmap.org/wiki/Tag:key='oauth\\.access-token\\.secret' https://wiki.openstreetmap.org/wiki/Tag:value='[0-9a-zA-Z]*'/>+<tag https://wiki.openstreetmap.org/wiki/Tag:key='oauth.access-token.secret' https://wiki.openstreetmap.org/wiki/Tag:value='$OAUTH_SECRET'/>+
" "$JOSM_PREFS" > "${JOSM_PREFS}.new" && mv "${JOSM_PREFS}.new" "$JOSM_PREFS"
"$JOSM_EXECUTABLE" "$@"
Discussion
Comment from mmd on 5 July 2023 at 18:31
Why don’t you use josm.home? Seems much easier to me to manage multiple profiles/users… https://community.openstreetmap.org/t/mehrere-josm-profile/74386
Comment from M!dgard on 5 July 2023 at 20:24
mmd: To keep my settings when I switch between accounts.
Comment from SherbetS on 6 July 2023 at 11:44
so, to switch the accounts you just run the script?
I frequently have to switch accounts when editing, and I’d love to see a solution beyond typing in my login in basic authentication.
Comment from M!dgard on 6 July 2023 at 12:39
The script also launches JOSM. If you’d rather use the script to just switch profiles, and then manually start JOSM, you can simply remove the last line.
Comment from SherbetS on 6 July 2023 at 12:47
When I’m changing accounts, it’s right after I make edits with the first account. I feel that the time it would take to close the program and relaunch it would be slower than just typing in the login once more. The feature I’d really like to have is if the devs added a list of accounts logged in and you could just open settings and select one from the list.
Comment from mmd on 6 July 2023 at 13:47
Switching accounts inside JOSM isn’t exactly a new idea: https://josm.openstreetmap.de/ticket/2710
Comment from M!dgard on 6 July 2023 at 14:15
SherbetS: yes, that would be even better. Changing the preferences file while JOSM is running does not work, though. (Hence the check at line 25 to avoid nasty surprises.) Let me know if you find a solution!