pptxman Posted April 2, 2020 Share Posted April 2, 2020 Hello, I want to execute a JavaScript that converts numbers to text (e.g. 500 → five hundred). I found great script on the web(https://skynet48.ru/javascript/summa-propisyu-na-javascript/) and now trying to create workflow but got stuck. The idea is to type numbers and get them in text. Please help. <script type = "text/javascript" language = "JavaScript"> // <![CDATA[ /* ---------------------------- Сумма прописью на JavaScript Author: Mad Max 2005 ---------------------------- */ var money; var price; var rub, kop; var litera = sotny = desatky = edinicy = minus = ""; var k = 0, i, j; N = ["", "один", "два", "три", "четыре", "пять", "шесть", "семь", "восемь", "девять", "", "одиннадцать", "двенадцать", "тринадцать", "четырнадцать", "пятнадцать", "шестнадцать", "семнадцать", "восемнадцать", "девятнадцать", "", "десять", "двадцать", "тридцать", "сорок", "пятьдесят", "шестьдесят", "семьдесят", "восемьдесят", "девяносто", "", "сто", "двести", "триста", "четыреста", "пятьсот", "шестьсот", "семьсот", "восемьсот", "девятьсот", "тысяч", "тысяча", "тысячи", "тысячи", "тысячи", "тысяч", "тысяч", "тысяч", "тысяч", "тысяч", "миллионов", "миллион", "миллиона", "миллиона", "миллиона", "миллионов", "миллионов", "миллионов", "миллионов", "миллионов", "миллиардов", "миллиард", "миллиарда", "миллиарда", "миллиарда", "миллиардов", "миллиардов", "миллиардов", "миллиардов", "миллиардов"]; var M = new Array(10); for (j = 0; j < 10; ++j) M[j] = new Array(N.length); for (i = 0; i < N.length; i++) for (j = 0; j < 10; j++) M[j][i] = N[k++] var R = new Array("рублей", "рубль", "рубля", "рубля", "рубля", "рублей", "рублей", "рублей", "рублей", "рублей"); var K = new Array("копеек", "копейка", "копейки", "копейки", "копейки", "копеек", "копеек", "копеек", "копеек", "копеек"); function num2str(money, target) { rub = "", kop = ""; money = money.replace(",", "."); if (isNaN(money)) { document.getElementById(target).innerHTML = "Не числовое значение"; return } if (money.substr(0, 1) == "-") { money = money.substr(1); minus = "минус " } else minus = ""; money = Math.round(money * 100) / 100 + ""; if (money.indexOf(".") != -1) { rub = money.substr(0, money.indexOf(".")); kop = money.substr(money.indexOf(".") + 1); if (kop.length == 1) kop += "0"; } else rub = money; if (rub.length > 12) { document.getElementById(target).innerHTML = "Слишком большое число"; return } ru = propis(price = rub, R); ko = propis(price = kop, K); ko != "" ? res = ru + " " + ko : res = ru; ru == "Ноль " + R[0] && ko != "" ? res = ko : 0; kop == 0 ? res += " ноль " + K[0] : 0; document.getElementById(target).innerHTML = (minus + res).substr(0, 1).toUpperCase() + (minus + res).substr(1); } function propis(price, D) { litera = ""; for (i = 0; i < price.length; i += 3) { sotny = desatky = edinicy = ""; if (n(i + 2, 2) > 10 && n(i + 2, 2) < 20) { edinicy = " " + M[n(i + 1, 1)][1] + " " + M[0][i / 3 + 3]; i == 0 ? edinicy += D[0] : 0; } else { edinicy = M[n(i + 1, 1)][0]; (edinicy == "один" && (i == 3 || D == K)) ? edinicy = "одна" : 0; (edinicy == "два" && (i == 3 || D == K)) ? edinicy = "две" : 0; i == 0 && edinicy != "" ? 0 : edinicy += " " + M[n(i + 1, 1)][i / 3 + 3]; edinicy == " " ? edinicy = "" : (edinicy == " " + M[n(i + 1, 1)][i / 3 + 3]) ? 0 : edinicy = " " + edinicy; i == 0 ? edinicy += " " + D[n(i + 1, 1)] : 0; (desatky = M[n(i + 2, 1)][2]) != "" ? desatky = " " + desatky : 0; } (sotny = M[n(i + 3, 1)][3]) != "" ? sotny = " " + sotny : 0; if (price.substr(price.length - i - 3, 3) == "000" && edinicy == " " + M[0][i / 3 + 3]) edinicy = ""; litera = sotny + desatky + edinicy + litera; } if (litera == " " + R[0]) return "ноль" + litera; else return litera.substr(1); } function n(start, len) { if (start > price.length) return 0; else return Number(price.substr(price.length - start, len)); } // ]]> </script> Link to comment
vitor Posted April 2, 2020 Share Posted April 2, 2020 Welcome @pptxman, You can’t just run any code anywhere. The code you posted is made to work inside a web browser. Technically you can run JavaScript outside the browser with default tools in macOS, but not all features are supported and that code is geared specifically towards writing to a web page. If you can find some in code in Python, Ruby, Bash, PHP, or Pearl, running it in Alfred is easy. Link to comment
deanishe Posted April 2, 2020 Share Posted April 2, 2020 (edited) Yeah, the code that does the conversion could be pulled out and used in Alfred (or more generally, anywhere), but that is a browser script. With JavaScript, it's important to understand the difference between the language and the browser. Scripts aren't run the same way outside a browser, and a lot of common functions and APIs are provided by the browser, not JavaScript itself. The script uses a CSS selector target and document.getElementById() for all its input and output, but document and elements and CSS selectors are a part of the browser environment, not JavaScript itself. All Alfred scripts must follow the UNIX model, and use ARGV and STDOUT/STDERR for input and output because that's what's available in the environment the scripts are run in. Edited April 2, 2020 by deanishe Link to comment
pptxman Posted April 3, 2020 Author Share Posted April 3, 2020 Thank you for helping So I managed to find script on Python, watched a video on how to run python script with Alfred but with no luck. No surprise since I have 0 programming skills. Could anybody please help me to create this workflow? Find my attempts here: https://1drv.ms/u/s!AkuQBe-rRIt9ocgDf74x8uamsWcFaQ?e=y4zelt Link to comment
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