Posted on 05-31-2020 07:19 PM
Hi all,
Am trying to create a script to copy files from /Library/Shared/TeamsBackground to the User's Microsoft Teams folder.
As a test case, i deliberately do not have Backgrounds and Backgrounds/Uploads folders in the User's Teams directory. When i ran the script, it just went to the last Else statement "Teams Never Ran". I know for a fact that the Teams folder ~/Library/Application Support/Microsoft/Teams exist.
What am I missing?
Thanks
#!/bin/sh
CURRENTUSER=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");'`
if [[ -f "/Users/"${CURRENTUSER}"/Library/Application Support/Microsoft/Teams/Backgrounds/Uploads/file1.jpg" ]]
then
echo "Already copied to the user's folder"
exit 0
else
if [[ -d "/Users/"${CURRENTUSER}"/Library/Application Support/Microsoft/Teams/Backgrounds/Uploads" ]]
then
echo "Upload folder exist"
sudo "cp /Library/Shared/TeamsBackground/*" "/Users/"${CURRENTUSER}"/Library/Application Support/Microsoft/Teams/Backgrounds/Uploads"
exit 0
else
if [[ -d "/Users/"${CURRENTUSER}"/Library/Application Support/Microsoft/Teams/Backgrounds" ]]
then
echo "Backgrounds folder exist"
sudo mkdir /Users/"${CURRENTUSER}"/Library/Application Support/Microsoft/Teams/Backgrounds/Uploads
sudo cp "/Library/Shared/TeamsBackground/*" "/Users/"${CURRENTUSER}"/Library/Application Support/Microsoft/Teams/Backgrounds/Uploads"
exit 0
else
if [[ -d "/Users/"${CURRENTUSER}"/Library/Application Support/Microsoft/Teams" ]]
then
echo "Teams Folder Exist, dumping the files"
sudo mkdir /Users/"${CURRENTUSER}"/Library/Application Support/Microsoft/Teams/Backgrounds
sudo mkdir /Users/"${CURRENTUSER}"/Library/Application Support/Microsoft/Teams/Backgrounds/Uploads
sudo cp "/Library/Shared/TeamsBackground/*" "/Users/"${CURRENTUSER}"/Library/Application Support/Microsoft/Teams/Backgrounds/Uploads"
exit 0
else
echo "Teams Never Ran"
exit 0
fi
fi
fi
fi
exit 0
Posted on 05-31-2020 07:40 PM
@esutedy Don't quote "${CURRENTUSER}"
inside something that's already quoted, and don't escape spaces if the path is quoted. i.e. the first conditional should be:
if [[ -f "/Users/${CURRENTUSER}/Library/Application Support/Microsoft/Teams/Backgrounds/Uploads/file1.jpg" ]]
(You have multiple incorrect uses of this pattern)
Don't use sudo in a script being run from Jamf as it's already running in the root context so this
sudo "cp /Library/Shared/TeamsBackground/*" "/Users/"${CURRENTUSER}"/Library/Application Support/Microsoft/Teams/Backgrounds/Uploads"
should be
cp "/Library/Shared/TeamsBackground/*" "/Users/${CURRENTUSER}/Library/Application Support/Microsoft/Teams/Backgrounds/Uploads/"
Your mkdir
commands need to ve followed by chown
commands to assign the new folders to the user whose directory you're creating them in
Don't use the deprecated backtick (`) pattern to wrap a call you want to assign to a variable. i.e. this line
CURRENTUSER=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");'`
should be:
CURRENTUSER=$(python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");')
Posted on 06-01-2020 12:36 AM
Thanks @sdagley . worked like a charm.
Thank you for your chown advice as well. I didnt realise that the user themselves need full access to their own Teams folders...
Posted on 06-01-2020 05:24 AM
@esutedy Good to hear you got it working. I should ahve also mentioned a couple of sites you'll find useful when it comes to tips on writing/troubleshooting scripts:
Scripting OS X - Definitely articles on what the name implies, plus lots of other Mac related info
ShellCheck - A tool for analyzing scripts that will quickly point out syntax errors, as well as semantic issues that aren't so obvious
Posted on 01-19-2023 08:21 AM
@esutedy glad this worked out for you. I'm using this as a reference to do something similar but for a different app's set of user folders. Would you mind sharing the script? I'm getting "python: command not found" in the logs. Plus i'm not sure where and what to add the CHOWN command that @sdagley mentioned.
thank you in advanced!
Posted on 01-19-2023 09:58 AM
@tc_pmg Python is no longer a default install in macOS, so a different mechanism for finding the current user is needed. See this post on Scripting OS X for one of those: https://scriptingosx.com/2020/02/getting-the-current-user-in-macos-update/
Do a 'man chown' in Terminal to see what the command parameters are. You need to use that command to assign ownership of the files/folders otherwise the user may not have access to them after they're copied.
Posted on 02-09-2023 01:32 AM
i m really intersted by thi sscript cause i looking for a way to pusgh Teams Background sloothly, and when i use Composer, the results do not match correctly
Posted on 02-10-2023 07:06 AM
I was able to accomplish what I wanted. I'm sure there are other ways to do this but it was the fastest and least complicated way for me.