,

Script with Snippets

Your snippets don’t have to come from another planet or be bitten by a radioactive spider to acquire super powers. If you know how to write AppleScript, JavaScript, or Unix shell scripts, you can harness the power of those technologies in your snippets.

TextExpander provides AppleScript snippets, JavaScript snippets, and Shell Script snippets. You just have to provide the code.

Script with AppleScript

AppleScript has been a part of the Macintosh experience since the days of System 7 (System 7.1.1 to be precise). AppleScript allows you to automate a number of tasks that would otherwise require the manipulation of menus, buttons, and other elements of the Mac’s graphical user interface.

If you know how to use AppleScript, you can leverage its power with TextExpander: put a script into a snippet and then invoke the script by typing the snippet’s abbreviation.

Here are the steps to follow for creating an AppleScript snippet:

  1. In TextExpander, create a new snippet.
  2. Click the pop-up menu above the snippet Content field and choose AppleScript. TextExpander may insert a sample script into the field; if so, just delete it.
  3. Type or paste your AppleScript code into the Content field.
  4. Assign the snippet an abbreviation and, optionally, a label.

Figure 40 depicts a short, but useful, AppleScript snippet. It assumes that iTunes is open. When the snippet’s abbreviation is typed, it tells iTunes to play the current track if it is paused or to pause if the current track is playing. If there is no current track queued up, the script does nothing.

**Figure 40:** An AppleScript snippet tells iTunes to play or pause.
Figure 40: An AppleScript snippet tells iTunes to play or pause.

It’s a trivial script, sure, and not really necessary if your keyboard has a play/pause button on it. On the other hand, if you’re in the middle of typing, say, an email while listening to music, and the phone rings, all you have to do is type the snippet’s abbreviation and iTunes pauses. Furthermore, since the script returns an empty string, TextExpander replaces the abbreviation you typed with it, which effectively erases the abbreviation from the email.

Keep the following points in mind when you use AppleScript snippets:

  • TextExpander does not debug: TextExpander can run AppleScripts, but it does not provide any debugging or formatting capabilities. Before you create an AppleScript snippet, you should develop and debug the script in another environment, such as the Script Editor application, located in /Applications/Utilities. Once the script is developed, it’s easy to copy and paste it into a snippet.
  • Labels are useful: TextExpander’s default behavior is to label a snippet with the content of the snippet. Many AppleScripts, however, begin with the same, or similar text (often a “tell” block), which makes them hard to identify solely by their opening lines. You’ll find your scripts much easier to identify and locate if you label them appropriately.
  • Return something of value in your scripts: Every AppleScript that runs returns some sort of value when it finishes, even if the value is nothing (technically, if you attempt to use the result of an AppleScript that returns nothing, that result is evaluated as the “missing value” constant). AppleScripts developed to be run in snippets should return values that TextExpander can use to replace the typed abbreviation unless you really want the snippet abbreviation to be replaced with nothing.

    In my example script shown above in Figure 40, an empty string is the thing of value I return because I explicitly want the replacement to be nothing. (I could have left the last line out to produce the same effect, of course.)

Script with the Shell

When OS X came to the Mac, developers discovered that they now had two scripting systems to exploit: AppleScript and the Unix shell. Or, rather shells—as any Unix developer knows, you have a number of choices for your shell-scripting activities.

If you know your way around the Unix command line, you don’t need to take a trip into Terminal to run your scripts. Instead, your script can live in a snippet, and you can run it by typing the snippet’s abbreviation in any text-editing environment.

To create a Unix shell script snippet, do the following:

  1. In TextExpander create a new snippet.
  2. Click the pop-up menu above the snippet Content field, and choose Shell Script.
  3. Type or paste your script into the Content field.
  4. Assign the snippet an abbreviation and a label.

Figure 41 shows a simple shell script snippet that invokes the Unix bash shell to generate a complete list of the files and directories in your user account’s Documents directory. You can type the snippet’s abbreviation to insert that directory listing in any text-editing environment.

**Figure 41:** Use this shell script snippet to list the contents of your `~/Documents` directory.
Figure 41: Use this shell script snippet to list the contents of your ~/Documents directory.

