TextExpander logo TextExpander SmileOnMyMac logo
 

Help: AppleScript Snippets

TextExpander 2.0 introduces a new type of snippet -- AppleScript. You can choose "AppleScript" as the format type then enter an AppleScript such as this one:

-- add seconds to current date
set tomorrow to (current date) + (60 * 60 * 24)
-- format result
set result to (year of tomorrow) & "-" & (month of tomorrow as number) & "-" & (day of tomorrow) as string
-- return result
return result

When you enter the abbreviation associated with an AppleScript snippet, the AppleScript will execute and if the snippet has a text result, it will replace the abbreviation.

If you'd like to have access to the abbreviation used to trigger the AppleScript snippet, you'll need to write your script as a textexpander handler such as this:

on textexpander(days)
-- assume snippet begins with single character
set days to characters 2 through (count characters of days) of days as string
-- add seconds to current date
set tomorrow to (current date) + (60 * 60 * 24) * (days as number)
-- format result
set result to (year of tomorrow) & "-" & (month of tomorrow as number) & "-" & (day of tomorrow) as string
-- return result
return result
end textexpander

The script above allows you to set up multiple abbreviations with the same script to get different results. For example, you could use the abbreviations "d+1" and "d+2" to get tomorrow's date and the day after tomorrow. Also, you could use "d-1" for yesterday.

AppleScript also offers a "do shell script" command so that you can execute shell scripts or scripts in other scripting languages such as Ruby, PHP, and Perl. For more information on the "do shell script" command, please see Apple's excellent Technical Note 2065:

http://developer.apple.com/technotes/tn2002/tn2065.html

 
 
© 2006-2008 SmileOnMyMac, LLC. All rights reserved.
SmileOnMyMac and TextExpander are trademarks of SmileOnMyMac, LLC.