The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Inactive » comp.soft-sys.math.mathematica

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Default path for opening files
Replies: 14   Last Post: Jul 1, 2012 2:13 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
E. Martin-Serrano

Posts: 114
Registered: 9/5/08
Re: Default path for opening files
Posted: Jun 24, 2012 4:22 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Sorry for the following misspelling that led someone to confusion

In my response to this question,

where I said 'Insert' in

Insert[Framed@Column[{"NonAccelerating Inflation Rate of Unemployment",
fbuttontofile[Style["NAIRU (Natural Unemployment)-Ball&Mankiw", 12],
"NAIRU-BallMankiw.pdf"]}, Center], {xpos, ypos}]

I should have said 'Inset'

Inset[Framed@Column[{"NonAccelerating Inflation Rate of Unemployment",
fbuttontofile[Style["NAIRU (Natural Unemployment)-Ball&Mankiw", 12],
"NAIRU-BallMankiw.pdf"]}, Center], {xpos, ypos}]

Of course 'Insert' doesn't belong to this context and cannot work.

Anyway, that was only for illustration purposes: just about a possible way
of using 'fbuttontofile[]'. But insetting a button (like this) in a graphics
is not probably a good idea, since some objects (like controls) inset in
graphics, at least in my case, tend to create performance problems and even
sometimes doesn't work at all. It is better to put them in columns or rows,
as for example:

Plot[{Sin[x] + x/2, Sin[x] + x}, {x, 0, 10},
Filling -> {1 -> {2}}, ImageSize -> 300,
Epilog ->
Column[{"For detailed info on the function" ,
"click the button 'NAIRU' below"}, Center],
FrameStyle -> Lighter[Blue, .3], Background -> LightGray,
RoundingRadius -> 5], Scaled[{0.32, .7}]]],
FrameStyle -> Lighter[Brown, .3], Background -> LightYellow],
Style["NAIRU (Natural Unemployment)-Ball&Mankiw", 12],
"NAIRU-BallMankiw.pdf", 280]}, Center],
Style["NonAccelerating Inflation Rate of Unemployment", Bold]]

Here 'fbuttontofile[]' works nicely.

Sorry again.

E. Martin-Serrano

-----Mensaje original-----
De: E. Mart=EDn-Serrano []
Enviado el: viernes, 22 de junio de 2012 11:14
Asunto: Re: Default path for opening files


I am not sure whether I understand you question, but I can tell you what I
do to find and open files of any extension spread over all directories (not
only those directories managed directly by Mathematica in $Path)

The basic problem I understand is (a) I do not use Mathematica as the root
directory for all my notebooks (b) Although it can be argued that is a
good or safe practice to locate all notebooks under the directory
Mathematica, it is true, too, that not everything in a computer must be
located in that directory, among other reason because some files are shared
by non Mathematica applications. (c) Mathematica provides the tools to do it

This is an example of what I do (here I use a button but it can be done in
other forms).


fbuttontofile::usage = "fbuttontofile[buttonlegend, filename,isize], yields
a button to open any file in any directory from a notebook"

fbuttontofile[buttonlegend_, filename_, isize_: Full] :=

Module[{directoryfiles, fullpath, filedirectorypath, button},

(* I think this could be down better with Block to avoid saving and
restoring $Path each time the routine is invoked*)

bckpath = $Path ;

(* locates the directory path to the filename file down the
$UserDocumentsDirectory *)

Quiet[directoryfiles = FileNames[filename, {"*"}, Infinity]];

fullpath = FileNameJoin[{$UserDocumentsDirectory, directoryfiles} //

filedirectorypath = FileNameTake[fullpath, FileNameDepth[fullpath] - 1];

(* adds directory path for the filename file to $Path *)

AppendTo[$Path, filedirectorypath ];

button = Button[buttonlegend, SystemOpen[filename], ImageSize -> isize];

(* I do not know why, but I ought to put this dynamic wrapping here for the
button to work, see that button is local to the module *)


(* I would like to restore $path to its canonical setting here, but it does
work, I mean the button does not work *)


(* restore the canonical value for $Path outside the module *)

$Path = bckpath;

(* this is done elsewhere outside the function*)

(* the following creates a button to open a file 'external' to Mathematica
(PDF) *)

fbuttontofile[Style["To Open Existing 'external' PDF File", 12],

(* the button can be located, inserted or enclosed wherever you want *)

As in, for example

Insert[Framed@Column[{"NonAccelerating Inflation Rate of Unemployment",
fbuttontofile[Style["NAIRU (Natural Unemployment)-Ball&Mankiw", 12],
"NAIRU-BallMankiw.pdf"]}, Center], {xpos, ypos}]

(* where "NAIRU-BallMankiw.pdf" is a paper on the subject the notebook is
about and is located in a directory which contains docs about inflation and
prices level nothing about Mathematica *)

So, substitute the file name for any other in your system and it should

The routine can be improved a lot but it works as it is.

Any clue to improve it would be appreciated

E. Martin-Serrano

On 6/21/2012 5:15 AM, nanobio9 wrote:
> Dear Mathematicans,
> Whenever I tried to open a file from my Mathematica directory (with
> rather deep structure), I have to start with "~/Documents/" (I am
> using Mac). I searched the help and I found that this directory is
> coded in the variable "$UserDocumentsDirectory". OK. So I tried to set
> this variable to the root of my Mathematica directory, but I found it
> protected. Then I setup my local "init.m" file. I unprotect it, set
> it, and protect it, in the init.m file. (This is another story though.
> I searched the web for half a day and I do not see any example or
> instruction, official or by other users, on how people can compose
> their own init.m file.) However, nothing is changed.
> When my data files grow in number, I decided that I should use the
> "FileNameSetter" interface in my NB to pick the file on the fly. That
> way I do not have to modify the code ( filename=xxxx ...., blablabla )
> for each datafile. However, I found the same problem. In the
> "FileNameSetter" function, there is not an option to set the default
> starting path. (Or I am just too stupid to use electronic
> documentation so that I do not find the solution?) Everything starts
> with "~/Documents", although since the second time the directory tree
> will be expanded like what I saw the previous time (this is the case
> in Mac. I did not try under Windows or Linux).
> Is there any smarter way to do this? Look forward to your helpful
> comments.

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.