File creation date in a macro?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

File creation date in a macro?

Bill Mohler
How can I read the creation date of a file from within a Macro?
I see a command to get the modification, but would much prefer to get
the creation date/time.
Thanks, Bill
Reply | Threaded
Open this post in threaded view
|

Re: File creation date in a macro?

dscho
Hi,

On Wed, 5 Nov 2008, Bill Mohler wrote:

> How can I read the creation date of a file from within a Macro? I see a
> command to get the modification, but would much prefer to get the
> creation date/time.

Unfortunately, this is not possible with the macro language so far.  You
might be able to use a command similar to

        ctime = eval('script',
                'new java.io.File("my-file.txt").getCreationDate();');

with the JavaScript plugin.  Unfortunately, I was not able to test this,
so you will most likely have to adjust this slightly.

Alternatively, you can use any other script plugin, such as Javascript,
Jython, JRuby or Clojure.

Hth,
Dscho
Reply | Threaded
Open this post in threaded view
|

Re: File creation date in a macro?

dscho
In reply to this post by Bill Mohler
Sorry, forgot to forward to the list...

Okay, I misremembered.  Java does not have the concept of a creation date.  
You'd have to call a program (on Linux, it would be "stat -c %z <file>"),
and capture the output.  Unfortunately, there does not seem to be a simple
way to do this from ImageJ's macro language or JavaScript (yet).

Sorry,
Dscho
Reply | Threaded
Open this post in threaded view
|

Re: File creation date in a macro?

ctrueden
Hi Bill & Johannes,

You can, however, get the last modified date by calling File.lastModified(),
which might be good enough.

There is another option, depending on why you need such a date.

If what you want to know is the creation date of your dataset, you can use
Bio-Formats to query the creation date independent of file format. If there
is a creation date present in the metadata, Bio-Formats will use that --
otherwise it uses the file's last modified datestamp by default.

Below is a plugin that queries this value. You can't easily query
standardized metadata fields in a macro yet, but I could add this
functionality if there is interest.

-Curtis

-----------
//
// Creation_Date.java
//

import ij.IJ;
import ij.io.OpenDialog;
import ij.plugin.PlugIn;
import java.io.IOException;
import loci.formats.FormatException;
import loci.formats.IFormatReader;
import loci.formats.ImageReader;
import loci.formats.MetadataTools;
import loci.formats.meta.IMetadata;

public class Creation_Date implements PlugIn {

  public void run(String arg) {
    OpenDialog od = new OpenDialog("Open", null);
    String dir = od.getDirectory();
    String name = od.getFileName();
    if (dir == null || name == null) return;
    String id = dir + name;
    IFormatReader r = new ImageReader();
    IMetadata omexmlMeta = MetadataTools.createOMEXMLMetadata();
    r.setMetadataStore(omexmlMeta);
    try {
      r.setId(id);
    }
    catch (FormatException exc) {
      IJ.error(exc.getMessage());
    }
    catch (IOException exc) {
      IJ.error(exc.getMessage());
    }
    String creationDate = omexmlMeta.getImageCreationDate(0);
    IJ.showMessage("File is: " + id + "\nCreation date is: " +
creationDate);
  }

}

On Fri, Nov 7, 2008 at 9:01 AM, Johannes Schindelin <
[hidden email]> wrote:

> Sorry, forgot to forward to the list...
>
> Okay, I misremembered.  Java does not have the concept of a creation date.
> You'd have to call a program (on Linux, it would be "stat -c %z <file>"),
> and capture the output.  Unfortunately, there does not seem to be a simple
> way to do this from ImageJ's macro language or JavaScript (yet).
>
> Sorry,
> Dscho
>
Reply | Threaded
Open this post in threaded view
|

Re: File creation date in a macro?

ctrueden
Hi Bill,

