Jump to content
pptxman

How to run a JavaScript?

Recommended Posts

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>

 

Share this post


Link to post

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.

Share this post


Link to post
Posted (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 by deanishe

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...