Change creation date attribute?

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Change creation date attribute? Wed Jul 31, 2013 10:21 am • by hansvanderdrift
Hi

I saw a post from 08:

"People commonly use the "touch" command but it only modifies the modification and/or access times. If you have the dev tools installed, there's the SetFile program which can be used to change the creation date. If you don't have the dev tools, drop me an email and I can send you a copy of the program. You should be able to invoke it from a shell script to do what you want."

I am trying to do the following:

1. Scan documents
2. OCR the Doc
3. Use Hazel to pattern match for dates etc.
4. Use the dates as part of the title
5. Change the creation date to match the pattern match date.

I have been able to complete 1-4, but 5 is difficult.

I want to do this for Evernote structure.

Any suggestions

Hans
hansvanderdrift
 
Posts: 13
Joined: Wed Jul 31, 2013 6:55 am

Re: Change creation date attribute? Wed Jul 31, 2013 2:32 pm • by sjk
I'm curious why you want to use creation instead of modification dates, at least if you don't intend to change the final documents.
sjk
 
Posts: 332
Joined: Thu Aug 02, 2007 5:43 pm
Location: Eugene

Re: Change creation date attribute? Wed Jul 31, 2013 3:43 pm • by Mr_Noodle
There's no real way to transmit custom tokens to a script at the moment. I am considering it for AppleScript. Shellscripts are a bit more difficult since there's no easy way to send a table of values. It would require some sort of parsing on the script end which can be a bit tedious. Of course, if someone knows of a standard format and parsing libraries for multiple scripting languages, I'm willing to consider it.
Mr_Noodle
Site Admin
 
Posts: 11866
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Change creation date attribute? Thu Aug 01, 2013 12:54 am • by hansvanderdrift
Mr Noodle,

Thanks for your reply.

Does this post: viewtopic.php?f=3&t=1770&p=7294

shed any light on my question.

I am not the most technical, but from the sounds of it he is using token in an apple script.

Hans
hansvanderdrift
 
Posts: 13
Joined: Wed Jul 31, 2013 6:55 am

Re: Change creation date attribute? Thu Aug 01, 2013 10:12 am • by a_freyer
sjk wrote:I'm curious why you want to use creation instead of modification dates, at least if you don't intend to change the final documents.


OP indicated that he's scanning documents, so the file creation date is the day of the scan. OP wants the file creation date to be a date that more accurately represents the creation date of the document, not the date that the document was scanned.


Now, to answer the question - yes, all of this can be simply accomplished within a shell script.

After OCRing:
Code: Select all
if (all) of the following conditions are met for (the file or folder being processed):
     kind is pdf
     contents matches (Date to Match)

