HashTag Macro KM 7.0

Here’s how to improve upon the AppleScript.

It now is type-selectable — and multiple items are supported.

-Chris

set hashTagList to items 1 thru -2 of {¬
  "AffinityDesigner", ¬
  "AffinityPhoto", ¬
  "Art", ¬
  "BohemianCodingSketch", ¬
  "Design", ¬
  "DigitalArt", ¬
  "draw", ¬
  "GraphicDesigner", ¬
  "illustration", ¬
  "KeyboardMaestro", ¬
  "Nice", ¬
  "Summer", ¬
  "TumultHype", ¬
  ""}

tell application (get path to frontmost application as text)
  set myChoice to choose from list hashTagList with title "HashTag Picker V1.0" with prompt "Select one or more HashTag, will be copied to System clipboard." default items {item 1 of hashTagList} with multiple selections allowed
end tell

if myChoice ≠ false then
  repeat with i in myChoice
    set contents of i to "#" & (contents of i)
  end repeat
end if

set AppleScript's text item delimiters to " "

return myChoice as text
1 Like

Wow This works great and Simpler than setting up palette. Though once the palette was configured works very good but allot of work compared to the script. Love them both and both have good benefits.
I really like both and will use both for now…

Again Thank you very much you guys are so kind to help out. :smile:

I was wondering is it possible to make this with check Box? for one hand operation.
I did research and found a lot of nothing…

Again Big Thank you.

Bill

Hey Bill,

That could probably be done with Pashua or ASObjC, but it starts getting complicated.

Keyboard Maestro can do checkboxes in its User-Input Action, but again you add complexity.

-Chris

1 Like

You can use a prompt for user input for checkboxes, but it’ll require manually adding new fields for each checkbox. Not ideal.

The palette’s the closest thing you’ll get without some substantial interface work. Not exactly the same as a checklist but it lets you click multiple items one-handed and insert each one.

1 Like

I’d love to see someone build this with the Custom HTML Prompt action. It would take more work to setup, although if the checkboxes were added via JavaScript, once done it would be a simple array to add entries to (like @ccstone’s AppleScript hashTagList).

I’d do it myself, but I’m a little busy right now!

1 Like

I've been enjoying messing with HTML prompts, so I thought I'd take a stab at this. As with all my JXA and JavaScript, it's pretty fugly, but it does what it needs to do - takes a comma-delimited list of hashtags and creates an HTML prompt with checkboxes for your tags, then pushes those back out to a variable.

Peter, one oddity I ran into is in the inserting the checkboxes variable by JavaScript. When I put them in there, they're automatically set to checked, even though that isn't the value in the variable. If I add a delay with setTimeout prior to inserting the checkboxes, that doesn't happen. Not sure if this is a bug or if it's how JS works, but it was strange.

It would also be fantastic if it were possible to just insert a variable into an HTML prompt without JavaScript. Just %Variable%Foo% like you can in other text fields.

HTML Prompt Checkboxes.kmmacros (5.6 KB)

3 Likes

Nothing fugly that I can see : - )

FWIW, mileage may vary, but I personally find it quicker (in terms of scripter time) to use .map(), which skips past having to set up and keep an eye on various variables and iterators etc.

e.g., perhaps:

function run() {
    var kme = Application('Keyboard Maestro Engine');

    return kme.variables['hashtags'].value()
        .split(',')
        .map(function (x) {
            var cbVal = x.trim();

            return '<label><input type="checkbox" name="checkbox" value="' +
                cbVal + '" data-ignore></input>' + cbVal +
                '</label>\n<br />';
        })
        .join('\n');
}
1 Like

I write my JXA like I write my AppleScript. Not up on all the JS functions. Need to look into map, I think.

1 Like

iNik, many thanks for sharing this macro. :+1:

It has really helped me understand how to dynamically update the HTML code from KM and JXA.

@peternlewis:
I agree with this request.
That would make it so easy to make dynamic HTML prompts.
Hopefully it would work with both text in the Action, and in a referenced HTML file.

Rob, thanks for sharing this great piece of JXA script.

I can use it directly, and as a model for dynamically building other parts of the HTML code for the HTML prompt Action.

Very cool macro!

I couldn't duplicate this so I don't really know what you mean. But Keyboard Maestro is going to try to go through your fields and configure them itself, but exactly when that happens compared to when your code is executing is hard to know.

It calls a function KMInit() before filling in the fields, so that may be the place you want to do your work. I have added for the next version calls to KMWillShowWindow() and KMDidShowWindow() which may be useful in the future.

1 Like

Peter, I'm not sure I fully understand your suggestion.

@iNik's current HTML code:

window.onload = function() {

	window.setTimeout(appendCheckboxes,0);
	
}

So, are you suggesting that it should be:

function KMInit() {
	appendCheckboxes();
}

Yeah… I can’t duplicate it either anymore.

KMInit and window.onload seem to run at about the same time. I was testing them both out to see if they changed the buggy behavior (it had no affect). Very odd.

And yes, JMichaelTX, that would be how you’d want to write up the JS.

Thanks iNik.

Just tested using the KMInit(), and it worked fine.
The checkboxes were NOT checked.

I'm working on some enhancements for the Custom HTML Prompt action, and I've thought about this but I don't think I'll do it. HTML is far too complex to safely parse, so I can't just run the text token expansion over it. At most I could perhaps special case %Variable%Whatever%, but even that is pretty ugly. What happens when your variable has quotes in it, or HTML, should it just be inserted verbatim? That could lead to some very weird behaviour.

It's easy enough to use boilerplate code in the KMInit to extract a variable (or any tokenised text) and put it as the value of an element, and then you know exactly what is going to happen.

Keep in mind too that Keyboard Maestro will fill in fields with the matching variable values when you create the Custom HTML Prompt window (as shown in the example in the Macro Library).

Peter, I'm not sure I understand your concern.

iNik's current process does exactly that: Inserts HTML code directly in the HTML code.

So, I don't see the functional difference (in terms of issues) between:

<div id="checkboxes">
   %Variable%cbArray%
</div>

and this:

<div id="checkboxes">
</div>

function appendCheckboxes() {
var cbTxt = window.KeyboardMaestro.GetVariable( 'cbArray' );
document.getElementById('checkboxes').innerHTML = cbTxt;
}

The biggest different is that the first approach of using the KM variable directly is simpler and much less prone to error.

I admit to not being an expert, but what happens if cbArray contains “</div>”? Do they actually behave the same in that case? I don’t think so - the structure of the document remains the same.

The main point remains the same though, I can’t safely parse for any text tokens - at most I could parse for variables, or I’d have to do something to make the text tokens stand out more since % characters are quite common in HTML.

It seems like to me in all cases it is incumbent on the user to provide valid HTML code. If he/she does not, then all bets are off.

I don't see insertion of HTML by JavaScript as any safer than by direct KM variable.

I don't think we were asking for anything other than just KM variables. If the user needs to use text tokens, he/she can do so in creating the KM var in the macro prior to the HTML prompt.

Use whatever syntax you like that would work. How about the shell script notation? That would make it easy to remember.

Or have a simple js function to insert a km variable or token within a script block. Save a lot of code and weirdness with a very explicit call to km.

E. G. </script>Kmtoken("%variable%whatever%") </script>

This would then evaluate the statement and insert it in-place.