Why  in  the  world  can't  Java get creation dates? Is  there  some
> philosophy behind the decision? Or is there a technological obstacle? I
>  just  find  it  very  curious, since  the  creation  date  is much less
>  labile  than  the mod  date,  which  can be  easily  changed by random
>  actions  of  the OS.
>

It's a technological obstacle caused by historical design decisions,
particularly in *nix. Nonetheless, I agree with you that Java *should* have
creation dates, but unfortunately it does not.

The thing is, creation date is not a universal concept among OSes -- both
Windows and Mac OS X support the idea, but Unix does not. Even on Mac OS X,
creation date is a second-class citizen -- since OS X is based on BSD and
BSD does not support it, Apple stores the information in the file's resource
fork, making it more difficult to access.

Given these restrictions, I have written a plugin for determining the
creation date of a file. It is HIGHLY platform dependent. On Windows, it
should work OK on regular files, but not directories (this problem could be
fixed, but I was too lazy). On Mac OS X, you must have the developer tools
installed, or the /Developer/Tools/GetFileInfo command it uses will fail.

If anyone knows another way to get the creation date of a file on Mac OS X
using a system call, I would be interested. However, there is no way to get
creation date with the stat utility, because creation date is not a piece of
*nix file metadata -- you can get ctime, which is the birth of the inode,
but that gets modified every time you modify the file or change the file
permissions, so it's useless.

HTH,
-Curtis

//
// Creation_Date.java
//

