how to create your own AppleScript commands

Screen Shot 2015-12-25 at 09.53.30
It being Christmas an’ all, we thought we’d contribute something special. What’s the one thing every AppleScripter wants for Christmas? Why “the missing command” of course.

You can bug report Apple all you like to add some command like “remove whitespace from a string” or “get substring” without having to mess around with offsets and so on, but there’s nothing better than helping your self. And that’s what we’re going to do today: learn how to add our own AppleScript commands with a bit of help from AppleScript-ObjectiveC and the use of Scripting libraries.

Put on your party hat and get ready to unwrap your gift. We’re going to learn how to add commands so we can write something like the script shown in the screenshot above.

That requires defining 3 new commands for AppleScript:

substring following  — get the rest of a string after a designated substring

substring preceding — get all the string before a designated substring

remove whitespace — remove all the extraneous spaces and new line characters at the beginning and end of a string.

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 a Library’s script bundle.

The steps are nicely laid out in this complete 26-minute video from OSXAutomation:

https://macosxautomation.com/mavericks/libraries/mda/libraries-with-terminology.m4v

(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.

iii. You’re going to need a heads-up on Objective-C and Cocoa if you don’t already know your way around Apple’s main programming language (Swift? Never heard of ya!) and APIs. Best place to get started with that is Shane Stanley’s intro guides to AppleScript-Objective-C.

If you get stuck, post a comment below and we’ll try to walk you past the obstacles. Once you get the bug, you’ll be writing your own commands left, right and centre!

After you’ve watched the video, you can either write your own commands or come back and copy our sdef file and library script.

If tackling AppleScript-ObjectiveC isn’t something you want to take on right now, let us know in the comments what kind of command you really want to add to your script library and we’ll see if we can’t cook up the code for you.

Alternatively, you can browse through some ready-made script libraries full of extra goodies from this collection.

Merry Christmas all, we’ll see you in the New Year!

🙂

 

Here’s our sdef file:

<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE dictionary SYSTEM “file://localhost/System/Library/DTDs/sdef.dtd“>

<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>

<command name=”remove whitespace” code=”SQTXRMWS” description=””>

<direct-parameter type=”text” description=”Remove extraneous whitespace and new line characters from beginning and end of a substring”/>

<documentation>

<html>

<![CDATA[<p>set s to remove whitespace ”  hello world  “</p><p>–> “hello world”</p>]]>

</html>

</documentation>

</command>

</suite>

</dictionary>

 

And here’s our library script, which we called “ASTU” (short for “AppleScript Text Utilities”).

 

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

on remove whitespace aString

set the sourceString to current application's NSString's stringWithString:aString

set trimmedString to sourceString's stringByTrimmingCharactersInSet:(current application's NSCharacterSet's whitespaceAndNewlineCharacterSet())

return (trimmedString as Unicode text)

end remove whitespace

 

Advertisements

About philastokes

Freelance Writer, Developer and Technical Communicator. Explaining the unexplainable with images, video and text. Scripting anything imaginable in Applescript, Bash, C, Objective C, Cocoa, Python and Xcode.

Posted on December 25, 2015, in AppleScript and tagged , . Bookmark the permalink. Comments Off on how to create your own AppleScript commands.

Comments are closed.

%d bloggers like this: