0

I want to implement a string interpolation in AppleScript, similar to the basic python implementation. For example in python:

print('Hello {}'.format('earth')) #> Hello World

In AppleScript, how do I implement the format handler?

to format(theString, {tokens})
    -- TODO
end format

log format("Hello {}", {"Apple"})  -- # Hello Apple
0

2 Answers 2

2

The python syntax and the AppleScript/ObjC syntax are not compatible.

If you are talking about only one argument there is a simple solution with help of NSString's stringWithFormat

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

to format(theString, tokens)
    return (current application's NSString's stringWithFormat_(theString, tokens)) as text
end format

log format("Hello %@", "Apple") -- # Hello Apple

For more arguments you have to use an if - else clause to convert the argument array to the ObjC va_list

to format(theString, tokens)
    set numberOfArguments to count tokens
    if numberOfArguments = 1 then
        return (current application's NSString's stringWithFormat_(theString, item 1 of tokens)) as text
    else if numberOfArguments = 2 then
        return (current application's NSString's stringWithFormat_(theString, item 1 of tokens, item 2 of tokens)) as text
    else if numberOfArguments = 3 then
        return (current application's NSString's stringWithFormat_(theString, item 1 of tokens, item 2 of tokens, item 3 of tokens)) as text
    else
        error "Invalid number of arguments"
    end if
end format

log format("Hello I'm %@ and I'm %@ years old", {"John", 25}) -- # Hello I'm John and I'm 25 years old
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your sample code. TBH, I'm not yet ready to try the ObjC, at least not yet, I'll give it try and see if I can build from there. My initial idea was to find and replace occurences of {} in the string, similar to when I implemented a find and replace. Just worried it won't be a trivial implementation.
I'm getting error "NSString doesn’t understand the “stringWithFormat_” message." do I need to install/configure anything to make it work?
You have to put always the three (at least the first two) use lines into your script when dealing with AppleScriptObjC. Parsing {} is not trivial in AppleScript.
1

I wrote a simple implementation using sed

to format(theString as text, theTokens as list)
    if class of theTokens is text then set theTokens to {theTokens}
    set builtString to theString
    repeat with nextToken in theTokens 
        set builtString to do shell script "echo '" & builtString & "' | sed 's/{}/" & nextToken & "/'"
    end repeat
    return builtString  
end format

I have tested it with only 2 scenarios, I'm sure there are more I haven't covered:

format("Hello {}", "baby") -- # Hello baby
"Hello {}, how are you {}", {"baby", "mom"}  -- # Hellow baby, how are you mom

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.