Opening Files#
In this section we will start building our actual script piece-by-piece. The script we are currently working on in any section will always appear like this:
Important
Whenever the script is hidden in a drop-down menu, try to come up with the solution yourself before looking at the answer.
Let's start by putting in our loop functionality from the previous section:
Macro: Open, Save and Close Files
Now that we have learned how to loop through files, we will open them. How can we find out how to open files? There are always two answers:
- Go to the ImageJ macro guide
- Use the macro recorder
In this case we can see using the macro recorder that the function to open a file is:
What should we put for "path/to/file"? This is the path
of a file, which is like an address to where the file lives on the computer. In this case we know the path of our files is path/to/file
. We need to pass in as an argument to the open
function a concatenation of this path (a string) and the file name (another string).
How can we generate the path?
Play around with this script a bit. See what happens when you change various things. Make sure you understand, in the final file_path
variable, where each of the components come from.
Now, add this code snippet to your script, but instead of using print
to print the path of the file, use open
to open the file.
Macro: Open, Save and Close Files
If you run the code in the second cell, you will see that all files will open!