Jump to content

Making Script Filter `uid`s unique to each session.


Recommended Posts

In the Script Filter JSON format, setting a uid makes Alfred “aware” of an item, meaning it will be sorted according to Alfred’s knowledge next time it shows up. It also means your position in the results list is preserved when the Script Filter reruns during the same session.


On rare occasions you may wish for the latter without the former: results which are displayed in a preset order (ignoring Alfred’s knowledge) while keeping your position in the list. Session variables make that possible.


For clarity, I’ll use ENV[varName] when referring to an environment variable and varName when referring to a variable (or preferably constant) in your code.

  1. Check if ENV[uidSeed] exists.
  2. If yes: make uidSeed with the contents of ENV[uidSeed].
  3. If not: make uidSeed something unique. Good pick: random number. Better pick: current date and time.
  4. When outputting the Script Filter JSON, prepend uidSeed to the uid of every item and set it in variables.

The result: if your Script Filter was just launched the steps above will execute 1 → 3 → 4, but on every rerun they will be 1 → 2 → 4. This creates what we sought: results which are aware of themselves only during the current session.


Below are working snippets to accomplish the first three steps.


Swift:

import Foundation

let uidSeed: String = ProcessInfo.processInfo.environment["uidSeed"] ?? String(describing: Date())

 

JXA (JavaScript for Automation):

const uidSeed = $.NSProcessInfo.processInfo.environment.objectForKey("uidSeed").js || Date.now().toString()


Ruby:

UID_SEED = ENV['UID_SEED'] || Time.now.to_s


Python:

from datetime import datetime
import os

uid_seed = os.getenv('uid_seed') or str(datetime.now())

 

Edited by vitor
Link to comment
Share on other sites

13 hours ago, vitor said:

Good pick: random number. Better pick: current date and time.

 

PID is an ideal one for this case. Let the OS guarantee the uniqueness.

 

I’d also recommend this technique (caching a value in an environment variable) for passwords stored in Keychain. Calling /usr/bin/security is slow, but you don’t want to store passwords in plaintext.

 

So do password = get_env_var('varname') or get_and_export_keychain_password('account name', 'varname') to ensure /usr/bin/security is only called once instead of every time the Script Filter is run.

 

Link to comment
Share on other sites

On 3/5/2022 at 10:34 AM, deanishe said:

PID is an ideal one for this case. Let the OS guarantee the uniqueness.


I like date and time for this because the uniqueness is guaranteed over multiple sessions and reboots. With a random number or process ID, there’s a minute (but not zero) chance of a collision down the line.

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...