giovanni Posted April 12, 2021 Share Posted April 12, 2021 Hello, I was hoping to double check something about the rerun feature in script filters. If the script filter is returning more than one result while being rerun, and the user attempts to select an option beyond the first using arrow-down on the keyboard, the selection will be lost upon refreshing. So, I understand one can select one of the results only via mouse (if you are quick) or typing matching text, is that correct? Or, is there a way to store the result currently selected/highlighted via keyboard and serve it back upon refreshing? thanks in advance! Link to comment
vitor Posted April 12, 2021 Share Posted April 12, 2021 If you set a uid, your selection will be maintained (and jump around) as the order of results changes. giovanni and luckman212 2 Link to comment
giovanni Posted April 12, 2021 Author Share Posted April 12, 2021 AHA! awesome, thank you so much! Link to comment
giovanni Posted May 2, 2021 Author Share Posted May 2, 2021 @vitor this works but Alfred will learn the selection and change the order of results (undesired here). I read that a random UID is recommended to obviate this issue, however this random number should be generated outside the script filter, correct? (otherwise a new one gets generated for each rerun)... I used a Random utility and passed the output to the python script and that works, however now the script filter needs to be triggered either by a hotkey or a keyword (so an extra ↩️ is needed). Am I missing anything? In other words, is there any other way to pass to a script filter a random number that stays the same across multiple reruns of that script filter? thanks! Link to comment
deanishe Posted May 2, 2021 Share Posted May 2, 2021 12 minutes ago, giovanni said: read that a random UID is recommended to obviate this issue, however this random number should be generated outside the script filter, correct? If you want random UIDs that persist till the workflow is closed, use a pseudo-random function, but store the seed in a top-level workflow variable (alongside rerun). When you seed the generator with the same number, the same “random” results come out. Most languages have a pseudo-random number generator. It’s often the standard random function (which is why you’re not supposed to use it for generating passwords). Link to comment
giovanni Posted May 3, 2021 Author Share Posted May 3, 2021 thanks, but wouldn't I need to pass a new (random) seed, each time the workflow is triggered? Link to comment
deanishe Posted May 3, 2021 Share Posted May 3, 2021 16 minutes ago, giovanni said: thanks, but wouldn't I need to pass a new (random) seed, each time the workflow is triggered? Yes. That's the point, isn't it? Just use the current time. Link to comment
giovanni Posted May 3, 2021 Author Share Posted May 3, 2021 I must be missing something... If I do that, the random generator gets reset at each rerun... here is my simplified code: import sys import time from workflow import Workflow3 import random def main(wf): random.seed() # setting a value, e.g. random.seed(4) will produce the same number every time myRandString = str(random.randint(0, 1000)) wf.rerun = 1 goals=get_goals() for x in goals: wf.add_item(title=x['blabla1'], subtitle="blabla", icon = myIcon, valid='TRUE', uid=x['blabla2'] + myRandString, arg=x['myArg']) wf.send_feedback() if __name__ == u"__main__": wf = Workflow3() log = wf.logger sys.exit(wf.run(main)) Link to comment
deanishe Posted May 3, 2021 Share Posted May 3, 2021 49 minutes ago, giovanni said: If I do that, the random generator gets reset at each rerun... Not if you set a workflow variable to the seed. Top-level variables get passed back to your script on rerun. import os import random import time # get seed from environment variable or current time seed = float(os.getenv('seed') or time.time()) # initialise RNG with seed to get deterministic values random.seed(seed) def main(): # set top-level variable that will be returned on re-run wf.setvar('seed', str(seed)) wf.rerun = 1 giovanni 1 Link to comment
giovanni Posted May 3, 2021 Author Share Posted May 3, 2021 brilliant, that works! thanks as always 🙏 Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now