Console.log() for those lazy buggers

Yes, I am a lazy typer who uses console.log() in my JS so much that I get fed up of typing it around what ever output value I need with it with for debugging.

I did have an app I wrote that I could just select the text and run a Action. But changes to the Action frameworks broke it and I never got around to fixing it ( would mean learning a whole new approach and rewriting the whole App.

So here is a tip for any others out there that uses console.log() as much as I do ( you should ) and is laaazzzy..

Create a New Automator.app Service.

  • Set it to Any Application

  • Check Output replaces selected text.
    This will replace the selected text with the final output of the service.

And will preserve your clipboard ( most people would use the clipboard to transit the input and output for this type of thing)

  • add a shell script action.

  • Set the Action to as Arguments

  • use this code.

    for f in "$@"
    do
    echo "console.log("$f")"

    done


The code takes the input surrounds it with console.log() and outputs the result which will replace the selected text.

  • Save and name the service.

Now open System Prefs.

* Go to **Security & Privac**y -> **Accessibility**
  • Click the + symbol to add to the Application list on the right.

  • Navigate to the Applications folder and Select Hype ( and/or Whisk )

(update - adding to Accessibility is only needed if we use keystrokes which I originally did but changed to the replace method above . It is not needed here)

Now still in System prefs.

  • Go to Keyboard -> Services
  • Scroll down the list on the right until you see the name of you new service.
  • Select it and give it a shortcut.

I suggest

crtl +alt +cmd +c

Thats it. now when in Hype you can select your text , hold down crtl +alt +cmd and hit c

And yourFooText gets made into a console.log("yourFooText”)

4 Likes

You wonderful genius.

1 Like

Cackle, cackle. :grinning:

Did a small update above ( we can take out the Accessibility step for this usage

Love it!! :heart_eyes:

1 Like

This is a good idea to add as a default snippet in whisk! :slight_smile:

It would also be great if it would properly escape quoting, and maybe have an alternate version that puts a log line right above the original line (say for log-style debugging usage).

1 Like

Hah, Forgot you put the post and pre post in Whisk snippets .

That would be, I had been thinking about a way for an action to allow you to select the text and drop a console.log() with it on the next line below. Keeping the original as is.

But for that kind of one I think people would have to wait for a native in app way, as it would be much easier to do with the native Whisk/app coding in delaminating the selected text and the full line of text.

With an Action putting a line above or below is easy

echo “$f”
echo “console.log(\”$f\”)”

but I have no way of doing that if we select only a part of the line.
.ie we need to account for the selected text and the text either side of it.

Screenshot 2021-10-05 at 21.52.29

If I do come up with something in the mean time I will post back. Ideally I want to avoid keystrokes but not sure I can

1 Like

Hah,

So I did figure out a way to drop the console.log() under the selected text line and not have it type out with keystrokes which would be annoying.

It does involves Kestrokes, keycodes and UI scripting so not sure how well it will hold up on other systems.

But it does show me that @jonathan can do this natively in Hype and Whisk (please :pray: )

Because we need Hype or whisk to Automate using Kestroke,keycodes and UI scripting.
We need to do this step.

Now open System Prefs.

  • Go to Security & Privacy -> Accessibility

  • Click the + symbol to add to the Application list on the right.

  • Navigate to the Applications folder and Select Hype ( and/or Whisk )

  • Then Change the code to (code in this txt file as the forum's encoding seems to introduce encoding errors when copied.

code_1.txt.zip (835 Bytes) )



  • uncheck -Output replaces selection

I have two version running.
The other one does not use quotes around the value.
So the line

set value of wctx to "console.log(" & "\"$f\"" & ")"

becomes

set value of wctx to "console.log(" & "$f" & ")"

Video show both ways.

2 Likes

Wow, awesome hack! I assume you are going the keystroke route to make sure that leading indentation is preserved?

Not 100% sure I follow?.

The original way I had to use the workflow was to:

  • copy the text/value
  • manually paste it where I wanted the console.log() to be
  • select the pasted text/value.
  • run the workflow on it, which would replace it with the new console.log..

We could not really run the workflow on the original text/value.
Otherwise we would get a console.log in the middle of the original line.

I think we both wanted to be able to have the console.log() automatically place itself above or below the original line.

So my main thinking is in the vain of not doing the copy and paste and to be able to select text/value that is within a line and run the workflow.
Like

Screenshot 2021-10-05 at 21.52.29

There is no native way for me to do that. i.e we do not know where we are in the doc and cannot jump the insertion point to a new position.

We need to use UI Scripting + keycode/keystrokes.

So the only answer is to use AppleScript (yes within shell) to jump the cursor to one end of the line and then drop down below it and then add the new line with the console.log.
( or above if you choose key code 123 and move the return keystroke to after the console.log has been place)

The problem with keystrokes when you ask it to keystroke a string, it will type it out in the UI one Character at a time. Just like you would if you typed it out your self.

So the final part to the puzzle was the UI scripting part.
The UI scripting part where I can gain some access to the document itself and change things more directly.

Ideally I would have like to just used the UI to jump the selected text range point to a new line. But even though I can change that range it just would not jump.

So now we can:

  • select our text/value
  • run the workflow on it and it will jump the insertion point to the new position and add the console.log there, preserving the original.

Hope that all makes sense..?

1 Like

Yup, makes sense. I wasn't even thinking about partial selection and see that solves the case. (I was originally thinking in my head that the "above" solution would just be putting a newline and repeating the original, but that is too naive and would require the work you've pretty much put in).

1 Like

I have been using TextExpander for such tasks until they went into a subscription model. Now the new normal, but back then I wasn't ready to commit. Then I was using other tools in between. I recently came across typedesk currently on sale. Pretty nice tool. Homebrew is ultimately the best, as one can customize it to fit every need (if one is committed enough).

My main tools when using Hype and JS are Hype itself I like to do it all in one place.
But the editor does have limitations. So when it get complex and I want to track down things like closing brackets, I switch to BBEdit. I do use Whisk also and Xcode. Mostly depends on what I am doing.

But the tip above will work for all of them apart from the last which is directed at Hype.app
Hmm thinking about that... should be simple enough to adjust the last one to determine which app it is in and work for that..

Yep.. simple

set activeApp to name of first application process whose frontmost is true

    	tell application process activeApp
    if activeApp is "Hype4"
    	set wctx to attribute "AXSelectedText" of text area 1 of scroll area 1 of splitter group 1 of group 1 of window 1
     	
    else if  activeApp is "Whisk"
    set wctx to attribute "AXSelectedText"  of text area 1 of scroll area 1 of window 1  

    end if
    set value of wctx to "console.log(" & "\"$f\"" & ")"

Hmmm, doesn´t work for me. I get the following error:

„zsh:22: parse error near `\n'“

@ktewes
Possibly an escape issue but,
Also I use bash.

Post me your file so. Can check it on my sys

Thanks for the file.

Seems the copy and paste from here is messing with the text encoding.
This does happen from time to time with code from the forum.

I have PM back to you the txt file with the code and updated the basic and first example above with the file and a screen shot of what it should be.

1 Like

Hei Mark, yep - that does the trick... though - I pasted your forum text into a text editor before (as I always do, when copying sth. from a website, for the 'purposes of hygiene' :grinning: ) Anyway - works like a charm now - thanks a lot again!!

1 Like

This one inspires me... :grinning:

var testArr = [1,2,3,4];

-> selecting 'test Arr' and running the script creates a 'for Loop':

for (i=0;i<testArr.length;i++){

			doThings();

		};

forLoop.txt.zip (1.2 KB)

1 Like

Nice.
I have that in a keyboard text substitution for the word forloop.
But may do it this way and with a hotkey

P.s.
I have 3 of the services running that work for either hype, whisk or bbedit. (Free mode)

One puts the log above, one below and the last below and value without quoting (for objects rather than strings )

@ktewes and anyone else Here you go

3scripts.zip (3.8 KB)

1 Like

Silly me.

I had started using shell script because it was a simple replace text at the time of me first looking at this where using shell was easier but then later on that changed and I started inserting AppleScript.

Just released now that we are doing it this way there is no need for the shell script and we can use a AppleScript action in the workflow.

The conversion is very simple.

  • Make sure the Workflow receives text is selected.
  • Replace the Shell Script with an Apple Script. And just use the Apple script. the $f can be change to the AppleScript var text_

set text_ to item 1 of input

quoted below.applescript.zip (1.3 KB)

1 Like

image

:grinning:

2 Likes