Jump to content
patgilmour

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!

Share this post


Link to post

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.

Share this post


Link to post

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.

Share this post


Link to post

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

Share this post


Link to post

*shrugs* Presumably a design choice. I guess they assume that anything you paste will be a complete todo, and adding the newline makes it easier to get straight to adding the next todo.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...