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: [mg78939] Re: Opening a foreign file from Mathematica
  • From: David Reiss <dbreiss at gmail.com>
  • Date: Fri, 13 Jul 2007 06:04:25 -0400 (EDT)
  • References: <f6vnng$qj7$1@smc.vnet.net><f72boa$c63$1@smc.vnet.net>

I see.  Hmmm.  Generally frontend-like programs (ie., that do things
like what I illustrated) have a only a small chance of being able to
be changed into pur FrontEnd code like you are desiring.  It might be
possible in this case, but it'd require some experimentation.  One
thing that one does is to look at all of the functions in the
FrontEnd` context to see if there are similarly named ones to what I
used in the Kernel.  Alas, I don't have time to do this right now.
Perhaps someone else qould like to experiment with thins and report
back tothe group?

--David
A WorkLife FrameWork
E x t e n d i n g MATHEMATICA's  Reach...
http://scientificarts.com/worklife
Compatible with Mathematica 6


On Jul 12, 5:31 am, "Steve Luttrell"
<steve at _removemefirst_luttrell.org.uk> wrote:
> All I want is a way to make hyperlinks-to-foreign-files that are as simple
> to build and which operate as cleanly as hyperlinks-to-Notebooks do
> currently.
>
> The Cell expression
>
> Cell[BoxData[
>     ButtonBox["test",
>       ButtonData:>{
>         URL[ "C:\\temp\\foo.doc"], None},
>       ButtonStyle->"Hyperlink"]], "Input"]
>
> is the only form that I know works correctly, and even that form has to be
> created manually, or at least I can't find of way of coercing Insert /
> Hyperlink into creating this type of Cell expression. The main limitation of
> this Cell expression is that it uses an absolute (rather than relative) file
> path, and its main advantage is that it evaluates using only the front end.
>
> Unfortunately, your generalisation of this makes use of the kernel; I was
> hoping that there was a way of doing it entirely in the front end, so that
> someone who didn't have a full Mathematica installation could also use my
> hyperlinks-to-foreign-files.
>
> --
> Steve Luttrell
> West Malvern, UK
>
> "David Reiss" <dbre... at gmail.com> wrote in message
>
> news:f72boa$c63$1 at smc.vnet.net...
>
> > Steve,
>
> > I am not sure that I completely understand the question here, but let
> > me phrase a question, answer it, and perhaps it will give you an
> > answer.   Or you can rephrase things so that we can go through an
> > iteration [contact me off-group if you want to].
>
> > So here's a question.  I have a notebook, and within it I want to
> > place a function that I can execute to open a file that is on some
> > known path **relative** to that notebook's path.  And I want this
> > function to work if I copy the notebook and the file to somewhere else
> > but maintain their directory relationship.
>
> > Let's say that the notebook is in some directory that has as its full
> > file path, dir, and that the file, which I will call "foo.other", is
> > in a subdirectory, "subdir", of dir.  (Of course dir is a  string.)
>
> > Given this, the full filepath to "foo.other"  is
>
> > ToFileName[{dir, "subdir"}, "foo.other"]
>
> > The remaining question is then how do we find out what dir is (I
> > assume that we know what the name "subdir" is, but there are ways of
> > discovering that as well--I will comment on that below).
>
> > Within the notebook (assuming that it is saved on disk--this will not
> > make sense if it doesn't exist on disk of course)  if I execute the
> > following I will get the value of dir
>
> > dir = DirectoryName[ToFileName["FileName" /. NotebookInformation[]]]
>
> > So, the path to the file "foo.other" is now
>
> > filepath =
> > ToFileName[{DirectoryName[ToFileName["FileName" /.
> > NotebookInformation[]]],
> >   "subdir"}, "foo.other"]
>
> > There is one further little bit that is needed to make the approach
> > that I am outlining cross-platform.  The function that one would now
> > use to open the file in its default application would be
> > NotebookLocate[{URL[fileLink], None}].  However,  how the value of
> > fileLink (which I haven't specified yet) is built from the value of
> > filepath depends on the operating system that you are using.  If it is
> > Windows then fileLink = filepath; if it is MacOS X, then it is
> > fileLink = "file://" <> filepath.  I believe that this is true for
> > other Unix systems as well.  (This is one of the things that is done
> > inside of the function OpenFileOrDirectory in "A WorkLIfeFrameWork").
>
> > So, finally, if I execute (I put this in a Module, only to show the
> > different pieces of the code nicely)
>
> > Catch@Module[{filepath, fileLink},
>
> >  dir = DirectoryName[ToFileName["FileName" /.
> > NotebookInformation[]]];
>
> >  filepath = ToFileName[{dir, "another"}, "example.txt"];
>
> >  Which[
>
> >   StringMatchQ[$System, "Mac OS X*"],
> >   fileLink = "file://" <> filepath,
>
> >   StringMatchQ[$System, "Microsoft Windows*"],
> >   fileLink = filepath,
>
> >   StringMatchQ[$System, "Linux" | "Unix"],
> >   fileLink = "file://" <> filepath,
>
> >   True,
> >   Throw[$Failed]
>
> >   ];
>
> >  NotebookLocate[{URL[fileLink], None}]
>
> >  ]
>
> > then this will open up the file "foo.other" in its default
> > application.
>
> > A couple of comments.
>
> > One: if "foo.other" is in a directory that contains dir then we'd have
> > to shuffle this code around a bit
>
> > Two: if we know that "foo.other" is in a subdirectory of dir, but
> > don't know which subdirectory it is in, we could "discover" where it
> > is (assuming that there is only one "foo.other") by looking at the
> > output of
>
> > FileNames[{ToFileName["*", "foo.other"]} {dir}, n]
>
> > where n is some sensible modest value.
>
> > So, to summarize, aside from the cross platform detail dealt with by
> > the Which statement above, the key thing in all of this is to (1) find
> > out where the notebook that this is executed is by suing
> > NotebookInformation, (2) to use commands to create a file path using
> > ToFileName, and (3) to use NotebookLocate in the form
> > NotebookLocate[{URL[fileLink], None}]
>
> > I hope that this helps...
>
> > --David
> > A WorkLife FrameWork
> > E x t e n d i n g MATHEMATICA's  Reach...
> >http://scientificarts.com/worklife
> > Compatible with Mathematica 6
>
> > On Jul 10, 6:40 am, "Steve Luttrell"
> > <steve at _removemefirst_luttrell.org.uk> wrote:
> >> Yes, that reads "from" not "in" Mathematica. Several years ago I asked in
> >> this forum how to create a hyperlink in a Mathematica notebook that when
> >> clicked would open a file in its associated application, thus emulating
> >> the
> >> effect of double-clicking on the file (at least in MS Windows, that is).
> >> Here is (part of) a response to that:
>
> >> =======================================
> >> Working in Windows I guess you should not have any problems in doing
> >> this.
> >> Just create a normal hyperlink button, but set the ButtonData to:
>
> >> {URL["fullfilename"], None}
>
> >> where, fullfilename is the full path to the file you want to
> >> open (with backslashes doubled as is normal in Mathematica strings).
>
> >> An example cell expression which might open a Word document:
>
> >> Cell[BoxData[
> >>     ButtonBox["test",
> >>       ButtonData:>{
> >>         URL[ "C:\\temp\\foo.doc"], None},
> >>       ButtonStyle->"Hyperlink"]], "Input"]
> >> =======================================
>
> >> This worked perfectly which is why I kept the code snippet for future
> >> use,
> >> but I somehow lost the name of whoever wrote this response.
>
> >> 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.
>
> >> --
> >> Steve Luttrell
> >> West Malvern, UK




  • Prev by Date: Re: confused!
  • Next by Date: Is FilledContourPlot exist ? mathematica 5
  • Previous by thread: Re: Opening a foreign file from Mathematica
  • Next by thread: Looking for Mathematica Consultant