5.5.4:Global Variables

グローバル変数

A global variable can be used in any handler,
anywhere in the application. Unlike a local variable, a global variable retains its value even after the handler that created it finishes executing. Unlike a script local variable, a global variable can be used by any handler in any object's script.

グローバル変数は、アプリケーション内のどの場所にあるどんなハンドラ内でも使うことができます。ローカル変数とは違い、グローバル変数は、変数の値をハンドラが実行された後も保持し続けます。また、スクリプト・ローカル変数とも違い、グローバル変数はどのスクリプト内のどんなハンドラにおいても使うことができます。

The same global variable can be used by any stack during a session. You can declare a global variable in one stack, and use it in others.

セッション内であれば同一のグローバル変数をどのスタックからも使うことができます。あるスタック内で宣言したグローバル変数は、他のスタック内でも使うことができるのです。

To create a global variable, you must declare it using the
global command:

グローバル変数を使うには、globalコマンドで宣言します:


  global someGlobalSetting

Important: You must also use the global command at the start of a handler to make an existing global variable available to a handler. While a global variable can be used by any handler, you need must do this in any handler you are going to use it in. If you don't declare a global variable before using it, the handler will not take the global variable's existence into account, and will simply create a local variable with the same name.

重要: globalコマンドは、既に存在するグローバル変数をハンドラに適用する為に、ハンドラの先頭で使う必要があります。グローバル変数はどのハンドラででも使用可能ですが、グローバル変数を使おうとするハンドラの全てで宣言をする必要があります。グローバル変数を宣言せずに使おうとすると、ハンドラはグローバル変数の存在を考慮せず、単純にその名前のローカル変数を作ってしまいます。

You can use the
global command either inside a handler, or outside any handler at the top of a script (like a script local). If you use the command in a handler, the global variable can be used by any statement in that handler. If you use the command in a handler but outside any script, the global variable is available to every handler in that script.

global
コマンドはハンドラ内で使うことも、全てのハンドラの外側でスクリプトの先頭で(スクリプト・ローカル変数の様に)使うことができます。ハンドラ内でこのコマンドを使うと、そのハンドラ内のどのステートメントでもそのグローバル変数を使うことができます。あるハンドラ内でしかも全てのスクリプトの外側で、そのコマンドを使うと、グローバル変数はそのスクリプト内の全てのハンドラで使うことができるようになります。

The following example shows the use of a global variable in a button script. In this example, the variable is declared outside any handler, so the individual handlers don't need to declare it again:

次の例では、ボタン・スクリプト内でのグローバル変数の使い方を示しています。この例では、変数は全てのハンドラの外側で宣言されており、その為ハンドラ毎にグローバル変数を宣言し直す必要がないわけです。

  global myGlobal -- declares this global for the whole script(スクリプト全体の為にこのグローバルを宣言します)

  on mouseDown -- can use "myGlobal"(myGlobalが使用可能です)
    put 1 into myGlobal
  end mouseDown

  on mouseUp -- can use "myGlobal"(myGlobalが使用可能)
    add 2 to myGlobal
    answer myGlobal -- displays "3"(3を表示します)
  end mouseUp

To use the same global variable in a handler where the variable isn't declared in the script, you must place the global declaration in the handler:

同一のグローバル変数を、その変数が宣言されていないスクリプト内のハンドラで使うには、ハンドラ内にグローバル変数の宣言を置かなければなりません:

  on mouseUp -- in another button's script(他のボタンのスクリプト内で)
    global myGlobal
    add 5 to myGlobal
    answer myGlobal
  end mouseUp

If you click the first button, then the secoond, the second button displays the number 8.

最初のボタンをクリックし、次に二番めのボタンをクリックします。二番めのボタンは数字8を表示します。

As with script local variables, we recommend you place all
global declarations in scripts at the top of the script, making the declarations easy for you to find later.

スクリプト・ローカル変数同様、全てのグローバル宣言は常にスクリプトの最上部で宣言するようにしてください。そうすれば、その宣言をあとで簡単に確認することができます。

Tip: You can get a list of existing global variables with the globalNames function. You can also choose Development > Variable Watcher to see a list of global variables and change their values.

秘訣: globalNamesファンクションを使えば、現在使用可能なグローバル変数の一覧を売ることができます。また、Development > Variable Watcherを選ぶと、グローバル変数の一覧を確認でき、またその値を変更することができます。

Global variables are automatically deleted when you quit the application. You can also use the
delete variable command to delete a global variable.

グローバル変数は、アプリケーションを終了すると自動的に消去されます。また、delete variableコマンドを使えばグローバル変数を削除することができます。