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)
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.
This is a good idea to add as a default snippet in whisk!
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).
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.
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
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
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.
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).
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\"" & ")"
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' ) Anyway - works like a charm now - thanks a lot again!!
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_