Get file creation date in Applescript

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

Moderator: Mr_Noodle

Get file creation date in Applescript Mon Dec 31, 2012 4:23 pm • by rbregt
I am trying to use Hazel to create notes in Evernote. With a simple script I can add files to Evernote like this:

Code: Select all
tell application "Evernote"
  create note from file theFile
end tell


What is would like to do is to set the note's created date to the created date of the file. If I try this, I get an error message "Can't get alias". Can anyone with some Applescript experience tell me what's wrong?

This is the script that does not work:

Code: Select all
tell application "Evernote"
  set theFileDate to (the creation date of theFile)
  create note from file theFile created theFileDate
end tell
rbregt
 
Posts: 2
Joined: Mon Dec 31, 2012 4:10 pm

Re: Get file creation date in Applescript Mon Dec 31, 2012 4:50 pm • by a_freyer
Although I do not have Evernote installed to test this, I believe that the following will solve your issue. I note that the Evernote applescript reference page takes posix paths (i.e. slashed paths in lieu of unix paths), so:

Code: Select all
tell application "Evernote"
  set thePOSIXFilePath to (the POSIX path of theFile)
  set theFileDate to (the creation date of theFile as string)
  create note from file thePOSIXFilePath title "Created  " & theFileDate
end tell


Also, I am presuming that you want your note to look like this:

Created: Mon Dec 31 13:55:50 MST 2012


... since "created" doesn't appear to be part of the Evernote applescript dictionary. We actually have to tell Evernote that "created" is a string to be concatenated with the date string. That's what the ampersand and quotations do.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Get file creation date in Applescript Tue Jan 01, 2013 4:16 pm • by rbregt
Thank you for your reply.

The error actually does not have to do with Evernote. The create note statement in the first sample script works just fine. The error is in the line that tries to get the date from the file. You can test this with this Applescript in a rule:

Code: Select all
tell application "Finder" -- you need this to have a UI for the display dialog
   try
      set theFileDate to (the creation date of theFile)
   on error errMsg
      display dialog errMsg
   end try
end tell


BTW, if you open the Evernote library in Applescript Editor, you will read this:

create note
[from file file] : Clip the contents of the specified file.
[from url text] : Clip the contents of the specified URL.
[with text text] : Create a new note using the specified plain-text as content.
[with html text] : Create a new note using the specified html as content.
[title text] : Title for the new note.
[notebook text or notebook] : Notebook in which to place the note. Can be the name of a notebook or an object reference. If no notebook with the specified name exists a new one is created. If no notebook is specified, the default notebook for the account is used.
[tags list of text or list of tag] : Tags to assign to the new note. Can be the name of a tag or an object reference. If no tag with the specified name exists a new one is created.
[attachments list of file] : Files to attach to the note.
[created date] : Creation date for the new note. Defaults to now.
→ note : The new note.


I will continue my quest
rbregt
 
Posts: 2
Joined: Mon Dec 31, 2012 4:10 pm

Re: Get file creation date in Applescript Thu Aug 01, 2013 9:31 am • by eiff
I'm trying to do the exact same thing.
I would expect this to work:

Code: Select all
set theFileDate to (the creation date of theFile)

Anyone have any solutions to this?
eiff
 
Posts: 7
Joined: Mon Aug 20, 2012 8:14 pm

Re: Get file creation date in Applescript Fri Aug 02, 2013 11:12 am • by a_freyer
I downloaded Evernote to test these since so many problems continue to arise between AppleScript, Hazel, and Evernote.

You have to ask finder to set the creation date for you. For example, this script works:

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


Return to Support