A way to select text in between quotes
Posted: 22 January 2010 07:53 PM   [ Ignore ]
Newbie
Rank
Total Posts:  2
Joined  2010-01-22

I have a macro that I use in TextMate all the time that highlights all the text between double quotes. It’s useful for replacing one url in a link tag with another, for example. I’m wondering if there’s a way to replicate this behavior within Espresso. I know there aren’t macros, but given all the useful selection stuff that’s showing up in TEA these days (Balance and Balance Inward), maybe there’s an easy way to do this.

Profile
 
 
Posted: 24 January 2010 03:17 AM   [ Ignore ]   [ # 1 ]
Moderator
RankRankRankRank
Total Posts:  984
Joined  2008-10-28

Welcome to the forums! There is indeed an easy way to do this using Spice. Spice is an Espresso sugar that makes it very easy to create custom actions coded in Javascript using a growing selection of utility classes (powered by Mootools). As you’ll see in the example code below, there’s no need to understand Objective-C.

First off, install Spice 1.0 beta 6:

http://onecrayon.com/downloads/Spice.zip

After installing Spice, navigate here in the Finder:

~/Library/Application Support/Espresso

Once there, create a folder called Support. Inside Support create two folders: TextActions and Scripts. Inside TextActions create an XML document called Spice.xml with the following contents:

<action-recipes>
    <
action id="com.onecrayon.BalanceZone" category="actions.text.generic">
        <class>
Spice</class>
        <
title>Balance Syntax Zone</title>
        
        <
setup>
            <
script>balance_zone</script>
            
<undo_name>Balance Syntax Zone</undo_name>
        </
setup>
    </
action>
</
action-recipes

This defines a new custom action for Espresso. Here’s more info about Spice action XML, including info on adding a keyboard shortcut for the action.

After the XML file is created, go into your Espresso Preferences, choose the Advanced tab, and in the TEA for Espresso prefs make sure that “Enable TEA custom user actions” is checked. After that, you’ll need to restart Espresso twice to get the action to show up in the Actions menu. This is due to limitations in how Espresso loads actions from XML.

Before the action will do anything, though, you need to create a new file in the Support/Scripts folder called balance_zone.js with the following contents:

// balance_zone
// Selects the text in the active syntax zone

// require() allows you easy modular access to Spice's helper classes
var textContext = require('text_action_context').textContext;
// We don't directly use the SyntaxZone class, but need to require it to load in some helpful Range methods for accessing zones
require('syntax_zone');

// exports.main is your primary function
// You could do without a function, but you couldn't return false to have Espresso beep
exports.main = function() {
    
// Voila
    
textContext.setSelection(textContext.getFirstSelection().getZone().range);
    return 
true;

You don’t have to restart Espresso after creating or modifying the Javascript file, because loading the Javascript is handled by Spice which will always execute the most recent saved version of the file.

This is a very basic action that may come in handy for things other than balancing quotation marks. What it does is use Espresso’s syntax API to find select the lowest syntax zone and select it. It’ll have varying degrees of success in languages depending on how they’ve defined their strings. For instance, in HTML the action will only select the string contents, but in Javascript it also selects the string delimiters.

You could make the action more quote-balancing specific by using text parsing logic instead of syntax zones. I leave it to you to experiment with such things if that tickles your fancy. You’ll find fairly extensive documentation for Spice’s utility classes in the Spice docs, and you can always let me know if there’s something missing, buggy, or confusing.

 Signature 

Ian Beck
MacRabbit Support

My stuff:
TEA (docs / source) .:. Spice.sugar .:. HTMLBundle.sugar .:. Quiet Light & Earthworm

Profile
 
 
Posted: 24 January 2010 08:24 PM   [ Ignore ]   [ # 2 ]
Newbie
Rank
Total Posts:  2
Joined  2010-01-22

Wow. This works great! Thanks.

I’m guessing more macro/scripting functionality like this will make its way into the app bundle over time, but cheers to you for adding these capabilities now. Now I just need to learn some more Javascript so I can fiddle with Spice.

And just a note if anyone tries to set this up and it doesn’t work at first. In the XML file that Ian pasted into his forum post the script tags get stripped out and replaced with [removed]. So you’ll have to replace [removed] with the appropriate “script” XML tag pair. And add the .js to the end of balance_zone. Which might all seem obvious enough, but I haphazardly copied and pasted into a new XML file without noticing what was missing.

If Espresso adopts a couple other features from TextMate – drag commands and the ability to assign multiple snippets/commands to the same keyboard shortcut – I’ll be able to drop it for good. Oh, and columnar selecting/editing would be nice too! Off to send these feature requests to Macrabbit.

Thanks again for the very helpful help.

Profile
 
 
Posted: 24 January 2010 10:18 PM   [ Ignore ]   [ # 3 ]
Moderator
RankRankRankRank
Total Posts:  984
Joined  2008-10-28

Darn it, I always forget about the forums and their weird rules for things you can and can’t put in code blocks. I’ve added the properly escaped script tags in. You also shouldn’t need the .js in the script element; it’s optional.

Glad it helped!

 Signature 

Ian Beck
MacRabbit Support

My stuff:
TEA (docs / source) .:. Spice.sugar .:. HTMLBundle.sugar .:. Quiet Light & Earthworm

Profile