Some caveats to consider when crafting shell script snippets:

  • Debug your scripts first: Shell scripts can be very powerful, and with great power comes (say it with me) great responsibility. It’s very easy to wipe out your entire system with a badly written script, and you don’t have a command line available to issue a kill command to a runaway script. To put it bluntly, TextExpander is the wrong tool to use for experiments in shell scripting. You should use only those scripts for snippets that you can verify are safe, and you should develop them elsewhere.
  • No bang without the shebang: As Figure 41, above, demonstrates, all shell scripts used in snippets must start with a shebang (#!), followed by the full path to the interpreter that executes the script. No shebang, no execution. It’s that simple. As a courtesy, TextExpander includes the shebang line in a new shell script snippet, followed by an echo statement; you can delete the latter.
  • Labels are (almost) mandatory: To repeat my AppleScript advice, TextExpander’s default behavior is to label a snippet with the opening content of the snippet. Since shell scripts must begin with a shebang line (see the previous bullet), TextExpander’s default behavior of labeling snippets with their opening text makes shell scripts even harder to differentiate from one another than that behavior does for AppleScripts. Labels for shell script snippets are only theoretically optional: in practice, you should always label your shell script snippets with good, clear names.
  • Output is also (almost) mandatory: If the shell script you run in a snippet produces no printable output, TextExpander replaces the abbreviation you just typed with nothing. Unless that is what you want (and it usually isn’t), use shell scripts only in snippets that produce output so that TextExpander has something it can use to replace the typed abbreviation.

Script with JavaScript

TextExpander also supports snippets that consist of JavaScript scripts. Such scripts can come in two flavors: the traditional JavaScript that Web programmers have known for years, and Apple’s JavaScript for Automation, which adds features to the language to make it suitable for automating tasks in OS X.

Creating a JavaScript snippet is similar to creating snippets in the other scripting languages that TextExpander supports:

  1. In TextExpander, create a new snippet.
  2. Click the pop-up menu above the snippet Content field, and choose JavaScript.
  3. Type or paste your script into the Content field. Similar to the way TextExpander handles new shell scripts, in a new JavaScript snippet TextExpander inserts a couple of commented lines of JavaScript guidance as a courtesy; feel free to discard or ignore those.
  4. Assign the snippet an abbreviation and a label.

In Figure 42 you can see a relatively simple JavaScript snippet that calculates the sales tax for a given dollar amount.

**Figure 42:** This JavaScript snippet calculates sales tax.
Figure 42: This JavaScript snippet calculates sales tax.

The snippet makes use of fill-ins (see Make a Fill-in Snippet) to obtain both the price and tax rate from the user. It also uses the %filltop% macro to place the fill-ins at the top of the window, thus hiding the JavaScript code that would otherwise appear (Figure 43).

**Figure 43:** The fill-in window produced by the tax-calculating JavaScript snippet presents its fill-ins at the top.
Figure 43: The fill-in window produced by the tax-calculating JavaScript snippet presents its fill-ins at the top.

This sample script makes use of a property provided by the global TextExpander JavaScript context object, in this case its appendOutput() function, to accumulate the text that the snippet returns when it expands. The TextExpander JavaScript object’s properties, as well as other useful JavaScript tips, are described in Smile’s blog post TextExpander Adds JavaScript Support as well as in the TextExpander Help.

As with shell script and AppleScript snippets, the following caveats apply to JavaScript snippets:

  • Don’t try to debug scripts in TextExpander: Apple’s Script Editor is a safer and more useful tool for such development work.
  • Give your JavaScript snippets labels: Otherwise, you see the script’s first line as its label in your snippets list, and that is usually not very informative.

Combine Scripting Snippets

Like any other snippet, you can embed snippets that contain scripts, whether AppleScript, JavaScript, or shell scripts, in other snippets. Just use the %snippet:<snippetabbrev>% macro that you learned about in Insert the Clipboard or Another Snippet, replacing <snippetabbrev> with the abbreviation for the script snippet that you want to embed.

For example, you can create a snippet that contains a reminder about your monthly poker game that you regularly email your gambling cronies, and embed within it a shell script snippet that generates a calendar for the current month using the Unix cal command. When the snippet expands, your email message includes the calendar.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.144.56