Use Any Date/Time as Custom Token for Renaming/Sorting Files

I originally posted this as a reply to a support request, but I think there are other folks who would find it handy.
This allows you to use any date or time as a custom token for file/folder naming ... regardless of a file's metadata.
This is exceptionally helpful if you want to:
Perhaps you want to rename/sort a file with these tokens:
Step 1: add your rule with an embedded AppleScript action:
Step 2: copy the AppleScript into Hazel:
The section date +%Y-%m is where the date format magic happens. Right now, this example returns 2012-06 for the year and month.
Here are some other examples you might want to use:
# Year and Month (e.g. 2012-07)
date +%Y-%m
# Year and 1 Month Later (notice the adjust command -v precedes the +1m which is one month forward)
date -v +1m +%Y-%m
# Year and 1 Month Back (notice the adjust command -v precedes the -1m which is one month back)
date -v -1m +%Y-%m
#Current TIme (e.g. 12:01:00)
date +%H:%M:%S
#One Hour From Now
date -v +1H +%H:%M:%S
#Current Day of the Week (e.g. Monday)
date +%A
#Current Day of the Week Abbreviation (e.g. Mon)
date +%a
#Current Month (e.g. July)
date +%B
#Subtract 1 Month ONLY if the Current Date is within the First 7 Days of the Month (e.g. 2012-06 is output until 07/07/2012, and then 2012-07 is output.
date -v -$(test $(date +%d) -lt 7 && echo 1 || echo 0)m +%Y-%m
And so on. You can see all of the date format options you have here.
Step 3: Make sure to add your custom token name to the token list!
Before you click out of the embedded script window, click the (i) in the upper right. Now, click the [+] in the lower left and type the exact name that appears in your custom script. In my example, the name is CustomDateTokenNameGoesHere but you can name it whatever you want.
Step 4: Add More Time Tokens if you want
By adding additional comma-delimited tokens to the applescript, and adding the names in the token naming window (step 3) you can create any number of time tokens:
Hope someone finds this useful!
This allows you to use any date or time as a custom token for file/folder naming ... regardless of a file's metadata.
This is exceptionally helpful if you want to:
- Rename files in your downloads folder with the date that they were downloaded.
- Rename class notes / meeting minutes with the current date or meeting time
- Sort files into folders based on when they were scanned
- ... whatever else you'd like ...
Perhaps you want to rename/sort a file with these tokens:
- Today's Date
- Time Right Now
- Last Month's Date
- Ten Minutes Ago
- Name of the Current Day of the Week
- Name of the Current Month
- ... whatever else you want relating to date or time ...
Step 1: add your rule with an embedded AppleScript action:
- Code: Select all
if (all) of the following conditions are met for the (the file or folder being matched)
<<Whatever you want>>
Do the following to the matched file or folder:
Run Applescript (embedded script)
Step 2: copy the AppleScript into Hazel:
- Code: Select all
return {hazelExportTokens:{CustomDateTokenNameGoesHere:do shell script "date +%Y-%m"}}
The section date +%Y-%m is where the date format magic happens. Right now, this example returns 2012-06 for the year and month.
Here are some other examples you might want to use:
# Year and Month (e.g. 2012-07)
date +%Y-%m
# Year and 1 Month Later (notice the adjust command -v precedes the +1m which is one month forward)
date -v +1m +%Y-%m
# Year and 1 Month Back (notice the adjust command -v precedes the -1m which is one month back)
date -v -1m +%Y-%m
#Current TIme (e.g. 12:01:00)
date +%H:%M:%S
#One Hour From Now
date -v +1H +%H:%M:%S
#Current Day of the Week (e.g. Monday)
date +%A
#Current Day of the Week Abbreviation (e.g. Mon)
date +%a
#Current Month (e.g. July)
date +%B
#Subtract 1 Month ONLY if the Current Date is within the First 7 Days of the Month (e.g. 2012-06 is output until 07/07/2012, and then 2012-07 is output.
date -v -$(test $(date +%d) -lt 7 && echo 1 || echo 0)m +%Y-%m
And so on. You can see all of the date format options you have here.
Step 3: Make sure to add your custom token name to the token list!
Before you click out of the embedded script window, click the (i) in the upper right. Now, click the [+] in the lower left and type the exact name that appears in your custom script. In my example, the name is CustomDateTokenNameGoesHere but you can name it whatever you want.
Step 4: Add More Time Tokens if you want
By adding additional comma-delimited tokens to the applescript, and adding the names in the token naming window (step 3) you can create any number of time tokens:
- Code: Select all
return {hazelExportTokens:{CustomDateTokenNameGoesHere:do shell script "date +%Y-%m",AnotherCustomDateTokenNameGoesHere:do shell script "date -v -1Y +%Y-%m"}}
Hope someone finds this useful!