import ij.IJ;
import ij.io.OpenDialog;
import ij.plugin.PlugIn;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Creation_Date implements PlugIn {

  public void run(String arg) {
    OpenDialog od = new OpenDialog("Get Creation Date", null);
    File file = new File(od.getDirectory(), od.getFileName());
    Date created = getCreationDate(file);
    Date modified = getModifiedDate(file);
    IJ.showMessage("File = " + file + "\nCreated = " + created + "\nModified
= " + modified);
  }

  public Date getCreationDate(File file) {
    String id = file.getAbsolutePath();
    Date date = null;
    if (IJ.isMacOSX()) {
      String[] cmd = {"/Developer/Tools/GetFileInfo", "-d", id};
      try {
        Process p = Runtime.getRuntime().exec(cmd);
        BufferedReader in = new BufferedReader(
          new InputStreamReader(p.getInputStream()));
        String result = in.readLine();
        SimpleDateFormat parser = new SimpleDateFormat("MM/dd/yyyy
HH:mm:ss");
        date = parser.parse(result, new ParsePosition(0));
      }
      catch (IOException exc) { }
    }
    else if (IJ.isWindows()) {
      String[] cmd = {"cmd", "/c", "dir", "/t:C", id};
      try {
        Process p = Runtime.getRuntime().exec(cmd);
        BufferedReader in = new BufferedReader(
          new InputStreamReader(p.getInputStream()));
        for (int i=0; i<5; i++) in.readLine();
        String result = in.readLine();
        if (result != null) {
          SimpleDateFormat parser = new SimpleDateFormat("MM/dd/yyyy  hh:mm
aa");
          date = parser.parse(result, new ParsePosition(0));
        }
      }
      catch (IOException exc) { }
    }
    return date;
  }

  public Date getModifiedDate(File file) {
    return new Date(file.lastModified());
  }

}

On Thu, Nov 13, 2008 at 7:26 PM, Bill Mohler <[hidden email]>wrote:

> Why  in  the  world  can't  Java get creation dates? Is  there  some
> philosophy behind the decision? Or is there a technological obstacle? I
>  just  find  it  very  curious, since  the  creation  date  is much less
>  labile  than  the mod  date,  which  can be  easily  changed by random
>  actions  of  the OS.
>
> Thanks,
> Bill
>
>
>
>  Hi Bill & Johannes,
>>
>> You can, however, get the last modified date by calling
>> File.lastModified(),
>> which might be good enough.
>>
>> There is another option, depending on why you need such a date.
>>
>> If what you want to know is the creation date of your dataset, you can use
>> Bio-Formats to query the creation date independent of file format. If
>> there
>> is a creation date present in the metadata, Bio-Formats will use that --
>> otherwise it uses the file's last modified datestamp by default.
>>
>> Below is a plugin that queries this value. You can't easily query
>> standardized metadata fields in a macro yet, but I could add this
>> functionality if there is interest.
>>
>> -Curtis
>>
>> -----------
>> //
>> // Creation_Date.java
>> //
>>
>> import ij.IJ;
>> import ij.io.OpenDialog;
>> import ij.plugin.PlugIn;
>> import java.io.IOException;
>> import loci.formats.FormatException;
>> import loci.formats.IFormatReader;
>> import loci.formats.ImageReader;
>> import loci.formats.MetadataTools;
>> import loci.formats.meta.IMetadata;
>>
>> public class Creation_Date implements PlugIn {
>>
>>  public void run(String arg) {
>>    OpenDialog od = new OpenDialog("Open", null);
>>    String dir = od.getDirectory();
>>    String name = od.getFileName();
>>    if (dir == null || name == null) return;
>>    String id = dir + name;
>>    IFormatReader r = new ImageReader();
>>    IMetadata omexmlMeta = MetadataTools.createOMEXMLMetadata();
>>    r.setMetadataStore(omexmlMeta);
>>    try {
>>      r.setId(id);
>>    }
>>    catch (FormatException exc) {
>>      IJ.error(exc.getMessage());
>>    }
>>    catch (IOException exc) {
>>      IJ.error(exc.getMessage());
>>    }
>>    String creationDate = omexmlMeta.getImageCreationDate(0);
>>    IJ.showMessage("File is: " + id + "\nCreation date is: " +
>> creationDate);
>>  }
>>
>> }
>>
>> On Fri, Nov 7, 2008 at 9:01 AM, Johannes Schindelin <
>> [hidden email]> wrote:
>>
>>   Sorry, forgot to forward to the list...
>>>
>>>  Okay, I misremembered.  Java does not have the concept of a creation
>>> date.
>>>  You'd have to call a program (on Linux, it would be "stat -c %z
>>> <file>"),
>>>  and capture the output.  Unfortunately, there does not seem to be a
>>> simple
>>>  way to do this from ImageJ's macro language or JavaScript (yet).
>>>
>>>  Sorry,
>>>  Dscho
>>>
>>>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: File creation date in a macro?

vischer
Hi Curtis

here is a macro that extracts the creation date via AppleScript.
It has been partly created with my AppleScript Wrapper
(see http://simon.bio.uva.nl/AppleScript/AppleScriptWrapper.html )
It only works on the startup volume - perhaps some AppleScript expert  
can tell
me how to remove this limitation.



macro "myScript"{
        pth = File.openDialog("Select a File");
        idx = indexOf(pth, "/") ;
        if (idx >= 0)
                pth= substring(pth, idx+1, lengthOf(pth));//remove  first element
        pth = replace(pth, "/", ":");
        pth = '"' +pth + '"' + " of startup disk ";
        var1 = pth;
       
        cr = fromCharCode(13);
        String.resetBuffer;
        String.append('tell application "Finder"' +cr);
        String.append('set var1 to file' + var1  +cr);
        String.append('get the creation date of var1' +cr);
        String.append('end tell');
        result = exec("osascript", "-e", String.buffer);
        print(result);
}

result:
Monday, 27. October 2008 23:20:45

Norbert
Reply | Threaded
Open this post in threaded view
|

Re: File creation date in a macro?

dscho
Hi,

On Sat, 15 Nov 2008, Norbert Vischer wrote:

> here is a macro that extracts the creation date via AppleScript. It has
> been partly created with my AppleScript Wrapper (see
> http://simon.bio.uva.nl/AppleScript/AppleScriptWrapper.html ) It only
> works on the startup volume - perhaps some AppleScript expert can tell
> me how to remove this limitation.

I am by no means an expert in AppleScript, but Wayne was so kind to remind
me that the macro function "exec()" _can_ execute programs.

So this should work on Mac:

        exec("stat -f %c " + filename);

and this on Linux:

        exec("stat -c %Z " + filename);

Note that this will not handle filenames with spaces correctly, you will
have to write something as ugly as this if you have such filenames:

        exec("stat -c %Z '" + filename + "'");

Ciao,
Dscho

P.S.: I am pretty certain that there is also some program on Windows that
can tell you the creation date, but Windows has a track record of changing
the set of available commands from time to time, so that may break.
Reply | Threaded
Open this post in threaded view
|

Re: File creation date in a macro?

Wayne Rasband
> I am by no means an expert in AppleScript, but Wayne was so kind to  
> remind
> me that the macro function "exec()" _can_ execute programs.
>
> So this should work on Mac:
>
> exec("stat -f %c " + filename);
>
> and this on Linux:
>
> exec("stat -c %Z " + filename);
>
> Note that this will not handle filenames with spaces correctly, you  
> will
> have to write something as ugly as this if you have such filenames:
>
> exec("stat -c %Z '" + filename + "'");

File paths with spaces can be handled by using the multiple argument  
version of exec():

    exec("stat", "-f%c", filepath);

In any case, "stat -f%c" on Mac OS X returns a cryptic 10 digit  
number (e.g., "1223733141") that I'm not sure how to interpret. Running

    exec("stat", "-l", filepath);

returns a line similar to what you would get using the "ls -l"  
command, for example:

   -rw-r--r-- 1 wayne staff 76250 Oct 11 09:52:21 2008 /Users/wayne/
IMG_0199.jpg

This is easier to interpret but the date is the date last modified,  
not the creation date.

-wayne



> Ciao,
> Dscho
>
> P.S.: I am pretty certain that there is also some program on  
> Windows that
> can tell you the creation date, but Windows has a track record of  
> changing
> the set of available commands from time to time, so that may break.
Reply | Threaded
Open this post in threaded view
|

Re: File creation date in a macro?

dscho
Hi,

On Sat, 15 Nov 2008, Rasband Wayne wrote:

> In any case, "stat -f%c" on Mac OS X returns a cryptic 10 digit number (e.g.,
> "1223733141") that I'm not sure how to interpret.

This is the number of seconds since January 1st, 1970 (AKA "UNIX epoch").  
The original poster asked for a way to sort files by their creation date,
and IMHO the UNIX epoch makes that the easiest.

But of course it may be easier to execute "ls -c" then, and split the
result by newlines.  (Although this will not work on Windows, and maybe
not even on MacOSX.)

Ciao,
Dscho
Reply | Threaded
Open this post in threaded view
|

Re: File creation date in a macro?

Michael Schmid
Hi Dscho,

the problem on Mac OSX: both "ls -lc" and "stat -f %c" don't give the  
real creation time. Editing the file (e.g. with TextEdit.app),  
appending to it and even the unix "touch" command change the date  
reported in this way.

So at least for Mac OSX 10.4, I fear that you need the macro by Curtis.

Michael
________________________________________________________________

On 15 Nov 2008, at 20:49, Johannes Schindelin wrote:

> Hi,
>
> On Sat, 15 Nov 2008, Rasband Wayne wrote:
>
>> In any case, "stat -f%c" on Mac OS X returns a cryptic 10 digit  
>> number (e.g.,
>> "1223733141") that I'm not sure how to interpret.
>
> This is the number of seconds since January 1st, 1970 (AKA "UNIX  
> epoch").
> The original poster asked for a way to sort files by their creation  
> date,
> and IMHO the UNIX epoch makes that the easiest.
>
> But of course it may be easier to execute "ls -c" then, and split the
> result by newlines.  (Although this will not work on Windows, and  
> maybe
> not even on MacOSX.)
>
> Ciao,
> Dscho