Jump to content

Text Processing Workflow without a Hotkey


Recommended Posts

I'd like to create a workflow that works in the following way:

 

- Select some text in an app in OSX
- Invoke Alfred
- Type: 'mb' and press Return
- Have Alfred take the selection and wrap it like **selection**
- Paste into the frontmost app, replacing the selection without adding a line break

 

Why?

 

I'd like to have these shortcuts available for quickly adding Markdown syntax to text, primarily:

 

Bold - 'mb' - selection -> **selection**
Italic - 'mi' - selection -> *selection*

 

I don't want to use a Hotkey for this but rather type "mb" return or "mi" return.

 

Any thoughts? Or any existing workflows I could take a look at that do something similar?

 

As a sidenote, now that Alfred is supporting JavaScript, would this be feasible with that new feature?

 

Thanks!

Link to comment

Thanks to Alfred's brand new method of triggering, yes, you can do this now. I have a text processing workflow which I'm going to have to update because of this. I didn't even realize it until you brought this up - so, kudos.

 

Here's the script snippet:

 

on alfred_script(q)

    tell application "System Events" to keystroke "c" using command down
    delay 0.3
    do shell script "pbpaste"
end alfred_script
 
This will send the current application a ⌘C to copy whatever text is selected. You need to pause at that point for the app to do the copy and put the text on the pasteboard. After that you can do what you want with the copied text. In the above example I just dump it onto stdout.
Link to comment

Thanks ctwise!

 

So to add bold Markdown around a text string:

 

- Create a new workflow

- You create a Inputs>Keyword with no arguments (in this case "mdb")

- Create an Actions>Run NSAppleScripts and paste the following:

on alfred_script(q)
    tell application "System Events" to keystroke "c" using command down
    delay 0.3
    set theString to the clipboard
    set the clipboard to "**" & theString & "**"
    tell application "System Events" to keystroke "v" using command down
end alfred_script

- Link the Input to the Action

- Select some text in any application, invoke Alfred, type 'mdb', and press Return

 

This will paste the "selection" as "**selection**"

 

This solution works pretty well and it is easy to add other Markdown text options for italics, code, etc.

 

However, I'm just wondering - is the only way to copy and paste to a Mac application to use System Events "keystroke"?

 

`pbpaste` doesn't seem to paste back into Mac apps - it only goes to standard output (Terminal).

 

I guess there's no disadvantage to using System Events, but it does seem "clunky" if you know what I mean.

Link to comment

is the only way to copy and paste to a Mac application to use System Events "keystroke"?

In a word, yes.

In a sentence, "no, but the other ways of doing it are either much more complicated or less reliable."

If you want to do it "properly", you might consider saving the previous contents of the clipboard and restoring them after you're done.

pbpaste and pbcopy are for you to get and set the clipboard contents on the command line (or in non-native scripting languages). They don't interact with applications in any way.

Edited by deanishe
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...