I have a script I am using for creating website shortcuts on the desktop. My manager doesn't like that they all end in .webloc and asked if I can hide the extension, which can be done in the Finder. The only way I found to do this is using AppleScript command set extension hidden. I have no problem running the following script as myself (removing the sudo -u $logged_in_user). It creates the shortcut and hides the webloc extension. But no matter what I cannot figure out how to get this to work when running the script as root. Since the script will run from JAMF it will always run as root. Any help with this would be appreciated.
#!/bin/zsh
logged_in_user=$(/usr/sbin/scutil <<< "show State:/Users/ConsoleUser" | awk '/Name
&& ! /loginwindow/ {print $3}')
webloc_filename="Apple"
webloc_address="https://www.apple.com/"
shortcutPath="/Users/${logged_in_user}/Desktop/${webloc_filename}.webloc"
webloc_data='<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>URL</key>
<string>'$webloc_address'</string>
</dict>
</plist>'
cat << EOWEBLOC > "${shortcutPath}"
$webloc_data
EOWEBLOC
sudo -u $logged_in_user osascript - "$shortcutPath" << 'EOF'
on run argv
tell application "Finder" to set extension hidden of (POSIX file (item 1 of argv) as alias) to true
end run
EOF