Hi,
When my plugin is executed from a command line sometimes the default directory gets incorrectly set (to c:\windows). So far I have not not figured out what is going on, but a possible fix would be to set that default directory to be the directory of the image opened in my plugin. Any way to do that? I don't see any way in the ImageJ open code. Jon -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
On Sun, April 5, 2015 20:10, Jon Harman wrote:
> When my plugin is executed from a command line sometimes the default > directory gets incorrectly set (to c:\windows). > So far I have not not figured out what is going on, but a possible fix > would be to set that default directory to be the directory of the image > opened in my plugin. Any way to do that? I don't see any way in the > ImageJ open code. Hi Jon, ij.io.OpenDialog has a method public OpenDialog(String title, String defaultDir, String defaultName) where you can set the default directory. If you use some other way of opening the file, you can use the OpenDialog method public static void setDefaultDirectory(String defaultDir) to set the default directory. You can get the directory of an image via the ImagePlus method String dir = imp.getOriginalFileInfo().directory; Note that the directory many be null (if the image was not read from disk). Michael -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Thanks Michael.
My knowledge of Java is minimal. I don't see how to use setDefaultDirectory() without an OpenDialog. I now do this: FileInfo fi = imp.getOriginalFileInfo(); if(fi.directory != null) { OpenDialog defdia = new OpenDialog("",fi.directory); } Jon On 4/5/2015 11:52 AM, Michael Schmid wrote: > On Sun, April 5, 2015 20:10, Jon Harman wrote: >> When my plugin is executed from a command line sometimes the default >> directory gets incorrectly set (to c:\windows). >> So far I have not not figured out what is going on, but a possible fix >> would be to set that default directory to be the directory of the image >> opened in my plugin. Any way to do that? I don't see any way in the >> ImageJ open code. > > Hi Jon, > > ij.io.OpenDialog has a method > public OpenDialog(String title, String defaultDir, String defaultName) > > where you can set the default directory. If you use some other way of > opening the file, you can use the OpenDialog method > public static void setDefaultDirectory(String defaultDir) > > to set the default directory. > > You can get the directory of an image via the ImagePlus method > String dir = imp.getOriginalFileInfo().directory; > Note that the directory many be null (if the image was not read from disk). > > Michael > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html > -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by Jon Harman-3
Hello Jon,
I recently also had problems with OpenDialog that might be related to yours. Calls to OpenDialog.setDefaultDirectory() had no effect, i.e., they left the default directory unchanged. I had no time to investigate further but noticed that, e.g., after calling OpenDialog.setDefaultDirectory("/"); the result from OpenDialog.getDefaultDirectory() was the path "/\". Looking at the source code this is due to the instruction (http://rsbweb.nih.gov/ij/developer/source/ij/io/OpenDialog.java.html) defaultDirectory = defaultDirectory + File.separator; at the end of the setDefaultDirectory method (and "\" being the default file separator in Windows). I am not sure the problem shows in non-Windows environments at all. In any event, the problem mysteriously disappeared when I set the "Use JFileChooser to open/save" in the Input/Output... options. This might explain why the problem appears inconsistently and I still suspect there is a bug in the setDefaultDirectory method. Hope this helps, Wilhelm ________________________________________ From: ImageJ Interest Group [[hidden email]] On Behalf Of Jon Harman [[hidden email]] Sent: Sunday, April 05, 2015 20:10 To: [hidden email] Subject: Possible to set default directory Hi, When my plugin is executed from a command line sometimes the default directory gets incorrectly set (to c:\windows). So far I have not not figured out what is going on, but a possible fix would be to set that default directory to be the directory of the image opened in my plugin. Any way to do that? I don't see any way in the ImageJ open code. Jon -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by Jon Harman-3
Hi Jon,
your code is absolutely fine (I guess that you have an 'else' branch for the case that fi.directory == null). By the way, OpenDialog.setDefaultDirectory is a static method, so you can set it without having a particular instance of OpenDialog, just use the class name as a prefix: OpenDialog.setDefaultDirectory(myDefaultDirectory); The String argument (here 'myDefaultDirectory') may have a path separator (backslash for Windows, otherwise slash) at the end. You need the static method if the OpenDialog is no called by your code but rather by some other command, so you have no access to the instance of OpenDialog. Michael ________________________________________________________ On Sun, April 5, 2015 22:21, Jon Harman wrote: > Thanks Michael. > > My knowledge of Java is minimal. I don't see how to use > setDefaultDirectory() without an OpenDialog. > > I now do this: > > FileInfo fi = imp.getOriginalFileInfo(); > if(fi.directory != null) { > OpenDialog defdia = new OpenDialog("",fi.directory); > } > > Jon > > > On 4/5/2015 11:52 AM, Michael Schmid wrote: >> On Sun, April 5, 2015 20:10, Jon Harman wrote: >>> When my plugin is executed from a command line sometimes the default >>> directory gets incorrectly set (to c:\windows). >>> So far I have not not figured out what is going on, but a possible fix >>> would be to set that default directory to be the directory of the image >>> opened in my plugin. Any way to do that? I don't see any way in the >>> ImageJ open code. >> >> Hi Jon, >> >> ij.io.OpenDialog has a method >> public OpenDialog(String title, String defaultDir, String >> defaultName) >> >> where you can set the default directory. If you use some other way of >> opening the file, you can use the OpenDialog method >> public static void setDefaultDirectory(String defaultDir) >> >> to set the default directory. >> >> You can get the directory of an image via the ImagePlus method >> String dir = imp.getOriginalFileInfo().directory; >> Note that the directory many be null (if the image was not read from >> disk). >> >> Michael >> >> -- >> ImageJ mailing list: http://imagej.nih.gov/ij/list.html >> > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html > -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Free forum by Nabble | Edit this page |