Re: ActionMenu with directories
- To: mathgroup at smc.vnet.net
- Subject: [mg105935] Re: [mg105926] ActionMenu with directories
- From: John Fultz <jfultz at wolfram.com>
- Date: Sun, 27 Dec 2009 02:24:00 -0500 (EST)
- Reply-to: jfultz at wolfram.com
If you look at the value Mathematica assigned to your variable 'menu', you'll
see why. Remember that RuleDelayed doesn't evaluate *anything* on the
right-hand side. That includes the value of 'k'.
With[] and pattern replacement are tools which Mathematica offers that allow you
to inject values directly into an expression which isn't being evaluated. Your
version could be fixed by wrapping With[] around the RuleDelayed, but in this
case, a pattern replacement ends up producing much simpler code.
I also added a Select[] to filter out filenames to only those which are actual
directories.
dirs = Select[FileNames[], FileType[#] == Directory &] ;
menu = dirs /. {x_String -> (x :> SetDirectory[x])};
ActionMenu["Choose Directory", menu]
Sincerely,
John Fultz
jfultz at wolfram.com
User Interface Group
Wolfram Research, Inc.
On Sat, 26 Dec 2009 19:07:32 -0500 (EST), Kevin J. McCann wrote:
> I would like to write a short script to create an action menu from which
> I can choose a directory. I have a data directory (Windows) whose only
> members are directories (each named by the date the data were taken). I
> would like to create an action menu that lists these directories and
> then does a SetDirectory to the chosen one. Here is what I have tried:
>
> dirs=FileNames[];
> numDirs=Length[dirs];
> menu={};
> For[k=1,k<=numDirs,
> AppendTo[menu,RuleDelayed[ToString[dirs[[k]]],SetDirectory[dirs[[k]]]]];
> k=k+1;
> ]
>
> ActionMenu["Choose Directory",menu]
>
> It creates the menu, but it doesn't convert the SetDirectory correctly.
>
> Any ideas?
>
> Also, is there a way to tell if a FileNames output is a directory or file?
>
> Thanks,
>
> Kevin