slyfox Posted September 13, 2019 Posted September 13, 2019 (edited) How can I have Alfred do a basic calculation? 1. Input Keyword variable 2. round({query}/10) 3. Output to {query} Edited September 13, 2019 by slyfox
deanishe Posted September 13, 2019 Posted September 13, 2019 Alfred's advanced calculator supports round: =round(115/10) Beyond that, you can use a Run Script and your preferred language (they can almost all do rounding). Just print / echo the result, and that becomes the {query} for the next action.
slyfox Posted September 14, 2019 Author Posted September 14, 2019 Where specifically in the Argument and Variables Utility should I enter the math formula? "=round({query}/10)"
deanishe Posted September 14, 2019 Posted September 14, 2019 You type the calculation directly into Alfred's query box.
slyfox Posted September 14, 2019 Author Posted September 14, 2019 This I know. I was under the impression that this formula can be included into "Arg and Var" utility somehow to do the calculation inside the workflow. Or this is only possible with a script? Not sure what I am doing here with a script though,.
deanishe Posted September 14, 2019 Posted September 14, 2019 1 hour ago, slyfox said: I was under the impression that this formula can be included into "Arg and Var" utility somehow to do the calculation inside the workflow. No. You can construct the calculation, but you can't evaluate it (using Alfred's own calculation engine) within the workflow. Arg and Vars only supports expanding {query} and {var:...} macros. I don't know exactly what @GuiB's workflow in the other thread was doing, but I suspect it was assembling the formula in the Arg and Vars and then passing it back to Alfred for evaluation with tell application id "com.runningwithcrayons.Alfred" to search "{query}" (or similar). 1 hour ago, slyfox said: Or this is only possible with a script? That's a fairly simple and powerful way, yes, but bash is a very poor language for doing maths in. You're better off with a programming language designed for that sort of thing. Here's the correct way to round numbers in Python: from __future__ import print_function from decimal import * import sys num = Decimal(sys.argv[1]) # Divide by 10 (or whatever) result = num / Decimal(10) ctx = getcontext() # Number of decimal places to round to ctx.prec = 2 # Rounding mode # https://docs.python.org/2.7/library/decimal.html#decimal.Context ctx.rounding = ROUND_HALF_EVEN print(result, end='') # Or to round to nearest whole number # print(result.to_integral_value(), end='')
GuiB Posted September 14, 2019 Posted September 14, 2019 @slyfox, yes @deanishe is right about the first example whose passing the Arg & Vars to Alfred, but my second example uses Python. Here is the workflow updated again: https://d.pr/f/z8i7VM But to put it here in the forum: My first one uses an expression in the Arg & Vars (ex: `round(({query}+({query}/0.971))*100)/100` ) and pass it to Alfred using `tell application id "com.runningwithcrayons.Alfred" to search "={query}"` My second example uses a Script Filter set to the Python language and what is great is that you can see directly see the output. You can just change the expression at `val = round(query+(query/0.971),2)` to use what you want. Here is the script: import sys, json query = float(sys.argv[1]) val = round(query+(query/0.971),2) data = {'items': [{ 'title': val, 'subtitle': str(query)+'+('+str(query)+'/0.971)', 'arg': val, 'text' : { 'copy' : val, 'largetype' : val }, }]} sys.stdout.write(json.dumps(data))
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