All this talk of libraries got me to playing around a bit. One thing that I wanted to do was create my own commands that are a bit more natural AppleScript-sounding like these ’substring preceeding’ and ' substring following’ commands to replace messing around with offsets and so on:
use script "ASTU"
set s to "Pleased to meet you, hope you guess my name. But what's troubling you is the nature of my game" set f to substring preceeding " hope" from s log f --or set f to substring following "But " from s
—> (*Pleased to meet you,*) Result: "what's troubling you is the nature of my game"
— —
Not only do these take all the grind out of doing all that “offset” stuff, they’re easy to remember and to use.
In order to define your own commands (note to the unwary: beware name clashes!)., you need to write an SDEF file and include it in the Library’s script bundle. It’s not as hard as it sounds but there are quite a few steps. Rather than trying to laboriously go through the steps here, there’s a complete 26 minute video that walks you through every step, from beginning to end:
(The video is interesting in it’s own right as it sounds like its entirely narrated by the one of the mac’s built in voices).
The library and the sdef file that I created are below, but you’ll need to watch the video to figure out how to put it all together.
A couple of ‘gotchas’ to watch out for:
i. the video doesn’t tell you that you need to include the 'use framework “Foundation”’ line (you don’t on 10.9, but on 10.11 I couldn’t get it to compile otherwise); ii. make sure the sdef file name, suite name and library script name all match or you might have trouble compiling.
Best
Phil
---------------------------------------------------------------------------------------------- Library Script bundle:
use framework "Foundation"
on substring preceeding aSubstring from aParentString set the sourceString to current application's NSString's stringWithString:aParentString set the searchString to current application's NSString's stringWithString:aSubstring set theRange to sourceString's rangeOfString:searchString set theRest to sourceString's substringToIndex:(theRange's location) return (theRest as Unicode text) end substring preceeding
on substring following aSubstring from aParentString set theLength to aSubstring's length set the sourceString to current application's NSString's stringWithString:aParentString set the searchString to current application's NSString's stringWithString:aSubstring set theRange to sourceString's rangeOfString:searchString set theRest to sourceString's substringFromIndex:((theRange's location) + (theLength)) return (theRest as Unicode text) end substring following
And here’s the SDEF file:
<?xml version="1.0" encoding="UTF-8"?> <dictionary> <suite name="ASTU" code="ASTU" description="Commands to edit text">
<command name="substring preceeding" code="SQTXSUBB" description=""> <direct-parameter type="text" description="Get the string up to a first occurrence of a substring"/> <parameter name="from" code="FRoM" type="text" description="..."/> <documentation> <html> <![CDATA[<p>set s to substring preceeding "world" from "hello world"</p> <p>--> "hello "</p]]> </html> </documentation> </command>
<command name="substring following" code="SQTXSUBA" description=""> <direct-parameter type="text" description="Get the rest of the string after the first occurence of a substring"/> <parameter name="from" code="FRoM" type="text" description="..."/> <documentation> <html> <![CDATA[<p>set s to substring following "hello " from "hello world"</p><p>--> "world"</p>]]> </html> </documentation> </command> </suite>
</dictionary>
|