|
16 | 16 | from . import bioformats as bf |
17 | 17 | from . import prefs |
18 | 18 |
|
| 19 | +from org.scijava.widget import WidgetStyle |
| 20 | +from org.scijava.widget import TextWidget |
| 21 | + |
19 | 22 |
|
20 | 23 | def show_status(msg): |
21 | 24 | """Update the ImageJ status bar and issue a log message. |
@@ -713,3 +716,57 @@ def run_imarisconvert(file_path, pixel_calibration=None): |
713 | 716 | IJ.log("Conversion to .ims is finished.") |
714 | 717 | else: |
715 | 718 | IJ.log("Conversion failed with error code: %d" % result) |
| 719 | + |
| 720 | + |
| 721 | +def save_script_parameters(destination, save_file_name="script_parameters.txt"): |
| 722 | + """Save all Fiji script parameters to a text file. |
| 723 | +
|
| 724 | + Parameters |
| 725 | + ---------- |
| 726 | + destination : str |
| 727 | + Directory where the script parameters file will be saved. |
| 728 | + save_file_name : str, optional |
| 729 | + Name of the script parameters file, by default "script_parameters.txt". |
| 730 | +
|
| 731 | + Notes |
| 732 | + ----- |
| 733 | + This function records all input parameters defined in the Fiji script header |
| 734 | + (e.g. `#@ String`) to a text file. |
| 735 | +
|
| 736 | + The following parameters are excluded: |
| 737 | + - Parameters explicitly declared with `style="password"` are ignored. |
| 738 | + - Runtime keys (e.g. 'SJLOG', 'COMMAND', 'RM') are also skipped. |
| 739 | + """ |
| 740 | + # Get the ScriptModule object from globals made by Fiji |
| 741 | + module = globals().get("org.scijava.script.ScriptModule") |
| 742 | + if module is None: |
| 743 | + IJ.log("No ScriptModule found - skipping saving script parameters.") |
| 744 | + return |
| 745 | + |
| 746 | + destination = str(destination) |
| 747 | + out_path = os.path.join(destination, save_file_name) |
| 748 | + |
| 749 | + # Access script metadata and inputs |
| 750 | + script_info = module.getInfo() |
| 751 | + inputs = module.getInputs() |
| 752 | + |
| 753 | + # Keys to skip explicitly |
| 754 | + skip_keys = ["USERNAME", "SJLOG", "COMMAND", "RM"] |
| 755 | + |
| 756 | + with open(out_path, "w") as f: |
| 757 | + for item in script_info.inputs(): |
| 758 | + key = item.getName() |
| 759 | + |
| 760 | + # Skip if any keys are in the skip list |
| 761 | + if any(skip in key.upper() for skip in skip_keys): |
| 762 | + continue |
| 763 | + |
| 764 | + # Skip if parameter is declared with password style |
| 765 | + if WidgetStyle.isStyle(item, TextWidget.PASSWORD_STYLE): |
| 766 | + continue |
| 767 | + |
| 768 | + if inputs.containsKey(key): |
| 769 | + val = inputs.get(key) |
| 770 | + f.write("%s: %s\n" % (key, str(val))) |
| 771 | + |
| 772 | + print("Saved script parameters to: %s" % out_path) |
0 commit comments