<p>When a script is saved as a bundle, it can use the <code>localized string</code> command to find the appropriate string, e.g. in <code>Contents/Resources/English.lproj/Localizable.strings</code>. If this is a format string, what is the best way to fill in the placeholders? In other words, what is the AppleScript equivalent of <code>+[NSString stringWithFormat:]</code>?</p>

<p>One idea I had was to use <code>do shell script</code> with <code>printf(1)</code>. Is there a better way?</p>

[{'answer_id': 66899, 'author': 'nlanza', 'author_id': 9373, 'author_profile': 'https://Stackoverflow.com/users/9373', 'pm_score': 0, 'selected': False, 'text': '<p>As ugly as it is, calling out to <code>printf(1)</code> is the common solution.</p>\n\n<p>A cleaner, though somewhat more complex, solution is to use AppleScript Studio, which allows you to call into Objective-C objects/classes from your AppleScript code with the <code>call method</code> syntax documented <a href="http://developer.apple.com/documentation/AppleScript/Conceptual/StudioBuildingApps/chapter04/chapter_4_section_2.html#//apple_ref/doc/uid/20001251-TPXREF175" rel="nofollow noreferrer">here</a>.</p>\n\n<p>With that, you\'d be able to use something like this:</p>\n\n<pre><code>call method "stringWithFormat:" of class "NSString" with parameters {formatString, arguments}\n</code></pre>\n\n<p>The downside of this, of course, is that you need to write an AppleScript Studio app instead of just writing a simple script. You do get a good bit more flexibility in general with Studio apps, though, so it\'s not all together a terrible route to go.</p>\n'}, {'answer_id': 41405599, 'author': 'Minh Nguyễn', 'author_id': 4585461, 'author_profile': 'https://Stackoverflow.com/users/4585461', 'pm_score': 3, 'selected': True, 'text': '<p><a href="https://developer.apple.com/library/content/releasenotes/AppleScript/RN-AppleScript/RN-10_10/RN-10_10.html" rel="nofollow noreferrer">Since OS X 10.10</a>, it’s been possible for any AppleScript script to use Objective-C. There are a few ways to call Objective-C methods from within AppleScript, as detailed in <a href="https://developer.apple.com/library/content/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/AppendixA-AppleScriptObjCQuickTranslationGuide.html" rel="nofollow noreferrer">this translation guide</a>. An Objective-C developer like me would gravitate toward this syntax, which interpolates the method\'s parameters with their values:</p>\n\n<pre><code>use framework "Foundation"\n\ntell the current application\'s NSWorkspace\'s sharedWorkspace to openFile:"/Users/me/Desktop/filter.png" withApplication:"Preview"\n</code></pre>\n\n<p>Result:</p>\n\n<pre><code>true\n</code></pre>\n\n<p><code>+[NSString stringWithFormat:]</code> is a tricky case. It takes a vararg list as its first parameter, so you need some way to force both the format string and its arguments into the same method parameter. The following results in an error, because AppleScript ends up passing a single NSArray into the parameter that expects, conceptually, a C array of NSStrings:</p>\n\n<pre><code>use framework "Foundation"\n\nthe current application\'s NSString\'s stringWithFormat:{"%lu documents", 8}\n</code></pre>\n\n<p>Result:</p>\n\n<pre><code>error "-[__NSArrayM length]: unrecognized selector sent to instance 0x7fd8d59f3bf0" number -10000\n</code></pre>\n\n<p>Instead, you have to use an alternative syntax that looks more like an AppleScript handler call than an Objective-C message. You also need to coerce the return value (an NSString object) into a <code>text</code>:</p>\n\n<pre><code>use framework "Foundation"\n\nthe current application\'s NSString\'s stringWithFormat_("%lu documents", 8) as text\n</code></pre>\n\n<p>Result:</p>\n\n<pre><code>"2087 documents"\n</code></pre>\n\n<p>The “with parameters” syntax that @nlanza mentions points to the fact that AppleScript is using something akin to <a href="https://developer.apple.com/reference/foundation/nsinvocation" rel="nofollow noreferrer">NSInvocation</a> under the hood. In Objective-C, NSInvocation allows you to send a message to an object, along with an array of parameter values, without necessarily matching each value to a particular parameter. (See <a href="http://theocacao.com/document.page/264" rel="nofollow noreferrer">this article</a> for some examples of using NSInvocation directly.)</p>\n'}]
['https://Stackoverflow.com/questions/64146', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6311/']