GenericDialogPlus -- storing addFileField as variable?

dialog
jython
python
fiji
Tags: #<Tag:0x00007fb87d2cb6f8> #<Tag:0x00007fb87d2cb5b8> #<Tag:0x00007fb87d2cb478> #<Tag:0x00007fb87d2cb338>

#1

Newbie to scripting here.

I’d like to create a dialog window that prompts user to choose 2 files (“pos”, and “neg”). I’d also like to store these file paths as a variable to use later on. I’m having trouble storing the file paths. Instead of storing the chosen paths, I am only able to collect the default path.

import os
from java.io import File
from ij import IJ
from ij.gui import GenericDialog
from fiji.util.gui import GenericDialogPlus

gdp = GenericDialogPlus("test")
gdp.addFileField("pos", "C:/Users/")
pos = gdp.getNextString()
gdp.addFileField("neg", "C:/Users/")
neg = gdp.getNextString()
gdp.showDialog()
print("yourfiles "+ pos+ " " + neg)

But this just returns:

yourfiles C:/Users/ C:/Users/

even if the user has selected a new file, ie: C:/Users/test.tiff

I’d like the output to be based on whatever the user chose:

yourfiles C:/Users/test.tiff C:/Users/negative.tiff

What command am I missing? Thanks!


#2

Welcome to the forum, @sjeschonek!

You need to get the values only after showing the dialog:

gdp = GenericDialogPlus("test")
gdp.addFileField("pos", "C:/Users/")
gdp.addFileField("neg", "C:/Users/")
gdp.showDialog()
pos = gdp.getNextString()
neg = gdp.getNextString()
print("yourfiles "+ pos+ " " + neg)

Note that you can also use script parameters to create a dialog asking for user parameters:

#@ File (value = "C:\\Users\\", style = "open, extensions:tif/tiff") pos
#@ File (value = "C:\\Users\\") neg

print("yourfiles "+ pos+ " " + neg)

#3

Note that this question was cross-posted on stackoverflow:

@sjeschonek please always disclose and cross-link when cross-posting, so that others interested in the issue will be able to see all suggestions, and those people answering don’t waste their time with something that was solved already.


#4

Sorry! I’ll be sure to note X-posting next time.