Learn JavaScript with me


#41

What kind of variable(s) would you want to share? If it’s just the text you could always set the clipboard to it as a temporary measure :slight_smile:


#42

For larger or more complex values (e.g. lists, dictionaries, and combinations of the two), I might:

  1. convert the JS value to JSON with the standard JSON.stringify method
  2. save the JSON from the draft
  3. read the draft with another script later, using JSON.parse() for the JSON -> JS Value conversion.

#43

I haven’t found a solution - just a workaround.

The thing is related to the date Action Group here: https://actions.getdrafts.com/g/1HD

I wrote this because sometimes I need the current date in different formats…
Like in the first task with the prepended date in the title.

I just wanted to make the actions useable in many scripts but I only found a scriptonly solution to queue an action after the script is done. So I cant „call“ the actions in a line of code and then go on with the script.

I need to make a two step action. In the first step I include the action with the date format I need (then I have the variable „todayString“ and I can use this in the following script action step


#44

A W E S O M E ! Signing up now!!!


#45

Disclaimer: I know nothing about js :grinning:

But…

Imm learning and very interested in playing with dates. So have found the below. Would it help streamlining your script for the dates?

var d1 = Date.Parse(“today”,“dd/mm/yyyy”).toString(“dd/MM/yyyy”);


#46

Me neither :grinning:

But yes I think it would :slight_smile:
I need to check this because the months are stored from zero to 11 and so on but maybe they can be shorter - in the end I think I won’t get any noticable performance improvements… I’ll check that and maybe update the action group if I have time

EDIT: it works so maybe ill update them later


#47

For what it’s worth, you may find it more useful in the long term to put the date scripts somewhere that allows expansion in more apps, rather than just have them available in Drafts.

I have built a set of about 25 TextExpander snippets that run JS (and hence run on Mac and iOS) based on Brett Terpstra’s model here http://brettterpstra.com/2015/06/01/textexpander-5-javascript-snippets/ . I’ve also added a selection of these to a Workflow with a menu which just calls a tetouch-xc:// URL to run the selected snippet.

This is all done with the last non-subscription version of TE, by the way, which works fine for me still.


#48

Drafts is a TextExpander enhanced app. Can I ask in what circumstances you are running from an Action rather than entering the snippet shortcut?


#49

I’d strongly recommend this action if you’re not using it already…does save time!!

http://actions.getdrafts.com/a/1DK

Which leads me to 2 questions:

  1. is there a web based playground, that I can run on my laptop, where I’d be able to test the code and see the results? (I understand much of this might bee to “Drafts-specific” but would be good to know

  2. Is there a “pythonist” like app for iOS for JS?


#50

The fastest way to Test Code from my mac for me is to put it in the drafts iCloud folder and import the script - sometimes directly into a script action step or just a draft and Test it with an „eval“ action for the current draft :slight_smile:


#51

I think perhaps JSBox is what you want.


#52

The comments suggest that it may not actually run code – just edit it.


#53

You can use a lot of things to run code - e.g. for JS a browser would do. The difficulty is that Drafts has some specialties (like the draft global) which are naturally not present in other apps.


#54

jsfiddle is a prolific web service for demoing web (including Javascript) configurations and tests. Just access in your browser of choice.


#55

I have a script I’d love to make. Before I try to do it, can anyone tell me if this is possible?

I design workshops and training sessions for businesses. This often means I have various chunks of content that I work through over the course of a half or full day.

So usually I would write:

Workshop Title

Activity one - 1h 20m

Description
Facilitation notes.

Activity two - 0h 40m

Description
Facilitation notes.

And so on…

Then I would add up the total time.

The problem us that if I have to add an activity or alter one I would then have to add up the total time again.

What I want to do is have a script go through and add up the times of each activity and then tell me the total. That way, if I have to change something I can easily see how much time I need or if I need to shorten some other activity.

I imagine I can write a script that would go and search my draft for anything that looks like (number)h or (number)m.

Then I think I need to tell that script to add up all the h numbers and m numbers. Then take the m total and divide by 60 adding that to the h number using the remainder as the m number.

I think my logic is sound on this.

So… can I do this? Is this much harder than I think?


#56

Array.reduce is helpful with summations.

If you:

  1. start with a seed value of 0, and
  2. for every line, match any hours and minutes with a regex pattern,
  3. adding (h * 60) + m to the accumulator

Array.reduce will return the total.

Click on the disclosure triangle for one version

( Make sure that you copy the whole text, down to })(); )

(() => {

    const
        // Regex pattern:
        // Maybe space, digit(s), 'h', space, digit(s), 'm'
        //        maybe space, end of line.
        rgxHM = /\s*(\d+)h\s+(\d+)m\s*$/,

        intMins = draft.content
        .split('\n')
        .reduce(

            // Step by step updates to the accumulator a,
            // based on each individual line x.
            (a, x) => {

                // Any regex match for this line:
                // Null (Boolean false) if no match, otherwise:
                //  m[0]         m[1]           m[2]
                // [whole match, h value match, m value match]
                const m = x.match(rgxHM);

                return a + (Boolean(m) ? (
                    (60 * parseInt(m[1], 10)) + parseInt(m[2], 10)
                ) : 0);
            },

            0 // Seed value for reduce acccumulator
        ),

        strDuration = intMins.toString() + ' mins = ' +
        Math.floor(intMins / 60).toString() + 'h ' +
        (intMins % 60) + 'm';

    return (
        alert(strDuration),
        console.log(strDuration),
        intMins
    );
})();

#57

I haven’t been. Was just suggesting to others that TE or an equivalent (if there is one that allows x-platform JS in snippets) might be a more useful place for date expansion in the long term.


#58

If the time is always formatted the same way, absolutely! You would need to use regular expressions to do this though. I’ll have a poke and see if I can come up with something :smiley:


#59

Took me a couple of hours (more than that to be honest) but it made me research and I learned a lot. Resolved! :smiley:

var p = Prompt.create();
var mydate = Date.parse("today","dd/mm/yyyy").toString("dd/MM/yyyy");

// Create Prompt

p.title = "Hello";
p.message = "Add info:";

p.addTextField("mytext","Your Text here:","");
p.addTextField("mytext2","Any more text?","");
p.addSelect("myselect", "Choose one",["#", "##", "###"],[],false);
p.addButton("Continue");
p.addButton("Cancel");

// Show Prompt
p.show();

// Store values from prompt
var myheader = p.fieldValues["mytext"];
var myheader2 = p.fieldValues["mytext2"];
var mytitle = p.fieldValues["myselect"];

// Action from 

if (p.buttonPressed == "Continue") {
    draft.content = (mytitle + " " + mydate + " - " + " " + myheader + myheader2 + "\n" + "\n");
}else if (p.buttonPressed == "Cancel") {
    draft.content = ("");
}
draft.update();

#60

Sorry, but the piece below indicated that’s exactly what you had been doing in the context of Drafts … which was the reason why I asked :upside_down_face: