Posted on 12-07-2018 12:49 PM
By default, new Finder windows open to Recents, but we wanted new Finder windows to open to the user's OneDrive folder instead. You can set this by hand in Finder > Preferences > General, but I wanted a way to scrip it to deploy it to all of our Mac users. The script I ultimately arrived at is fairly simple, but figuring out exactly how to put it together took quite a bit of trial and error, so I wanted to share this in case anyone else is interested in doing this. In addition to trial and error I'm indebted to the Jamf Nation community. Looking through other threads where people were looking to modify NewWindowTarget was very helpful. Also worth mentioning that users can still modify this in Finder > Preferences if they prefer their new Finder windows to open in a different folder. Here's the script:
# Set new Finder windows to show the user’s OneDrive folder
#!/bin/bash
# Finds current username and defines a variable
currentuser=`stat -f "%Su" /dev/console`
# Sets Finder preference to always open new Finder windows to the user's OneDrive folder
su "$currentuser" -c "defaults write com.apple.finder NewWindowTarget -string 'PfLo'"
su "$currentuser" -c "defaults write com.apple.finder NewWindowTargetPath -string 'file://localhost/Users/$currentuser/OneDrive folder name here/'"
Note the last line. Where it says "OneDrive folder name here" you'll have to specify the actual folder name. OneDrive creates a folder that includes the organization name (at least as we've configured it via Office 365), so it'll be different for each organization. It's also possible that your OneDrive folder is in a different location than the Home folder, though the Home folder is the default location. In our organization the vast majority of users have their OneDrive folder in their Home folder. If a user's OneDrive folder is in a different location (like Desktop) then the user's new Finder windows will open to their Home folder instead.
What was tricky about this is that there are the two keys: NewWindowTarget and NewWindowTargetPath. NewWindowTarget has custom values for pre-defined folders. So Home is PfHm, Desktop is PfDe, etc. But if you're wanting to have new Finder windows open to a custom location, you need to have the top line that directs NewWindowTarget to PfLo and then your second line points NewWindowTargetPath to the specific directory that you want your new Finder windows to open to. So while I've focused on pointing this to the user's OneDrive folder, this could potentially be used to have any folder be the target of new Finder windows.
I hope this is helpful. If you have any question or any tips on improving the script further, post away!