Do the following to the matched file or folder:
     rename with pattern (Date to Match, formatted to YYYY-MM-DD-hh-mm-ss)-(extension)
     run shell script (embedded script)
     rename with pattern (Date to Match, however you'd like to format it) (name) (extension)


Now, the embedded script will be this:

First, lets separate the filename out, then we set the creation date of the file, using the 'touch' command:
Code: Select all
filename_without_path=$(basename "$1")
extension="${filename_without_path##*.}"
filename_without_extension_or_path="${filename_without_path%.*}"

YYYY=$(echo "$filename_without_extension_or_path" | awk -F "-" '{print $1}')
MM=$(echo "$filename_without_extension_or_path" | awk -F "-" '{print $2}')
DD=$(echo "$filename_without_extension_or_path" | awk -F "-" '{print $3}')
hh=$(echo "$filename_without_extension_or_path" | awk -F "-" '{print $4}')
mm=$(echo "$filename_without_extension_or_path" | awk -F "-" '{print $5}')
ss=$(echo "$filename_without_extension_or_path" | awk -F "-" '{print $6}')

touch -t "$YYYY$MM$DD$hh$mm$ss" "$1"


Basically what we're doing here is temporarily naming the file to the date that you extract, running a script on that file, then renaming the file however you like. It's a bit of trickery to get a single token passed to a shell script in order to set the creation date of the file.

I do not recall if the 'touch' command is included in the default set or the dev set as of 10.8. Many of the more useful dev tools were included in the main set as of 10.7. You can check if you have the tool by opening /Applications/Utilities/Terminal.app and typing 'which touch' without the ticks. If a path is returned, then you have 'touch' installed. If you do not see a path, then post back and we'll come up with another way to set the metadata for you.

I have not tested the above code, but it should work. Post back if you have issues.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Change creation date attribute? Thu Aug 01, 2013 7:28 pm • by hansvanderdrift
Thank you very much.

I wrote the rule, and with my limited knowledge can see what it is doing.

It looks like I have touch

Image

But the applescript seems to have an error.

When I try to run the script it says: Expected expression but found unknown token.

Image
hansvanderdrift
 
Posts: 13
Joined: Wed Jul 31, 2013 6:55 am

Re: Change creation date attribute? Thu Aug 01, 2013 7:48 pm • by a_freyer
Shell script, not AppleScript.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Change creation date attribute? Fri Aug 02, 2013 12:12 am • by hansvanderdrift
a_freyer wrote:Shell script, not AppleScript.


Sometimes the answer is staring you in the face....I will try tonight and let you know how I go.
hansvanderdrift
 
Posts: 13
Joined: Wed Jul 31, 2013 6:55 am

Re: Change creation date attribute? Fri Aug 02, 2013 3:38 am • by hansvanderdrift
I hope I am not side railing this topic too much, I first must thank a_freyer for his patience and support.

Yes is all worked.…Almost.

Hazel found the date:

Image

Renamed it with the date:

Image

and changed its creation date to the correct format:

Image


So we are 99.99% on the way there, however, when I import it into Evernote, evernote makes the create date as the date it was imported not the meta data of the file (I just leant that)

I use an apple script to import the file into evernote:

Code: Select all
tell application "Evernote"
   activate
   create note from file theFile notebook {"203 - VISA"} tags {"ANZ", "BS", "VISA"}
end tell


And I found this applescript that apparently keeps the meta data:

Code: Select all
set listOfNames to {}
set theFolder to choose folder "Select the source folder"

tell application "Finder"
    set filelist to every file of the folder theFolder
    repeat with currentFile in filelist
        set currentFileName to (the name of currentFile)
        set nom to currentFileName
        if text -4 of nom is "." then
            set currentFileName to (text 1 thru -5 of nom)
        end if
        set currentFileDate to (the creation date of currentFile)
        set modFileDate to (the modification date of currentFile)
        set currentFileLocation to currentFile as alias
        set currentFileFolder to (the name of theFolder)
        tell application "Evernote"
            set theItem to create note title currentFileName created currentFileDate notebook currentFileFolder from file currentFileLocation
            set (modification date of theItem) to modFileDate
        end tell
    end repeat
end tell


Can it be combined?

Thanks again.….

Hans
hansvanderdrift
 
Posts: 13
Joined: Wed Jul 31, 2013 6:55 am

Re: Change creation date attribute? Fri Aug 02, 2013 11:10 am • by a_freyer
The problem is that Evernote continues to change their AppleScript dictionary. Very bad if a user wants consistency.

However, I have tested this script with the most recent version of Evernote and it will work for you:

Code: Select all
tell application "Finder"
   set theFileName to (text items 1 thru ((length of (name of theFile as string)) - 4) of (name of theFile as string)) as string
   set theFileDate to (the creation date of theFile)
   set modFileDate to (the modification date of theFile)

set theNotebook to "The Notebook You'd Like the File To Be Added To"   
   tell application "Evernote"
      create note title theFileName from file theFile created theFileDate notebook theNotebook
   end tell
end tell
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Change creation date attribute? Fri Aug 02, 2013 7:09 pm • by hansvanderdrift
That worked a charm.….

Thank you very much.
hansvanderdrift
 
Posts: 13
Joined: Wed Jul 31, 2013 6:55 am

Re: Change creation date attribute? Sun Nov 24, 2013 7:23 pm • by Plex
I've been trying to figure out how to use the touch command to change the creation date of a file (see here:http://www.noodlesoft.com/forums/viewtopic.php?f=4&t=2960) and I stumbled upon this thread and I got to thinking:

Instead of asking Hazel to look inside the contents of a file for a date pattern, couldn't I ask Hazel to look in the file name for a date pattern and use that with the Hazel and shell script attributes in the posts above? Since Hazel can change the name to the creation date, this may be the perfect solution for me to "carry forward" the creation date through a video conversion and then "put it back" when I'm finished.

So here's my starting file: 2013.11.17_17.22.36.m4v
Image
Hazel renamed the file from a generic.MTS file that was created on Nov. 17, 2013 at 5:22.36pm

And then I try the solutions posted in this thread.

Here are my Hazel attributes:
Image

Here are the details of those attributes:

Code: Select all
if (all)
kind is movie
name matches date pattern (in format YYYY.MM.DD_hh.mm.ss)


I then set the following to happen once the file is matched:

First:
Code: Select all
Rename with pattern Date Pattern, in format YYYY-MM-DD-hh-mm-ss)-(extension)

