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

From your noodle to other noodles. Talk about ways to get the most from Hazel. Even exchange recipes for the cool rules you've thought up. DO NOT POST YOUR QUESTIONS HERE.

Moderators: Mr_Noodle, Moderators

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:

  • 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!
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

I would like to take the year or any word in the file name and create a folder with that value

Example 2014-01 - yak bill.pdf

I want to extract 2014 and put the file in a folder with that name and if it does not exist create it

I tried the video on this but instead of using just the year it added the current month and day

Thanks

Keith
kshivner
 
Posts: 2
Joined: Sun Dec 15, 2013 2:18 am

Please post a new topic in the Support forum as you don't need a script to do this so it isn't relevant to this thread.
Mr_Noodle
Site Admin
 
Posts: 11195
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

I figured it out and got it working

Thanks
kshivner
 
Posts: 2
Joined: Sun Dec 15, 2013 2:18 am

How did you do this? I would like to use the same rule to sort photos by the date they are created. If a folder does not exist for the date, I would like the rule to create a folder and then move the photo to the new folder.
pbain
 
Posts: 5
Joined: Thu Mar 31, 2011 1:04 pm

Again, please post a new topic in the support forum with the specifics of your case.
Mr_Noodle
Site Admin
 
Posts: 11195
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Sorry if I ask for basic questions, but I am newby with Hazel.
I want to rename a bill that in the file name has the date in the format 'Dec_2014' and I want to cenvert to the format '2014.12'
How to do that? It is possible?
Many thanks in advance!
rbianchi
 
Posts: 10
Joined: Fri Jan 23, 2015 6:40 pm

Please post in the support forum as a separate question. This does not below in this topic or in this forum.
Mr_Noodle
Site Admin
 
Posts: 11195
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Tips & Tricks - DO NOT POST QUESTIONS

cron