Alfred... passing variable from AppleScript step to Notification step

Be patient on this one. I’m trying to learn and I’m doing a bit of blind googling!

I have an Alfred workflow where I would like to pass the value of a variable set within an AppleScript step to a Post Notification step.

I’ve put together a very simple demonstration workflow to try to get this working. I’m clearly doing something wrong with the AppleScript. The code I’m using I borrowed from other similar Alfred discussion I found online. I clearly don’t understand.

I’m posting screenshot of the workflow as I thought it would be easiest. The behavior I expect to see in this simple example is display of alert “The message is sample alert message”. Instead, the output is simply “The message is”. Obviously my AppleScript and variable handling is not correct.

You’ll see both the AppleScript I’m using and the Post Notification. Any tips? Thanks — jay

Here’s one way to do it where you can set multiple variables.

Let’s have three steps - a trigger, action (AppleScript) and output (notification).

First a bit of AppleScript that is outputting some special Alfred JSON.

Raw AppleScript
on run argv
	set strJSON to "{\"alfredworkflow\": {\"variables\": {"

	set strJSON to strJSON & "\"TITLE\": \"the title\", "
	set strJSON to strJSON & "\"BODY\": \"the body\", "

	set strJSON to strJSON & "}}}"

	return strJSON
end run

This JSON tells Alfred to set some variables, which are then accessed via the {var:****} placeholders.

The notification then populates with the text that was set in the AppleScript.

If you just want to use a single variable, then if you return the value you want from the AppleScript, you can add a subsequent args and vars utility step that you can use to take the argument (e.g. {query}) and set it to the variable.

Here’s a simple version of that.

Hope that helps.

3 Likes

Typical @sylumer response. Thoughtful, detailed and quite helpful.

Thank you for posting. I look forward to spending some time with this later this evening. I appreciate the assistance and the opportunity to learn… cheers — jay

works great. Updated my workflow and it is now working as I would like. Thanks again

1 Like

Of course, there are situations where it’s useful to store the value in a variable for re-use, but for the majority of times when this isn’t necessary, one doesn’t need the args and vars step; simply insert {query} into the Post Notification output, which will be a placeholder for whatever the AppleScript returns.