• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
creating your own commands
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

creating your own commands


  • Subject: creating your own commands
  • From: 2551 <email@hidden>
  • Date: Thu, 24 Dec 2015 19:14:35 +0700

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:

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. 




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"?>
<!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>
</suite>


</dictionary>



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: creating your own commands
      • From: Shane Stanley <email@hidden>
    • Re: creating your own commands
      • From: "Stockly, Ed" <email@hidden>
    • Re: creating your own commands
      • From: 2551 <email@hidden>
  • Prev by Date: Re: Script Starter Templates
  • Next by Date: Re: Script Starter Templates
  • Previous by thread: Re: AppleScript-Users Digest, Vol 12, Issue 643
  • Next by thread: Re: creating your own commands
  • Index(es):
    • Date
    • Thread