Checking folder/file creation time using bash on Mac OS X

Why would you want to check the folder/file creation time in Mac OS X?

Outset allows you to run login scripts at every login or only once per user. The way Outset keeps track of whether a script has been run for a user or not is in a .plist at /Users/username//Library/Preferences/com.github.outset.once.plist with the name of the script and the corresponding date and time. Outset also has an option to add certain users to the ignore list, but that means they’re ignored for all Outset scripts.

But what if you want to run a certain login-once script only for new users and not existing users?

For example, let’s say you want to deploy a default Dock to clients using Outset. The old (now frowned-upon but still works) way to do it would be to modify the /System/Library/User Template/English.lproj folder. Now the new (recommended) way is to script all changes.

You don’t want to make it a login-once script, unless you’re replacing an already-existing login-once script with the exact same name, because then it will modify the Dock for users who have already customized their own Docks.

So one way to approach this problem is to check the creation time of the user folder. A user folder created in the last two minutes is likely to be new and okay to be scripted to the new default, but a user folder created over two minutes ago may have already been customized by the user. Dock. Actually, the user folder gets created when the user folder is created, not when the user logs in. So it’s actually better to check for when the com.apple.dock.plist was created, even though that creation date shifts to about a day ago, even if you don’t modify the Dock at all—it’s still a more accurate indicator than the user folder creation.

Script to check for user folder com.apple.dock.plist creation date/time

I wrote up a CheckUserFolderCreationTime script that you can pop into various login-once scripts that you want to have applied to only newly-created users.

The logic of it is basically:

  1. Define a threshold of creation time.
  2. Find the current user’s home directory Dock .plist.
  3. Find the date and time it was created.
  4. See if it was created within the past [threshold] minutes.


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *