Jump to content

stupid simple calculation workflow isn't simple


Recommended Posts

Posted (edited)

Hi @Cliff, you need to return a JSON or XML object.

if you can use zsh (with input as argv) this below works

 

#!/bin/zsh

myVar=$(( $1 *0.029 +0.3))
myVar_f=$(printf "%.2g" $myVar)

cat << EOB
{"items": [

	{
		"title": "your fee is $myVar_f",
		"subtitle": "your input was $1",
		"arg": $myVar_f
		}
	

	

]}
EOB

 

 

Edited by giovanni
Posted

@giovanni tyvm but is there any way to stick with PHP so I can be comfortable editing further and to learn for other snippets?

  1. In PHP, I tried both `return` and `echo` of JSON and it didn't work like your script did
  2. I want the result to pass through to the clipboard action so I can auto-paste

Posted (edited)
5 hours ago, Cliff said:

And the start of my workflow is here: https://share.getcloudapp.com/llu5eeEE

 

You misunderstand how Alfred works. There is only one argument to your script, $argv[1], which contains everything the user entered. You’re using $argv[2] etc., which don’t exist.


You also can’t just echo a result from a Script Filter. You must output JSON in the format expected by Alfred.

 

Try this code instead:

<?php

$gross = $argv[1];  // user input
$pct = 0.029;
$flat = 0.3;

$sum = $gross * $pct + $flat;

// Alfred feedback JSON
$item = array('title' => "$sum", 'arg' => "$sum");
$json = json_encode(array('items' => array($item)));

echo $json;  // send JSON to Alfred
?>

 

Edited by deanishe
Posted

@deanishe thank you! It worked :)

 

However, when I change it from "with input as argv" to "with input as {query}" (and changed your code to either $gross = {query}; or $gross = "{query}"; then it wouldn't work.

 

Where is documentation for this? I didn't find these specifications at https://www.alfredapp.com/help/workflows/

 

For example, what else is available within the $argv array?

Posted
17 hours ago, Cliff said:

However, when I change it from "with input as argv" to "with input as {query}" (and changed your code to either $gross = {query}; or $gross = "{query}"; then it wouldn't work.

 

Then you've done something wrong. And you should almost always use "with input as argv", anyway. It works much better.

 

17 hours ago, Cliff said:

For example, what else is available within the $argv array?

 

Nothing else. ARGV is where command-line arguments are stored. That's just how UNIX programs work.

 

It's not mentioned in Alfred's documentation because it's a topic for your programming language's docs.

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...