patgilmour Posted September 28, 2015 Posted September 28, 2015 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!
ctwise Posted September 28, 2015 Posted September 28, 2015 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. patgilmour 1
patgilmour Posted September 29, 2015 Author Posted September 29, 2015 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.
deanishe Posted September 29, 2015 Posted September 29, 2015 (edited) 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 September 29, 2015 by deanishe patgilmour 1
patgilmour Posted September 30, 2015 Author Posted September 30, 2015 Thanks for the answers! Any thoughts as to why the keystroke paste command would be adding a carriage return in Apple Notes? It doesn't happen in TextEdit or plain text editors.
deanishe Posted September 30, 2015 Posted September 30, 2015 *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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now