Category Archives: TextEdit
run Terminal commands from any app
In this post I’m going to show you how you can select a piece of text in any app and have it run in Terminal simply by hitting a hotkey. The trick is especially useful for running commands you find on websites (like this one!) in a browser like Safari or Firefox.
This 20-second clip demonstrates running a command from a Firefox browser and another one from TextEdit, but you can also do it from an AppleScript editor window (and indeed any app that has selectable text), which can be useful for testing the formatting of your ‘do shell script’ commands and the like:
The first thing you’re going to need is to create an Automator workflow, add an AppleScript action and insert some code. Really? Nah, just kidding. I did it for you. 🙂 Just download, unzip and double-click the .workflow file to install the completed Service:
Download Run in Terminal.workflow.zip
Click through the various dialog boxes and choose ‘Install’ on the last one* (note for Snow Leopard users: the service will open directly in Automator; just do ‘command-shift-S’ to name it and save it).
All you need to do now is set the hotkey. Open > System Preferences.. > Keyboard | Shortcuts and click ‘Services’ in the sidebar. Scroll down the window till you see the ‘Run in Terminal’ command. Click on the far right to add a shortcut of your choice. The one I used in the video is ‘command-option-control-T’ (‘T’ for ‘Terminal’ helps me remember the shortcut).
To use the Service, just highlight any Terminal command by triple clicking it and pressing your hotkey. Try this one,
cd ~/Desktop; ls -alF
which lists all the visible and invisible files on your Desktop, as a test.
You can also get to the Service from both the contextual menu (right-click > Services) and the application menu bar at the top (e.g., Safari > Services).
As a bonus, try out your new Service on the Terminal command in this post, and now you’ll be able to run Terminal commands even from Quick Look previews in Finder!
Enjoy! 🙂
5 things you never knew about TextEdit
Although Apple’s oddball TextEdit.app has a variety of good formatting options, the chances are if you do any kind of word processing, you have one or more of the heavy duty apps like Pages, LibreOffice or Ms Word. If, on the other hand, you’re a coder or scripter who needs a plain text editor, you likely use Tincta, Sublime Editor 2, BBEdit, Coda or one of the many other full-featured editors that can do things like syntax colouring, snippet saving, script execution and so on that TextEdit can only dream of. Indeed, Apple have strangely forsaken giving their homegrown editor even a ‘line numbers’ option (though see tip 5 below), making it all but unusable for scripting.
Given its limitations, you might feel you haven’t been missing much by leaving TextEdit unloved and untouched in the Applications folder. However, here’s at least 5 reasons to think again.
1. Easiest way to create an audiobook.
I started off by calling TextEdit an ‘oddball’ app and here’s the first reason: name me another ostensibly “text editing” program that can make audio books? Dump any old text into a TextEdit window, and from the menu at the top choose ‘Edit > Speech > Start Speaking’. Plug in the headphones, sit back and relax! Works great with long online articles (but be sure to strip out any meta text and pictures first!). Also, don’t forget you can change the system voice in ‘System Preferences > Dictation & Speech’, and as I’ve pointed out elsewhere, foreign language learners can download optional voices and listen to foreign language text to improve their listening skills. For the parents among us, this one can also be great for the kids (both for them to listen to and create their own). 😉
To turn a TextEdit file into an audio book, select the entire text and from the menubar choose ‘TextEdit > Services > Add to iTunes as a Spoken Track’
2. Easy way to read, edit or save lengthy Terminal output
You can add
| open -f
to the end of any Terminal command, and (assuming TextEdit is your default editor) the output will be piped to a TextEdit window. This makes it easy to search, save or just scroll the output. This is particularly useful for reading and saving man
pages. However, be aware that for some bizarre reason, man
pages in particular tend to duplicate a number of characters in certain fields, which can affect readability. To prevent this here or with any other output you encounter that does the same, use
| col -b | open -f
after the Terminal command.
For example, if you enjoy hunting down defaults tricks, try this in Terminal. Type
defaults read | col -b | open -f
3. Easy way to get a file path
You can drag and drop any item from a Finder window into a TextEdit window to reveal that item’s path. This is a very handy trick if you need to quickly copy a file path to the clipboard. You can also drag urls from Safari’s address bar into a TextEdit window, or just drop them onto its Dock icon to get the same effect.
4. Easily share TextEdit content or files
Although TextEdit lacks the ‘Share’ icon in the menubar that was introduced to many apps in Mountain Lion, you can still access the ‘Share’ feature through the contextual (‘right/control click’) menu. Another oddity of TextEdit lurks here though: make sure the cursor in the TextEdit window is not on an empty line, or you won’t see the ‘Share’ option in the menu.
5. (not so) Easy way to get line numbers!
This one is for the coders among you. We all know TextEdit suffers from the lack of a ‘View line numbers’ option (come on, Apple – ridiculous!). There’s a few tips here. First, if you don’t know already, you can use the hotkey ‘command-L’ to go to any given line number. You can also find the total number of lines by using the ‘Find’ feature. Hit ‘control-F’ first, then click the spotlight in the filter bar. Choose ‘Insert Pattern’ and search for line breaks. You’ll see the total number of line breaks in the far right of the filter bar:
You can add pseudo (see below for why I call them ‘pseudo’) line numbers with a bit of Awk magic. Open Terminal.app and paste this into the window:
awk '{print FNR "\t" $0}'
Next drag and drop the file you want to add line numbers to into the Terminal window. Don’t worry, nothing you do here will change the original file. Apply your new found skill from Tip 2 above, typing a space and then a pipe onto the end of the command:
| open -f
so the whole thing might look something like this
awk '{print FNR "\t" $0}' ~/Desktop/myfile.txt | open -f
and hit ‘return’. Hey presto, you have a TextEdit file with “line numbers”.
Now, here’s the catch. I called these ‘pseudo’ line numbers because unlike real line numbers, the numbers are actually part of the text. That means if you select some portion of the text that spans line numbers, the numbers will be selected and copied too. In other words, you can’t properly copy and paste text, so be sure to keep your original version of the file for future editing purposes (*EDIT: see a workaround for this provided by Caroline in the Comments below).
So yes, TextEdit is an oddball program; it is like no other I know of for containing a bizarre mix of unrelated yet strangely powerful features coupled with the inexplicable absence of basic functionality that any other program would be handicapped without. Nonetheless, I hope this post has given you some ideas of how you can use TextEdit to better effect in your work and play. 🙂
Featured picture: Purple Glass Text Edit Icon by ~Drawder