Dynamic variable (in a For Each action)

I’m sorry, I’ve read some threads on dynamic variables, but I’m having a hard time getting my head around it.

I’m trying to use a dynamic variable in a For Each action…

Bottom line:

If a Finder folder is Smith, then the variable “Smith” should be used in the For Each action. If the Finder folder is “Roberts,” then the variable “Roberts” should be used in the For Each action.

Further explanation:

Assume I have a Variable titled “Smith” with the several lines of data (student names).

Assume I have an open, front-facing Finder folder with the title “Smith.” (I use a script to get the title of that folder and put it in a variable called “Folder.”)

Obviously, the Finder folder and the variable share the same title of “Smith.”

However, the front-facing open folder title can vary. It might be “Roberts” or “Benjamin.” So, I want to use a dynamic variable in a For Each action so that, whichever folder is open and at the front, the variable of the same title will be used in the For Each action.

So, if the folder is Smith, then the variable “Smith” will be used in the For Each action.
If the folder is Roberts, then the variable “Roberts” will be used in the For Each action.

Do you mean “used as the source”?

This should do what you want I think:

The FinderInsertionLocation token returns the path to the front Finder window.

The Split Path action will extract part of the path, in this case the folder name.

The Filter action with the Value of Named Variable filter will then get the text from the specified named variable.

Then the For Each action with the Lines In collection will iterate through the lines in the variable value and set the variable Student to each line in turn.

Note that I would not really recommend this approach - storing long term data in variables with set names is likely to lead to pain in the future. Better would be to store all the data in a file, or perhaps store the data in a file in each folder. Even a Dictionary would likely be a better solution.

1 Like