0

Hi so I want to preset specific line of html code, and then paste it with key combo using Applescript.

Here is the html code:

<li> 
<div class="topic-title"> 
<h2>Topic title goes here</h2> 
</div> 
</li> 

I want Applescript to paste all of this html code into my text editor using a key combo. I tried a script but it gets confused by the quotes in the html code itself. Please advise. Thanks!

10
  • If all you need help with is the quotes within the HTML code, then simple escape the quotes with a backslash, e.g.: \"topic-title\". You could do something like, e.g. set the clipboard to "<li>" & linefeed & "<div class=\"topic-title\">" & linefeed & "<h2>Topic title goes here</h2>" & linefeed & "</div>" & linefeed & "</li>" then activate the target app, then use System Events to paste it, e.g.: tell application "System Events" to keystroke "v" using command down Commented Jan 31, 2020 at 2:03
  • 1
    If you're using something like TextWrangler or BBEdit you can use their paste command instead of System Events. Or you could just set up a text replacement where you'd type something and it would be replaced with the HTML code. System Preferences > Keyboard > Text Commented Jan 31, 2020 at 2:09
  • I searched on google but Bbedit has no way to store custom code and paste when required. Any suggestions regarding this BBEdit feature? Thanks!
    – sd100
    Commented Jan 31, 2020 at 2:12
  • I'd use text replacement as IMO its the easiest way to go. As an example, all the worlds in bold or italics in my other comments are text replacements. E.g. I type cmd and it get replaced with _command_ and shows as command. Almost everything I write more than a few times I've setup a text replacement for. You could use e.g. topt and have it replaced with the HTML code in your OP Commented Jan 31, 2020 at 2:24
  • Well I just found BBEdit feature Clippings will do this easily 👍
    – sd100
    Commented Jan 31, 2020 at 2:36

1 Answer 1

1

Create a 'service' in Automator.

Give it the following settings: Service receives 'no input' in 'any application' Check 'Output replaces selected text'

Add the following 'Actions' Get Specified Text - insert your clip here Copy to Clipboard Run AppleScript - replace the (* your script *) with 'the clipboard'

Click the 'Run' button to test the workflow. If there are no issues here, save the service.

To test whether the service works, put the cursor in a text doc of some sort (any place that you can paste multi-line text) and right-click… you should see the service's name in the menu. Click it and see what happens. Hopefully, it will paste your clipping into the text area which the cursor is in.

If it works well, go to the System Preferences > Keyboard > Shortcuts and click Services. Scroll through the resulting list on the right to find the name of your newly created service (should be in the Text section). Give it a keyboard shortcut (hopefully one that doesn't conflict with any other), and you're done.

So what does the service do? You specify the text for the clipping, it copies that to the clipboard. The applescript pastes the clipboard wherever you trigger the service.

As an aside, you don't mention what text editor you're using which might make it easier to devise a solution. BBedit was mentioned in the comments but the are other apps such as Coda that also offer some variation of a clippings feature. And as a further aside, I am unable to include a screenshot with my post.

2
  • Yes thanks so much. I am now using BBEdit which has clipping feature.
    – sd100
    Commented Mar 1, 2020 at 13:06
  • 1
    Good to hear. I didn't notice that comment when I wrote mine. BBEdit is a great app.
    – Mockman
    Commented Mar 1, 2020 at 15:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .