MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Opening a foreign file from Mathematica

  • To: mathgroup at smc.vnet.net
  • Subject: [mg78895] Re: Opening a foreign file from Mathematica
  • From: "Steve Luttrell" <steve at _removemefirst_luttrell.org.uk>
  • Date: Thu, 12 Jul 2007 05:17:51 -0400 (EDT)
  • References: <f6vnng$qj7$1@smc.vnet.net> <f72bep$ad7$1@smc.vnet.net>

If you want to know "why" I asked the question, it is because I often need 
to incorporate foreign files into my web of Mathematica notebooks, and when 
I make hyperlinks (from notebooks to foreign files) I want them to work as 
cleanly as possible. Without this I would be forced to manually jump outside 
my web of notebooks to locate the foreign files in order to open them.

-- 
Steve Luttrell
West Malvern, UK

"Peltio" <peltio at twilight.zone> wrote in message 
news:f72bep$ad7$1 at smc.vnet.net...
> As is often the case, nobody knew why. All we know so far is
>    Where: comp.soft-sys.math.mathematica
>    When: 10-07-07, 12.40.48
>    Who: Steve Luttrell
>    What: see below
>
>
>> I have always been frustrated that the full path to the file was needed 
>> in
>> this approach, because this locked down the notebook/file pair to a 
>> single
>> place in the filesystem, and this approach broke even if you subsequently
>> changed only the name of (i.e. letter assigned to) your hard drive. Many 
>> of
>> my past links have been broken in this way. This is not ideal.
>>
>> I have tried the obvious approach to using relative paths:
>>
>> SetDirectory[NotebookDirectory[]]
>>
>> Cell[BoxData[
>>     ButtonBox["test",
>>       ButtonData:>{
>>         URL[ ".\\temp\\foo.doc"], None},
>>       ButtonStyle->"Hyperlink"]], "Input"]
>>
>> which I hoped would work from a notebook stored at the top level of C: 
>> (in
>> this example), but unfortunately it doesn't work.
>>
>> So, the question is how to generalise this so that you can use paths
>> relative to the notebook.
>
> I have not access to Mathematica right now, so I cannot give you a clean 
> and
> isolated code snippet. But I once wrote a package that allowed me to
> import pictures stored in a directory 'nearby' the actual notebook.
> That is, the pictures could be stored in the same folder as the
> notebook, in a folder named "Pics" in the same directory of the
> notebook or in subfolder whose path was relative to that of the
> notebook.
>
> It was nice, because in this way the notebooks behaved like an html
> files saved with a web browser (a filename.htm file with all the
> pictures in folder filename_files).
>
> Embedded in this short package, I guess there is what you need.
> I paste the whole package, to be sure. It worked under 4.x, and someone
> used it on 5.x (not complaing with me after that :-) ). Have no idea if
> it works on v6.
>
> =================
>
>
>
> (* *)
> (* NBImport.m (Import TOOLS) v1.2 English *)
> (* for Mathematica 4.x  (and 5.x)     *)
> (* *)
> (****************************************************************)
> (*
> -:Package: NBImport (Notebook Importing tools)
> - a simple package that allows the user to import
> - files and pictures contained in a folder inside
> - the notebook's directory. Options specify the name
> - of the folder and the import and show procedures.
> -
> -
> -:Author: Peltio
> -:Date: March 2003
> -:Version: 1.2 for Mathematica 4.x (and 5.x)
> -:Disclaimer: This package may be freely used for non-commercial
> - purposes. Please retain the comments above.
> - This package is free and must remain free.
> -
> -------------------------------------------------------------------
> -:Version History:
> -
> - version 1.0 -Mar 2003
> - Main dump
> - version 1.0.1
> -   Added a loader for the PNG import facility by JensPeer Kuska
> - version 1.0.2
> -   Changed DirName in RelativePath
> - version 1.1
> -   Added Fig
> -   Has an option that specifies the priority
> -   in filetype
> - version 1.1.1
> -   Fig is no longer a protected symbol
> -   this is so that the user can define his own figures.
> - version 1.2
> -   Corrected code for fig, added (almost redundant)
> -   SubDirName option
> -
> -
> -:Notes for future development:
> -
> - check if the specified directory exists before
> - launching ResetDirectory[]
> - A syntactic check can find if the extension has been
> - specified. If not, add the one that is in the default
> - option.
> -
> -------------------------------------------------------------------
> -:Limitations:
> -
> - It is not compatible with versions 2.x 3.x
> - No effort has been made to make this package robust.
> - No error message was implemented.
> -
> -------------------------------------------------------------------
> -
> *)
>
>
> (* The following line loads Jens Peer Kuska's PNGImport routine
> If you followed Jens instruction you should have placed
> his package in the Autoload folder and this line is not
> really needed. If you leave it there, and you don't
> have his package in a folder named MathPNG in a path directly
> accessible by Mathematica you will get an error, though.
>
> Jens package can be downloaded from his website, along with
> his excellent rendering package MathGL3D. Do check it out.
> If you don't want to install PNGImportExport, then just
> comment out the line below.
> *)
>
>
> If[MemberQ[$ImportFormats, "PNG"]==False,
> Get["MathPNG`PNGImportExport`"]
> ]
>
>
> BeginPackage["InOut`NBImport`", "Utilities`FilterOptions`"]
>
> NBPicture::usage=
>    "NBPicture[\"filename.ext\"] imports and shows the picture with
> filename \
> 'filename' and file extension 'ext' contained in the subdirectory
> 'Pics' of \
> the directory of the current notebook.\n
> NBPicture[\"filename.ext\",DirName\
> ->\"dir\"] allows to specify the name of the subfolder into which the
> file is \
> stored. It allows for options to be used in Show and in Import.";
>
> Fig::usage=
>    "Fig[\"filename\"] imports and shows the first picture found in the
> \
> local \"Pics\" folder that has an extension listed in the FilePriority
> \
> option.\n
> Fig[\"filename\",DirName->\"dir\"] allows to specify the name of the \
> folder into which the file is stored (default is \"Pics\"). The option
> \
> SubDirName specifies the subfolder into which the pictures are to be \
> found (default is \"\")\n
> Fig[\"filename\",FilePriority->{\"ext1\",\"ext2\",...}] specifies a \
> list of file extensions to be associated with \"filename\". The options
> \
> Canvas and CanvasSize can be used to display a blank region when no \
> picture with the given filename and the extensions contained in \
> FilePriority is found.";
>
> NBFile::usage=
>    "NBFile[\"filename.ext\"] imports (and shows) the file with
> filename \
> 'filename' and file extension 'ext' contained in the subdirectory
> 'Data' of \
> the directory of the current notebook.\n \
> NBFile[\"filename.ext\",DirName->\"dir\"] allows to specify the name of
> the \
> subfolder into which the file is stored. It allows for options to be
> used in Import.";
>
> NBDirectory::usage=
>    "NBDirectory[] return the directory of the notebook being
>    evaluated."
>
> LocalGet::usage=
> "LocalNeeds[\"packagename`\"] loads a package that resides in the \
> same directory as the notebook. Working directory is changed to the \
> notebook's directory."
>
> RelativePath::usage=
> "RelativePath specifies the path, relative to the evaluated notebook,
> where to find a file or picture. It can be a string or a list of
> string."
>
> Canvas::usage=
> "Canvas is an option for Fig that specifies whether or not to  \
> display a blank space when no picture is found."
>
> CanvasSize::usage=
> "CanvasSize is an option for Fig that specifies the dimensions of
> the canvas to be shown when no picture is found."
>
> FilePriority::usage=
> "FilePriority is an option for Fig that specifies a list of file \
> extensions to be associated with \"filename\"."
>
> DirName::usage=
>    "DirName is an option for Fig that specifies the directory \
>    inside the notebook's folder into which looking for the pictures."
>
> SubDirName::usage=
> "SubDirName is an option for Fig that specifies the subdirectory \
>    inside the specified picture folder into which looking for the \
>    pictures."
>
>
>
>
>
> Begin["`Private`"]
>
>
> NBDirectory[]:=
> DirectoryName[
>
> ToFileName["FileName"/.NotebookInformation[EvaluationNotebook[]]
>        ]
>    ]
>
>
>
> Options[NBPicture]={RelativePath->"Pics"};
>
> NBPicture[filename_String,opts___]:=
>  Module[
>    {basedir,picdir,importopt,showopt},
>    showopt=FilterOptions[Show,opts];
>    importopt=FilterOptions[Import,opts];
>    basedir=
>      DirectoryName[
>
> ToFileName["FileName"/.NotebookInformation[EvaluationNotebook[]]]];
>    picdir=RelativePath /. {opts}/.Options[NBPicture];
>    SetDirectory[ToFileName[Flatten[{basedir,picdir}]]];
>    Show[Import[filename,importopt],showopt];
>    ResetDirectory[];
>    ]
>
>
>
>
> Options[Fig]={
> DirName->"Pics",
> SubDirName->"",
> Canvas->True,
> CanvasSize->{300,200},
> FilePriority->{"jpg","jpeg","png","bmp","gif","tiff","tif"}
> };
>
> Fig[filename_String, opts___]:=
>  Module[
>
> {basedir,picdir,subpicdir,showopt,importopt,filechoice,canv,canvSize},
>
>    showopt=FilterOptions[Show,opts];
>    importopt=FilterOptions[Import,opts];
>    basedir=
>      DirectoryName[
>        ToFileName["FileName" /.
> NotebookInformation[EvaluationNotebook[]]]];
>    picdir=DirName /. {opts} /. Options[Fig];
>    subpicdir=SubDirName /. {opts} /. Options[Fig];
>    SetDirectory[ToFileName[Flatten[{basedir,picdir,subpicdir}]]];
>    fileformats=FilePriority/.{opts}/.Options[Fig];
>    filechoice=FileNames[StringJoin[filename,".",#]& /@fileformats];
>    Switch[Length[filechoice],
>
>      0,     (* No file found: print canvas if required *)
>             canv=Canvas/.{opts}/.Options[Fig];
>             canvSize=CanvasSize/.{opts}/.Options[Fig];
>             If[canv,
>                 Show[Graphics[{},ImageSize->canvSize]];,
>                 Print["File not found."]
>             ],
>
>      1,     (* Unique file with that filename: import that file *)
>             Show[Import[filechoice[[1]],importopt],showopt];,
>
>      True,  (* Multiple files with different extensions: import the
>                file whose extension appears first in the FilePriority
>                option *)
>             Print["Multiple files found. Importing according to \
>             extension priority"];
>             Show[Import[First[filechoice], importopt],showopt];
>      ];
>    ResetDirectory[];
>    ]
>
>
>
>
> Options[NBFile]={RelativePath->"Data"};
>
> NBFile[filename_String,opts___]:=
>  Module[
>    {basedir,datadir,temp,importopt},
>    importopt=FilterOptions[Import,opts];
>    basedir=
>      DirectoryName[
>
> ToFileName["FileName"/.NotebookInformation[EvaluationNotebook[]]]];
>    datadir=RelativePath /. {opts} /.Options[NBFile];
>    SetDirectory[ToFileName[Flatten[{basedir,datadir}]]];
>    temp=Import[filename,importopt];
>    ResetDirectory[];
>    temp
>    ]
>
>
> LocalGet[packagename_String]:=(
> SetDirectory[NBDirectory[]];
> Get[packagename]
> )
>
>
> End[]
>
> Protect[NBPicture, NBFile, NBDirectory, (*Fig,*) LocalGet]
>
> EndPackage[]
>
> ===========================
>
>
>
>
>
>
> hope this helps,
> Peltio
>
> -- 
> Invalid address in reply-to. Crafty demunging required to mail me.
> (Hint: use peltio at despammed dot com).
> 



  • Prev by Date: Graphics, text and turning lines into tubes?
  • Next by Date: Re: Log Error ( ? )
  • Previous by thread: Re: Opening a foreign file from Mathematica
  • Next by thread: Re: Opening a foreign file from Mathematica