DTPab

DTPにまつわるあれこれ

AppleScriptで悩まされるダイアログの戻り値resultの要素

調べたのでメモ。

公式ドキュメントのここにこう書いてありました。

Result
A record containing the button clicked and text entered, if any. For example:
{text returned:"Cupertino", button returned:"OK"}
If the dialog does not allow text input, there is no text returned item in the returned record.
If the user clicks the specified cancel button, the command signals a “user canceled” error. For an example of how to handle such errors, see try Statements.
If the display dialog command specifies a giving up after value, and the dialog is dismissed due to timing out before the user clicks a button, it returns a record indicating that no button was returned and the command gave up: {button returned:"", gave up:true}

3行目にある通り、戻り値は

{text returned:"Cupertino", button returned:"OK"}  

となるらしい。
JavaScript好きからすると、オブジェクトのKeyに半角スペースだと……!?
となること請け合いですが、そこはAppleScriptなので*1慣れるしかないですね。

要するに、例えば入力するダイアログの「入力されたテキスト」はこのようにゲットできる。

display dialog "テキストを入力:" default button 2 default answer "ここをキャンプ地とする"
set inputText to text returned of result

AppleScriptのこのresult、どーも慣れないんだよね。
でもこれでひとつスッキリしたぞい。

*1:AppleScriptではオブジェクトリテラルやハッシュではなく、この形のオブジェクトをレコード(record)と呼びます