Jump to content

Simple ideas: intermission—variables & cheese sandwiches

Recommended Posts

Do not be frightened of variables! Variables are nothing more than little titbits saved in a pocket for future use—just like cheese sandwiches.

Except, except…you wouldn't put a cheese sandwich straight into your pocket, would you? First you'd tenderly wrap it to preserve its identity through the vicissitudes of pocket travel. So you won't be surprised to know that variables are exactly like cheese sandwiches: they need wrapping too.

The skeletal workflow

This is a quick and reasonably useless workflow purely to demonstrate use of variables: please excuse both it and me.

1. We have a Universal Action limited to URLs (and we'll talk more about Universal Actions another day). All we do is to select a URL (remember, you always need to select something before using a Universal Action), press our Universal Actions hotkey, chose "Copy the URL” and press :

2. We don't want to close the Alfred window (which we covered in this post).

3. We're going to need to use the URL later in the workflow so we must pocket it in order not to lose it. That's what we do here:

We use Alfred's Arg and Vars Utility. When we first open it {query} appears in the top box after Argument:. {query}, in this case, is the URL we just copied. We don't want to pass that on directly to the next item (the prompt for a friendly name) because if we do the URL will appear in the prompt field we have to complete. So we delete {query} if it appears after Argument:.

What we do need to do is to save the URL as a variable. To do that we click on the + sign at the bottom of the Variables: box. First we give the variable a sensible name. That's important because we want easily to be able to recall what we called it. You can use any form of name you like—although I tend to use Camel case as here. After we've named the variable we must indicate what it contains. In this case it's very simple: {query} is what Alfred passed in to this Arg and Vars utility, so {query} is the URL and that's what we want to save to the variable.

4. Now, for the first time we're going to use a variable <cue sound of distant trumpets>. The prompt for the URL friendly name is this:

Friendly name for '{var:theURL}'

(Note you can use variables in prompts like that—as well as in things like notifications and dialogs—to make them more helpful.)

It's very important to note that when we use a variable we must use its wrapped-up form so that Alfred recognises it as a variable. That form is {var:variableName}: so the only thing that changes from case to case is variableName.

In this case we've encased the variable in single quotation marks simply to make look better in the prompt:

5. We don't close Alfred's window.

6. Now, in a blaze of glory and another Arg and Vars Utility, we assemble the final text:

We use the saved URL ({var:theURL}), the friendly name (which Alfred has passed us as {query}) and a little bit of text between them.

We want to pass the text to Alfred 5.5's Text View but we can't pass out a new created variable from the Arg and Vars Utility that created it so, while ensuring this Arg and Vars Utility is not passing out anything (blank Argument:, unchecked Pass through input argument), we…

7. Pass the text with another Arg and Vars Utility:

Note, again, that the variable we saved as theText must appear here as a wrapped variable Alfred will recognise: {var:theText}.

8. Finally…in Alfred's text view {var:theText} is translated into Real (if uninspiring) English:

In conclusion
This was intended to be a short post but, obsessed with the variables of cheese and Spam and the need to provide a foundation for future simple ideas, it has not been so. I apologise. Please take a break, choose your favourite sandwich and munch while meditating upon the delights of variables.




The Simple Ideas posts

Edited by Stephen_C
Add link to the Simple Ideas posts
Link to comment

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
  • Create New...