スクリプトでボタンの配列を作成したいと思います。これには、サイズと位置の設定と、mouseUp ハンドラーの割り当てが含まれます。
mouseUp ハンドラーは
on mouseUp
go to card "aName"
end mouseUp
名前のリストは、テキスト変数 tCardNames にあります。各行にはカード名があります。
このスクリプトは問題なく動作するはずですが、基本的にすべてのボタンのスクリプトが同じであるため、ハンドラーのスクリプト部分を省略して、代わりに動作を割り当てることができます。これは、動作を使用する場合の良い例です。動作ボタンのスクリプトは次のようになります。
on mouseUp
go cd (the label of the target)
end mouseUp
そのボタンを作成し、「goCardBehavior」という名前を付けて非表示にし、元のハンドラーで、スクリプトを記述する部分の代わりに次の行を追加します。
set the behavior of it to the long ID of button "goCardBehavior"
ビヘイビアーを使用する利点の 1 つは、後でスクリプトを変更する必要がある場合に、1 か所で変更するだけで済むことです。
次のスクリプトは仕事をします
on createButtons
repeat with i = 1 to the number of lines of tCardNames
put line i of field "cardNames" into tName
createNamedButton i, tName
end repeat
end createButtons
on createNamedButton n, aName
create button
set the label of it to aName
put "on mouseUp" & return into s
put "go to cd " & quote & aName & quote& return after s
put "end mouseUp" after s
set the script of it to s
put (10 + 30 * (n -1)) into tDistanceFromTop
set the top of it to tDistanceFromTop
end createNamedButton
ここでは、少し異なるアプローチを示します。フォームごとに繰り返す方が、長いリストの場合は を使用して繰り返すよりも効率的ですが、この状況では、大きな違いはないでしょう。
on createButtons
repeat for each line tBtnName in tCardNames
createNamedButton tBtnName
end repeat
end createButtons
on createNamedButton pName
create button pName
set the script of btn pName to "on mouseUp" & cr & \
"go cd " & quote & pName & quote & cr & \
"end mouseUp"
put the number of btn pName into tNum
set the top of btn pName to (10 * 30 * (tNum - 1))
end createNamedButton