How to define a 2-parameter handler in sdef with parens instead of curly braces?

74 views Asked by At

I want to define a handler that I can call like this:

log format("Hello {}", "World")  -- #> Hello World
log format("Hello {}, {}", {"World", "Again"})  -- #> Hello World, Again

I defined it in SDEF:

    <command name="format" code="RTSUFRMT" description="">
        <direct-parameter type="text" description="Python style string interpolation"/>
        <parameter name="with" code="WiTo" type="list or text" description="..."/>
        <documentation>
            <html>
                <![CDATA[<p>format("Hello {}", "world"R<p>]]>]
            </html>
        </documentation>
    </command>

And unfortunately my handler, while still functions correctly, automatically got rewritten like this:

on format {sourceText, tokens}
....
end format

How do I make it retain the syntax handler("Hello", "World"), instead of format {"Hello {}", "World"} ?

1

There are 1 answers

0
foo On

Simplest solution is to get rid of the SDEF and just write:

use _mylib: script "My Lib"

_mylib's format("Hello {}", "World")

SDEFs are there when you want to put a scripting addition-like syntax over AS handlers. You could define the SDEF command to be format [string] using [list of anything] (I’d suggest naming it format text to reduce risk of keyword collisions). But, honestly, SDEFs are often more work and trouble than they’re worth.


p.s. Making everything stupidly complex, with up to 7 different and incompatible ways of doing the same thing and never quite work right, is AppleScript’s baliwick, alas. It’s part of the reason AppleScript is dead in the water, and has been years. IMO, writing refined reusable general-purpose libraries is a huge waste of time right now. Best just do the simplest thing that works for the task at hand, and wait to see if Shortcuts finally lands for macOS this WWDC, and if AS has any future past that.