From: Adam Griffith on
What about something roughly like this:

BeginPackage["MyApplication`Common"];
$ApplicationDir = DirectoryName[FindFile[$Input]];
Begin["`Private`"];
End[];
EndPackage[];

Then a resource file could be accessed as follows:

FileNameJoin[{$ApplicationDir, "Resource Files", "001.tiff"}]


-Adam

Karsten W. wrote:
> Hello,
>
> my packages uses some data files. Currently, I store them in a
> subdirectory "resources" below the package directory and access the
> data via
>
> ToFileName[{$UserBaseDirectory, "Applications", myPackage,
> "resources"}, myDataFileName];
>
> I wonder if this is a portable way. I am afraid it is not, since the
> package may be installed under $BaseDirectory as well. And then there
> is this directory at the same level as "Applications" named
> "ApplicationData"
>
> What is the recommended way to store my data files?
>
> Any hint appreciated,
>
> kind regards,
> Karsten.
>

From: Albert Retey on
Hi,

> my packages uses some data files. Currently, I store them in a
> subdirectory "resources" below the package directory and access the
> data via
>
> ToFileName[{$UserBaseDirectory, "Applications", myPackage,
> "resources"}, myDataFileName];
>
> I wonder if this is a portable way. I am afraid it is not, since the
> package may be installed under $BaseDirectory as well. And then there
> is this directory at the same level as "Applications" named
> "ApplicationData"
> What is the recommended way to store my data files?

I don't know, and I think it depends on what the format of your data
files is. If they contain data but are normal package files ending in .m
you can just use e.g. Get["myPackage`resources`file1`"]. For arbitrary
file formats that can not be read with Get you probably need some more
effort, but something like the following should do the trick, no matter
where your package is installed (as long as the package itself can be
loaded in the first place):

datadir=First[FileNames[FileNameJoin[{myPackage, "resources"}], $Path]]

Then you can import your data with e.g.:

Import[FileNameJoin[{datadir,filename}]]

While this should be pretty robust to find a "myPackage/resources"
directory (if there is _one_), of course it still can possibly find a
wrong "myPackage/resources" directory if there is another one elsewhere
in $Path (very unlikley but possible). So depending on how your package
is organized you might want to search for something that is more
specific to your package and deduce the correct directory from that
(e.g. myPackage/Kernel/init.m). Or you think of it as a special feature
that allows the user to collect his own datafiles in e.g.
$UserBaseDirectory while leaving the package and the original datafiles
in e.g. $BaseDirectory... (it would be just another call to FileNames to
search both or all myPackage/resources directories you can find in $Path...)

Note that I have switched from ToFileName to FileNameJoin, since: 'As of
Mathematica 7, ToFileName has been superseded by FileNameJoin.'
Anyway, ToFileName still works alright and I can hardly imagine they
will remove it very soon...

hth,

albert

From: Karsten W. on
Thanks a lot for this discussion. It helped me a lot.

Kind regards,
Karsten.

From: David Bailey on
Karsten W. wrote:
> Hello,
>
> my packages uses some data files. Currently, I store them in a
> subdirectory "resources" below the package directory and access the
> data via
>
> ToFileName[{$UserBaseDirectory, "Applications", myPackage,
> "resources"}, myDataFileName];
>
> I wonder if this is a portable way. I am afraid it is not, since the
> package may be installed under $BaseDirectory as well. And then there
> is this directory at the same level as "Applications" named
> "ApplicationData"
>
> What is the recommended way to store my data files?
>
> Any hint appreciated,
>
> kind regards,
> Karsten.
>
This delivers the name of the package .m file, but it is obviously
undocumented!

System`Private`$InputFileName

You need to execute this as the package is being read in, and assign the
result to a private variable.

I use it, then test the result to determine if it worked, and otherwise
do a messy search for the file in the possible directories.

Once you have the full path name of the .m file, you can easily compute
the location of your resources.

This is definitely something that is missing from Mathematica, that
would be trivial for WRI to add!

David Bailey
http://www.dbaileyconsultancy.co.uk