Jump to content

Creating a formatted code block with the selected text


Recommended Posts

As a programmer, I often write emails, posts, white papers, and other such things with code embedded in a rich text document. I would love to share a workflow I am working on with the world but I'm having a hell of a time trying to get what should be simple to work.

 

Here's the vision:

  • Write some code in a document
  • Select the text
  • Fire a global hotkey which triggers an Alfred workflow
  • Copy the text
  • Use an AppleScript to format the text to a Monaco font, perhaps with a background color, etc.
  • Paste the text back (which, in theory should replace what's selected)

I am falling down miserably at this. Here's something I've tried but I'll admit my AppleScript-fu is a little novice.

 

try
    try
        set old to the clipboard
    end try
    tell application "System Events" to keystroke "c" using command down
    delay 0.03
    set input to Unicode text of (the clipboard as record)
    set txt to quoted form of ("<span style=\"font:14px 'Monaco'\">" & input & "</span>")
    do shell script "echo " & txt & " | textutil -format html -convert rtf -stdin -stdout | pbcopy -Prefer rtf"
    tell application "System Events" to keystroke "v" using command down
    delay 0.03
    set the clipboard to old
end try

 

And just in case you need to see it, this is how I have this particular applescript configured in Alfred's workflow:

 

Alfred%20Code%20Block%20Workflow.jpg

 

Any help would be greatly appreciated. Thanks folks! Loving version 2 so far!

Link to comment

As a programmer, I often write emails, posts, white papers, and other such things with code embedded in a rich text document. I would love to share a workflow I am working on with the world but I'm having a hell of a time trying to get what should be simple to work.

 

Here's the vision:

  • Write some code in a document
  • Select the text
  • Fire a global hotkey which triggers an Alfred workflow
  • Copy the text
  • Use an AppleScript to format the text to a Monaco font, perhaps with a background color, etc.
  • Paste the text back (which, in theory should replace what's selected)

I am falling down miserably at this. Here's something I've tried but I'll admit my AppleScript-fu is a little novice.

 

try
    try
        set old to the clipboard
    end try
    tell application "System Events" to keystroke "c" using command down
    delay 0.03
    set input to Unicode text of (the clipboard as record)
    set txt to quoted form of ("<span style=\"font:14px 'Monaco'\">" & input & "</span>")
    do shell script "echo " & txt & " | textutil -format html -convert rtf -stdin -stdout | pbcopy -Prefer rtf"
    tell application "System Events" to keystroke "v" using command down
    delay 0.03
    set the clipboard to old
end try

 

And just in case you need to see it, this is how I have this particular applescript configured in Alfred's workflow:

 

Alfred%20Code%20Block%20Workflow.jpg

 

Any help would be greatly appreciated. Thanks folks! Loving version 2 so far!

 

You can't echo your results, you need to save it in a text file. The shell is barfing on the html.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...