I'm not clear on the "-" between the (Date Pattern) and (extension), so I've put a "dash" as you see it above in between those two items in (); not sure if that was supposed to be a space, or whether the "dash" should follow "ss" in the Date Pattern.

Second:
Code: Select all
Run shell script embedded script

Code: Select all
filename_without_path=$(basename "$1")
extension="${filename_without_path##*.}"
filename_without_extension_or_path="${filename_without_path%.*}"

YYYY=$(echo "$filename_without_extension_or_path" | awk -F "-" '{print $1}')
MM=$(echo "$filename_without_extension_or_path" | awk -F "-" '{print $2}')
DD=$(echo "$filename_without_extension_or_path" | awk -F "-" '{print $3}')
hh=$(echo "$filename_without_extension_or_path" | awk -F "-" '{print $4}')
mm=$(echo "$filename_without_extension_or_path" | awk -F "-" '{print $5}')
ss=$(echo "$filename_without_extension_or_path" | awk -F "-" '{print $6}')

touch -t "$YYYY$MM$DD$hh$mm$ss" "$1")


Third:
Code: Select all
Rename with pattern Date Pattern, in format YYYY.MM.DD_hh.mm.ss) (name) (extension)


Hazel finds 2013.11.17_17.22.36.m4v and renames it to 2013-11-17-17-22-36-.m4v yet the script fails with the following error:

Code: Select all
2013-11-24 16:17:32.627 hazelworker[50159] DEBUG: Unexpected type for Mail download URL: (null)
2013-11-24 16:17:32.668 hazelworker[50159] DEBUG: Could not scan past browser.download.dir preference
2013-11-24 16:17:33.201 hazelworker[50159] Processing folder TempOutput (forced)
2013-11-24 16:17:33.201 hazelworker[50159] DEBUG: Initialized
2013-11-24 16:17:33.201 hazelworker[50159] DEBUG: Pausing to wait for things to settle down.
2013-11-24 16:17:35.201 hazelworker[50159] DEBUG: Processing directories: (
    "/Volumes/HTPC/Media/Movies/AVCHD/TempOutput"
)
2013-11-24 16:17:35.224 hazelworker[50159] DEBUG: About to process directory /Volumes/HTPC/Media/Movies/AVCHD/TempOutput
2013-11-24 16:17:35.226 hazelworker[50159] DEBUG: Received file event: {
    abort = 1;
}
2013-11-24 16:17:35.228 hazelworker[50159] DEBUG: .DS_Store: File is hidden/invisible. Skipping.
2013-11-24 16:17:35.430 hazelworker[50159] 2013.11.17_17.22.36.m4v: Rule Set Creation Date to Match Name matched.
2013-11-24 16:17:35.430 hazelworker[50159] DEBUG: New rule signature. Executing actions.
Old signature: (null)
New Signature:{typeObject isType: "public.movie" AND displayBasename hazelMatchesPattern: {{hazelcustomtoken:Date Pattern:({year:sc0:cap}:.:{month:sc0:cap}:.:{dayOfMonth:sc0:cap}:_:{hour:sc0:cap}:.:{minute:sc0:cap}:.:{second:sc0:cap}):cap}}}:{(rename:(
    "{hazelcustomtoken:Date Pattern:({year:cd4}:-:{month:cd2}:-:{dayOfMonth:cd2}:-:{hour:cd2:uaf1}:-:{minute:cd2}:-:{second:cd2})}",
    "-",
    "{extension:sc0}"
),{
})(shellscript:/bin/bash:844d663f3786103870ea953f8eb3b615,{
})(rename:(
    "{hazelcustomtoken:Date Pattern:({year:cd4}:{month:cd2}:{dayOfMonth:cd2}:_:{hour:cd2:uaf1}:{minute:cd2}:{second:cd2})}",
    "{name:sc0}",
    "{extension:sc0}"
),{
})}
2013-11-24 16:17:35.790 hazelworker[50159] [File Event] File renamed: /Volumes/HTPC/Media/Movies/AVCHD/TempOutput/2013.11.17_17.22.36.m4v renamed to /Volumes/HTPC/Media/Movies/AVCHD/TempOutput/2013-11-17-17-22-36-.m4v.
2013-11-24 16:17:36.805 hazelworker[50159] DEBUG: == script output ==
touch: out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]

