KM has a couple of actions that helps with regard to processing tokens, variables and calculations:
There's also another (older?) way to dereference a variable: using AppleScript. The method is different depending upon whether your variable is local or global. You didn't indicate whether your variable was local or global. The following example shows how to assign a value (the parameter passed to the macro) to a local variable called LocalValue.
Those are your options. But your scenario involves finding the variable(s) in the line and replacing them with their values. That may require some extra coding. But I want to get some breakfast soon so for now I'll just leave you with this lead.
My objective is not to filter these sentences which contain variables put I just want KM to replace the variables inside those sentences by a specific text which I indicate at the beginning of the macro.
The variables are local ones, I set them as follow:
Yes I know, that's what my examples were meant to show. I wasn't "filtering" anything, that's just the name of the KM action (and it's a bit of a misnomer for the example I provided.) In particular the first example shows how to replace a variable name with its contents. That's what you are asking for.
Then you said your variables are "local ones" but your example showed a "global variable". Are you familiar with the difference between local and global variables?
JM gave another answer. His advice is almost always great. In this case, I don't think it is because you want the variable evaluated not when the sentence is assigned to a variable, but at a future time. That's what my advice was attempting to solve for you. My advice was about letting you store variable names as part of sentences and evaluating them later. JM's advice was about evaluating a variable name at the time it is assigned to a variable, which I think is not what you want. But if I'm wrong, that's fine.
I checked the difference between local / global variables and yes my variables are global ones.
If I have followed correctly, by integrating the AppleScript, I'll be able to assign the variable which is set at the beginning of the macro and then use it within the shell script ?
If I have understand well what your macro is doing:
I set a list of sentences
The macro create a temporary txt file
The macro processes the actions I have defined until every one of the sentence has been used (and they are used once they have been removed from the list
The macro processes the actions I have defined until every one of the sentence has been used (and they are used once they have been removed from the list --
Have you tried running the macro? I think that will answer all your questions.
It does NOT require a file, but you can optionally get your question list from a file using this Action:
@JMichaelTX Your macro is working but there is one thing which does not fit exactly my needs.
Indeed, the macro is set to loop until the list is empty while I only need the macro to select a random line within the list, remove it so when it's processed again, the previously chose line is not available.
In that sense, I would need the macro not to loop the actions until the list is empty.
That's easy enough. Just take the Actions out of the Repeat Action, and change the "Local__Master" list to a global variable like "DND_RT__MasterList". Of course you need some way to initialize that variable.
So, I just took out the actions from the Repeat Action as you suggested, it worked perfectly. However, I did not change the "Local__Master" list to a global variable. Is it really necessary ?
Depends on your use case. Local variables cease to exist when the Macro terminates, so each time you trigger the Macro it will start with the same data. If you want the line removed to NOT be in the data for the next trigger, then you would need to use a global variable.
@JMichaelTX So, I have transformed the Local variables into Instance ones. But it does not work as it's not writting anything when it should be.
I've done the following modification to your script:
I've placed the 2 first actions (Replace with your... + Set Variable "Instance__CurrentList") before connecting to the first profile (i'm referring to the scheme I previously posted)
Are my modifications correct ? Would you script work the way I arranged it (or should all the actions of the script be at the same place to work properly) ?
Well, it is hard to tell just from looking at your screenshots.
The best way to find out is just to actually test the macros. Trigger them like you would for your workflow.
Yes I ran test with the instance variables and it did not work. However, I figure out that by using a global variable, it works. Maybe the instance variable is not "wide" enough, not integrating all my sub-macros levels.
So, the solution for the ones looking to do something similar:
Place the 2 first actions at the beginning of your macro + use a global variable instead of the local one, be sure to change every variable
Define all the actions you need in between
Each time you need to enter a random sentence, place the script + an "insert text" variable + append variable
That is correct. Instance variables can only be shared/seen by the main macro and any sub-macros it calls. They cannot be seen by other macros triggered independently.
I ran Peter's "Paste Random.kmmacros 18 (3.5 KB)" and it didn't work. Used as is, it just keeps choosing the last item in the list. In this case, it was a routine day, over and over and over.
Maybe it needs to be updated for KM 9.2?
I'm trying to get the path of a random file in the Finder's selection. I must be doing something wrong, because currently it outputs the entire list in its original (alphabetical) order.