Advertisement
  1. Computer Skills
  2. Textwell
Computers

Automating Text Conversions With Textwell

by
Difficulty:IntermediateLength:LongLanguages:

When I saw a tweet about a modeless text editor, I had to follow that up. Textwell is modeless in that there is nothing to save.

Textwell is a very handy program to have around. It's the ultimate scratch pad for the Mac or any iOS device. 

In this tutorial, I’ill show you how to make actions for Textwell to extend its functionality. The first action will be a simple title case converter. The second action will demonstrate the power of Textwell to perform web raking tasks.

Textwell the Modeless Editor
Textwell, the Modeless Editor

It is a simple, minimalist application that acts as a writing buffer, supplying the input for user created actions. You never save it, but it keeps a history—for up to a year—of changes to that buffer.

History and Actions Panels
History and Actions Panels

The history and actions panels can be open or closed depending on your needs. In this tutorial, I am going to show you how to create your own actions to automate your text editing.

Actions

An action in Textwell is some HTML and JavaScript. The HTML defines the graphical input to get more information from the user if needed, and the JavaScript performs the needed tasks.

Selecting New Action
Selecting New Action

To create a new action, right click on one of the actions and select New Action from the drop-down menu.

Edit Action Dialogue
Edit Action Dialogue

This will bring up a blank Edit Action dialogue. The top box is for the title of the action. The larger bottom box is for entering the HTML and JavaScript for the action. To the left of the title box is a selector for changing the icon to represent the type of editing the action will perform.

There are two radial buttons in the middle: Confirm and Translucent. If Confirm is active, the user has to confirm their choice before the action is performed. The Translucent setting tells Textwell if the script area contains only JavaScript code (Translucent selected), or an HTML web page with JavaScript embedded (Translucent unselected). 

When Translucent is selected and an action performed, a spinner is in the middle with everything in the text area dimmed out. When Translucent is not selected and an action is performed, whatever the HTML renders is in the display area.

The box to the right of the Translucent radial button is for setting a hot-key for the action. This is great for not having to leave the keyboard, but be careful. Too many hot-keys in your system can get in the way.

The button above the edit area allows you to set what you will edit: the code or the description of the code. With code selected, you can execute the code with the button at the bottom of the dialogue.

Title Case Action

To create the title case action, create a new action with the title Title Case. Turn on the Translucent radial and put the following code into the script area:

The first function is the TitleCase() function. This is a complete translation of the same routine written in Go in my other tutorial Start Developing With Go on the Mac.

After defining the function, the T.selectedText variable gets the selected text from Textwell if any. If there wasn’t a selection, the T.whole variable gives all of the text. The T variable is a JavaScript object that Textwell creates for all actions to use. The complete Textwell API describes every function that is available.

Once the string is determined, it gets parsed using the TitleCase() function. If the string was a selection, the results are inserted with the T('insert',{ text: str }); function. If the string was everything in the buffer, the text in Textwell gets replaced with the T('replace',{ text: str }); function call.

If you do not want to copy all of that text, do not worry. It is in the download file, or you can load it directly in to Textwell with this link: Title Case Textwell Action.

You can create a link to load any Textwell action by selecting the action and then the Generate Link to Share submenu entry of the main Action menu. This is a handy way to share your actions with others.

Web Raking Action

The title case action is a simple example of what Textwell can do. To show the power of Textwell, I’ll show you how to do Web Raking.

Web Raking is the pulling of information from a web page, deciphering what to remove, and just making use of part of the page. I live in Thailand and I always need to convert money from US dollars to Thai bahts. This action automates that process.

Create a new action, but this time set the Translucent radial to off. I call this one Currency Converter. Put the following code into the script area:

This is a full web page. It starts with the html tag, a header tag with all the styling for the body elements, and a body tag. The header tag also has a reference to load the jQuery JavaScript library. 

Since this action is about retrieving information from the web, it is safe to assume that a web connection is available. The body describes some labels for the data input fields and a div for the spinner.

The conversion takes place in the script tag at the end of the body tag area. In this script tag, there are three parts: getting the text either from the selection or from the whole document, the function for getting the exchange rate, and the binding of the function to the two button inputs in the body of the page.

The first section gets the text the same way as the Title Case action. The majority of this is figuring out the format of the text and processing it into the two variables: date and amount.

The items of interest are in the requestExchangeRate() function. This function starts with using jQuery to turn on the spinner. The T.request({}); Textwell function requests information from the specified website. This function accepts a JSON structure with three items: URL of the website, type of HTTP request to perform, and a callback function. The callback function gets the text of the website in the url address. When called, the callback function uses jQuery to get the text of the third table element of the #ratesTable element. That element contains the exchange rate needed.

If the result of the jQuery search is null, the rate variable contains Not a value. Otherwise, the rate variable gets multiplied by the amount and returned to Textwell using the T('replace',{}) or T('insert',{}), depending on whether the retrieved text was the whole text or a selection from Textwell

Remember, Textwell expects text values in the JSON structure.

With the code in place, you can now test this action.

Multiplier and Date Ready for Exchange Conversion
Multiplier and Date Ready for Exchange Conversion

To run the action, put in either: multiplier and date, just a date, just a multiplier, or nothing at all. The action will parse it out and display it to you.

Picking Conversion Type
Picking Conversion Type

If it parsed wrong, change it here or hit the escape key to get out of the action. If you like how it parsed the input, select which type of exchange you want: Thai Baht to US dollar, or US dollar to Thai Baht.

When you select one, the spinner will show at the bottom of the page. The action is now getting the web page for the conversion rate and extracting the needed exchange rate.

Results After Conversion
Results After Conversion

When finished, the action will replace the previous text with the exchange rate selected multiplied by the multiplier (or by one if a multiplier was not given).

If you want different currencies, it is simple to change the program for it. The exchange rates are coming from the website Exchange-Rates.org.

You can load this action into Textwell with this link: Currency Converter. It is also in the download file for this tutorial.

Textwell and Alfred

In order to make using Textwell even easier, I created a Textwell Workflow for Alfred. Once you've installed it in Alfred and assigned the hot-keys, you will be able to select text, press the hot-key to copy to Textwell, edit the text, and press the hot-key to put the text back into the original application. It helps my workflow every day.

Conclusion

Many things can be accomplished with Textwell’s actions. I have shown you how to use simple HTML and JavaScript to create useful actions. 

Now that you know how to make them, go ahead and create something interesting. Do not forget to share them with others as well.


Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.