== End script output ==
2013-11-24 16:17:36.805 hazelworker[50159] [Error] Shell script failed: Error processing shell script on file /Volumes/HTPC/Media/Movies/AVCHD/TempOutput/2013-11-17-17-22-36-.m4v.
2013-11-24 16:17:36.806 hazelworker[50159] Shellscript exited with non-successful status code: 1
2013-11-24 16:17:36.806 hazelworker[50159] DEBUG: Tapping error retry sequence
2013-11-24 16:17:36.820 hazelworker[50159] DEBUG: Writing out DB file: /Volumes/HTPC/Media/Movies/AVCHD/TempOutput
2013-11-24 16:17:36.872 hazelworker[50159] DEBUG: Directory /Volumes/HTPC/Media/Movies/AVCHD/TempOutput processed in 1.647401 seconds
2013-11-24 16:17:36.872 hazelworker[50159] DEBUG: Sleeping
2013-11-24 16:17:38.970 hazelworker[50159] DEBUG: About to process directory /Volumes/HTPC/Media/Movies/AVCHD/TempOutput
2013-11-24 16:17:38.971 hazelworker[50159] Received abort event.
2013-11-24 16:17:38.972 hazelworker[50159] DEBUG: Sending metrics to scheduler. Next scheduled run:
2013-11-24 16:17:38.973 hazelworker[50159] Done processing folder TempOutput
 


Any ideas and/or suggestions? I'm so close I can feel it! :D

Many thanks!!
Last edited by Plex on Sat Mar 18, 2017 2:46 pm, edited 1 time in total.
Plex
 
Posts: 14
Joined: Mon Nov 18, 2013 8:02 pm
Location: Colorado

Re: Change creation date attribute? Mon Nov 25, 2013 2:16 pm • by Mr_Noodle
It's a problem with your use of the "touch" program. I suggest debugging it outside of Hazel. Print out the argument you are passing in to "touch" would be a good place to start.
Mr_Noodle
Site Admin
 
Posts: 11866
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Change creation date attribute? Mon Nov 25, 2013 2:35 pm • by a_freyer
I'm not clear on the "-" between the (Date Pattern) and (extension), so I've put a "dash" as you see it above in between those two items in (); not sure if that was supposed to be a space, or whether the "dash" should follow "ss" in the Date Pattern.


The dash is so that when we're extracting the token in shell, we have properly formatted seconds. If you don't include that dash, then the 'touch' command will fail.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Change creation date attribute? Mon Nov 25, 2013 2:47 pm • by Plex
Thanks, I'll make sure the dash is included (it was in the example above where I was receiving the error). Does it matter where the dash is located, whether it's part of the date pattern or specifically between the date pattern and the extension (with no spaces between the three variable)? Right now I've got it as "(date pattern)-(extension)".
Plex
 
Posts: 14
Joined: Mon Nov 18, 2013 8:02 pm
Location: Colorado

Next

Return to Support