メインコンテンツまでスキップ

プロパティリスト

プロパティリストはリストと似ていますが、プロパティリストとリストはともに値のコレクションです。基本的な違いは、リストが単純な値のシーケンスであるのに対し、プロパティリストの各値はそのキーと呼ばれる名前またはラベルで識別されるという点です。プロパティリストの操作についての詳細は、Property List Operatorsを参照してください。

例:

put {x:5,y:12} into myPropList // このプロパティリストには2つのキー、xとy、およびそれぞれに対応する2つの値、5と12が含まれています。

もう1つの違いは、あまり明らかでないかもしれませんが、それがもっと重要であるかもしれません。それは、プロパティリストがSenseTalk オブジェクトの最も単純な形式であるということで、これはプロパティだけでなく振る舞いも持っているということを意味します。このセクションの残りの部分では、主にデータコンテナとしてのプロパティリストを説明します。オブジェクトについての完全な情報については、Objects and Messagesを参照してください。

ノート

リストとプロパティリストはパラメータとして渡すことができます。パラメータとしてリストを渡す方法については、Parameters and Resultsを参照してください。

プロパティリストの作成

プロパティリストを作成するには、各プロパティのキーと値を入力し、それを中括弧で囲みます。各プロパティのキーはその関連する値の前に来て、コロンで区切られ、キー/値のペアはカンマで区切られます。

ノート

Eggplant Functional 20.1以前は、リストとプロパティリストの両方がデフォルトで括弧()を使用していました。それに対して、今は角括弧[]または中括弧を使用します(それぞれに対して)。括弧はまだプロパティリストの有効な構文と見なされていますが、それらの使用は混乱を招く可能性があるため、推奨されていません。

例:

put {name:"Elizabeth", age:14} into daughter

空のプロパティリストを指定するには、中括弧内にコロン{:}を使用するか、フレーズempty objectまたはempty property listを使用します。

例:

put {:} into newPlist

プロパティリスト(それがオブジェクトであるため)は、テキストファイルで定義されたオブジェクトなど、他のオブジェクトによってhelpedすることができます。

例:

put {name:"Hank", age:47} helped by parent,actor into candidate

リストは行の続行(“\”)文字を使用せずに複数の行にまたがることができます:

set detective to {name: "Sherlock Holmes",
address: "221 B Baker Street, London",
remarks: "Enjoys playing violin, solving mysteries"}

プロパティリストは、中括弧 の代わりに括弧 () を使用することもできます。括弧を使用する場合、プロパティリスト全体が単一の行に表示される必要があります:

例:

put (beverage: "milk", food: "chocolate chips") into groceries

プロパティリストが通常のリストの最後のアイテムとして発生すると、プロパティリストの周囲から中括弧を省略することができます。このアプローチは、リスト/プロパティリストのハイブリッドのような構文をもたらしますが、名前付きプロパティは最後に来なければなりません。以下の2行は同等です:

例:

put [5, 12, {color:"Green", size:42}]
put [5, 12, color:"Green", size:42]

プロパティキー

すべてのプロパティ名(キー)はテキスト文字列です。プロパティ名が次の条件を満たす場合は、特別な処理をせずにキーを入力することができます。それは:(a) アルファベット文字またはアンダースコアで始まる場合、及び (b) アルファベット文字、数字、アンダースコアのみを含む場合です。スペースや特殊文字を含むキーを使用する場合、または数字で始まるキーを使用する場合は、キーを引用符で囲む必要があります。

例:

set oddPlist to {alpha:"ok", "87":"four score and seven", "$":50}

さらに柔軟性を持たせるために、括弧で囲まれた式を使用してキーを指定することもできます。この機能の最も一般的な使い方は、変数の値をキーとして単純に使用することですが、任意の式を使用することができます。

例:

if typeCode is 1 then set leaveType to "vacation" else set leaveType to "sick"
set leaveInfo to {employee:name, date:today, (leaveType):hoursTaken}

ここでは、3つのキー:"employee", "date", そして typeCode 変数の値に応じて "vacation" または "sick" を持つプロパティリストが作成されます。

重複キーと DuplicatePropertyKeyMode グローバルプロパティ

プロパティリスト内でキーが繰り返される場合、SenseTalkはduplicatePropertyKeyModeグローバルプロパティを使用して何が起こるかを制御します。デフォルトの設定では例外がスローされます。しかし、最初または最後の値を取るように、またはすべての値をリストに追加するように値を変更することができます。

このグローバルプロパティについての詳細は、duplicatePropertyKeyMode を参照してください。

プロパティリストの内容

リストと同様に、SenseTalkではプロパティリストに任意のタイプの値を含めることができます。これには、リストや他のプロパティリストも含まれます。

:::値を割り当てる際に、式を使用することができます:

例:

put {label:12592-A, 
area:pi*r^2,
dimensions:[9,8,42],
color:{top:"Red", sides:"Black"} } into currentPart

プロパティリストは順序が決まっていないため、プロパティリストを表示する際、例えば logput コマンドを使用すると、SenseTalkはキーをアルファベット順に表示します。

例:

set Features to {width:9 inches,ports:16,hues:4}
log Features // Displays '{hues:"4", ports:"16", width:"9 inches"}'

プロパティリスト内のプロパティへのアクセス

プロパティリスト内のプロパティにアクセスするには、いくつかの異なる方法を使用できます。ドット (.) やアポストロフィ-S ('s)、そして of 構文を使用すると、SenseTalkの特殊なメカニズムが起動され、これを使用して要求されたプロパティにアクセスするか、オブジェクトの関数を呼び出します。 property ... of 構文は常にプロパティにアクセスします(関数を呼び出すことはありません)。

単純なプロパティリストのプロパティにアクセスする場合、上記のそれぞれの構文の効果は同じです:それぞれが特定のプロパティにアクセスします。プロパティ名はケースに敏感ではありません。

例:

put {firstName:"Joseph", age:50} into joe
put property age of joe // 50
put the AGE of joe // 50
put joe's firstName // Joseph
put joe.firstName // Joseph

例:

log evaluateProduct(price:49.99,category:"toys",name:"lego set #12332") // 関数ハンドラー "evaluateProduct" にプロパティのリストを渡します

function evaluateProduct ProductInfo // パラメータ "ProductInfo" を持つ関数ハンドラ "evaluateProduct" を宣言します。このパラメータは渡されたプロパティリストを受け取ります。
if ProductInfo.category is "toys" or ProductInfo.category is "books"
if ProductInfo.price is greater than 20 then
return ProductInfo.Name & " は高すぎます。"
else
return ProductInfo.Name & " の価格は適切です。"
end if
else
throw "不明な製品カテゴリ。", "Category: " & ProductInfo.category
end if
end evaluateProduct

### 変数を使用してプロパティリストのキーを参照する

put {label:12592-A, area:pi*r^2, dimensions:[9,8,42], color:{top:"Red", sides:"Black"} } into currentPart

例:

put {firstname:"Joseph", age:50} into joe
put "Olivier" into joe.LastName
put joe —> {age:50, firstname:"Joseph", LastName:"Olivier"}

未定義のプロパティ

プロパティリストで設定されていないプロパティにアクセスすると、空が返されます。ただし、グローバルプロパティ the strictPropertiestrue に設定されている場合は例外です。the strictPropertiestrue である場合、事前に設定されていないプロパティにアクセスしようとすると例外がスローされます。 the strictProperties のデフォルト値は false です。

例:

put {name:"Whiskers"} into myCat
put the color of myCat // Displays nothing (empty) by default

リストとして複数のプロパティにアクセスする

キーのリストを指定することで、一度に複数のプロパティを値のリストとしてアクセスすることができます:

例:

set pitch to {a:440, b:493.88, c:523.25, d:587.33, e:659.26}
put pitch's [e,d,c,d,a] // [659.26,587.33,523.25,587.33,440]

要求された順序で値を取得します:

例:

put {color:"blue",tvshow:"X-Files",hobby:"skiing",movie:"Independence Day"} into favoriteList
put favoriteList's ["hobby", "color", "movie"] // Displays the list '[skiing,blue,Independence Day]'

プロパティ値の設定または変更

プロパティリストのすべてのプロパティはコンテナーなので、SenseTalkは、putaddsortなどのコンテナの内容を変更する任意のコマンドがプロパティの値を変更することを許可します:

例:

put {firstname:"Joseph", age:50} into joe
put "ine" after joe's firstName // 「Joseph」を「Josephine」に変更します
subtract 7 from the age of joe // 結果として、Joeの年齢の値は43になります
put joe --> {age:43, firstname:"Josephine"}

新しいプロパティの追加

プロパティリストに新しいプロパティを追加するには、単に存在しないプロパティに何かを格納します(値を既存のプロパティに格納すると、その現在の値が新しい値で置き換えられます)。

例:

put {firstname:"Joseph", age:50} into joe
put "Olivier" into joe.LastName
put "Tabby" into the name of joe's cat // Results in the property list '{age:"50", cat:{name:"Tabby"}, firstname:"Joseph", LastName:"Olivier"}'

SenseTalkは必要に応じてネストしたプロパティリストを自動的に作成します:

例:

put {firstname:"Joseph", age:50} into joe
put "Olivier" into joe.LastName // 'LastName' プロパティを追加します
put "Tabby" into the name of joe's cat // ネストされた 'cat' プロパティリストを作成し、その名前を「Tabby」に設定します
put joe --> {age:50, cat:{name:"Tabby"}, firstname:"Joseph", LastName:"Olivier"}

キーのリストを指定することで、一度に複数の新しいプロパティを追加できます:

例:

put {firstname:"Joseph", age:50} into joe
put "ine" after joe's firstName // Changes "Joseph" to "Josephine"
subtract 7 from the age of joe // Results in the value of joe's age being 43

Messages - メッセージの送信と処理に関連するコマンドと構造について説明します。

例:

put {firstname:"Joseph", age:50} into joe
add properties {lastname:"Jones", age:34} to joe
put {firstName:"Joseph", age:50} into joe

既存のプロパティを新しい値で上書きするには、単純に新しい値をプロパティに格納します。または、adding propertiesの代わりにreplacing properties演算子やreplace propertiesコマンドを使用します。

例:

put {firstname:"Joseph", age:50} into joe
replace properties {lastname:"Jones", age:34} in joe
put joe --> {age:34, firstname:"Joseph", lastname:"Jones"}

プロパティの削除

deleteコマンドを使用して、プロパティリストから既存のプロパティを削除します。

例:

put {school:"West Elementary",hobbies:"fishing"} into joe
delete joe.hobbies // Results in the property list '{school:"West Elementary"}'

removing properties演算子やremove propertiesコマンドを使用して、一度に複数のプロパティを削除します。

例:

put {barExamStatus:"passed", honors:"summa cum laude", LSAT:"170",internshipStatus:"completed"} into newHireRequirements
put {honors:"magna cum laude",barExamStatus:"passed"} into Executive
remove properties of Executive from newHireRequirements// Results in the property list '{internshipStatus:"completed", LSAT:"170"}'
put newHireRequirements --> {internshipStatus:"completed", LSAT:"170"}

put newHireRequirements removing property "internshipStatus" // Displays '{LSAT:"170"}'

ソースプロパティリストに存在しないプロパティを削除しても影響はありません。

retain propertiesコマンドは、明示的に保持されるようリクエストされていないプロパティをすべて削除します。

例:

put {dance:"fox trot",song: "Ain't That a Kick in the Head", partner:"Bob"} into DanceProgram
retain properties ["song","dance"] of DanceProgram // Results in property list '{dance:"fox trot", song:"Ain't That a Kick in the Head"}'

プロパティリストのプロパティを数える

number関数を使用して、プロパティリストに何個のプロパティがあるかを、キー、値、またはプロパティの数を問い合わせることで調べます。

例:

put the number of keys in {x:5,y:12} -- 2
put the number of values in phoneBook into phoneCount

number of occurrences関数は、特定の値やキーが何回存在するかを数えます:

例:

put the number of occurrences of 5 among the values in {x:5,y:12} -- 1

プロパティ名のリスト化:Keys関数

keys関数は、プロパティリストのプロパティの名前をアルファベット順のリストで提供します:

構文:
{the} keys of objectOrPropertyList
keys( objectOrPropertyList )

例:

put {barExamStatus:"passed", honors:"summa cum laude", LSAT:"170",internshipStatus:"completed"} into newHireRequirements
put the keys of newHireRequirements // Displays the list '[barExamStatus,honors,internshipStatus,LSAT]'

例:

set mike to {current:["Mike and the Mechanics","Genesis"],previous:"Red 7"}
repeat with each item of keys(mike)
put it & ": " & property (it) of mike
end repeat
// current: [Mike and the Mechanics,Genesis]
// previous: Red 7

例:

put {testA:"Windows",testB:"Linux"} into testSet
repeat with each item test of keys(testSet)
Log "Running"&&test
RunWithNewResults "LoginTestCase", property (test) of testSet
end repeat

プロパティ値のリスト化:KeyForValue関数とAllKeysForValue関数

keyForValue関数を使用して、プロパティリストの特定の値に対応するキーを返します。プロパティリストがその値を含まない場合、関数は空を返します。指定された値に等しいプロパティが複数ある場合、その値のキーのいずれかが返されます。

構文:
keyForValue( objectOrPropertyList , aValue )
objectOrPropertyList . keyForValue( aValue )

例:

set myPets to {cat:"Yellow", dog:"Black"}
put keyforvalue(myPets, "Black") -- dog
put myPets.keyForValue("Yellow") -- cat

set myPets to (cat:"Yellow", dog:"Black")

構文:
allKeysForValue( objectOrPropertyList , aValue )
objectOrPropertyList . allKeysForValue( aValue )

例:

set letterCounts to {A:7, B:2, C:3, D:3, E:7, F:3}
put allKeysForValue(letterCounts, 3) --> ["D","C","F"]
put letterCounts.allKeysForValue(7) --> ["E","A"]

log evaluateProduct(price:49.99,category:"toys",name:"lego set #12332") // Passes a property list into the function handler "evaluateProduct"

プロパティ値のリスト化:Values関数 {#the-values} values関数を使用して、プロパティリストのすべてのプロパティの値のリストを取得します。SenseTalkは、キーのアルファベット順に値をリストします

構文:
{the} values of objectOrPropertyList
values( objectOrPropertyList )

例:

set mike to {current:["Mike and the Mechanics","Genesis"],previous:"Red 7"}
put the values of mike // Displays the nested list '[[Mike and the Mechanics,Genesis],Red 7]'

例:

set mike to {current:["Mike and the Mechanics","Genesis"],previous:"Red 7"}
repeat with each item of mike's values
if it contains "Genesis" then log "Found it."
end repeat

values関数に1つ以上のプロパティ名やプロパティ名のリストを渡すと、リクエストされた順序でリクエストされたプロパティ値のみをリストします:

例:

put {color:"blue",tvshow:"X-Files",hobby:"skiing",movie:"Independence Day"} into favoriteList
put favoriteList's ["hobby", "color", "movie"] --> ["skiing","blue","Independence Day"]

プロパティリストのプロパティを順番に処理する

repeat with eachループを使用して、プロパティリストの値を1つずつ処理します。特殊なit変数を使用して、現在のキーを参照できます。

例:

set phoneBook to {Mark:"555-1234", John:"555-2345", Eli:"555-3456"}
repeat with each of the keys of phoneBook
set value to phoneBook.(it)
put key & ":" && value-- work with
end repeat

プロパティリストはパラメータとしても渡すことができます。詳細については、Parameters and Resultsを参照してください。

例:

set myEnvironments to {WindowsVM:("Chrome","Firefox","Internet Explorer"),LinuxVM:("Chrome","Firefox"),MacOSXVM:("Chrome","Safari","Firefox")}
repeat with each SUT of the keys of myEnvironments // Iterates through LinuxVM, MacOSXVM, and finally WindowsVM, following alphabetical order of the keys
Connect SUT
repeat with each Browser of BrowserList // Iterates through each item in the list that was pulled in as the property list value
RunWithNewResults "WebTest", Browser
end repeat
end repeat

キーを参照するためのカスタム変数名も使用できます。

例:

put "A single question can be more influential than a thousand statements." into quotation

// 引用文中の各文字の出現回数を数えます。
set characterTally to {:}

repeat with each character of quotation
if it is a letter then add 1 to characterTally.(uppercase of it)
end repeat

repeat with each [char,count] of characterTally
put !"[[char]] : [[count]]"
end repeat

プロパティリストにキーまたは値が存在するかを確認する

is amongオペレーターを使用して、プロパティリストが特定のキーまたは値を含んでいるかどうかを確認します。

例:

put "x" is among the keys of {x:5,y:12} // Displays 'true'

例:

put 12 is among the values of {x:5,y:12} // Displays 'true'

例:

put the first item of the folders of "C:\Users\Carrie\Desktop\" into myFolder // Stores the object (a property list) representing the first listed folder on the Desktop into a variable.
put "NSFileType" is among the keys of myFolder // Displays 'true'

特定の状況では、 containsis in オペレーターを使用します。これらのオペレーターの意味は、プロパティリストと一緒に使用する場合、オブジェクト自体で定義できます。あるいは、 the objectContainsItemDefinition グローバルプロパティを設定することで設定できます。しかし、プロパティリストで containsis in オペレーターを使用するデフォルトの動作は、プロパティリストのテキスト値の部分文字列をチェックすることです。

例:

set PersonalInfo to {asText:"Robert",age:31,hair:"brown",height:"5 feet 11 inches",degrees:["BS","MS"]}
put "hair" is among the keys of PersonalInfo // Returns 'true'
put PersonalInfo contains "hair" // Returns 'false' because the text value of the property list is "Robert"
set the objectContainsItemDefinition to KeyorValueEquals
put "hair" is in PersonalInfo // Returns 'true' because hair is a key of the property list
put "BS" is in PersonalInfo // Returns 'false' because "BS" is neither a key nor value in the property list
set the objectContainsItemDefinition to NestedValueContains
put PersonalInfo contains "BS" // Returns 'true' because "BS" is a nested value in the property list

プロパティリストをテキストに変換する

プロパティリストがテキストとしてアクセスされる(putコマンドによって表示されるなど)場合、SenseTalkはそれをテキスト表現に変換します。プロパティリストがテキストに変換されると、SenseTalkはリスト内の各値をテキスト表現に変換します。これがどのように行われるかの詳細については、Conversion of Valuesを参照してください。

プロパティリストに asText プロパティが含まれている場合、SenseTalkはそのプロパティの値をオブジェクトのテキスト表現として使用します。

>

set Color to {asText:"Pink","Candy Floss":424,"Warley Rose":874,"Alyssum Wonder":737}
log Color // Displays 'Pink'

プロパティリストが asText プロパティを含んでいないが、asTextFormat プロパティを含んでいる場合、SenseTalkはその値をマージフォーマットとして使用してテキスト表現を生成します。マージフォーマットの説明については、merge functionを参照してください。asTextasTextFormatプロパティなどの特別なプロパティについての情報は、Special Properties または Displaying Objects as Textを参照してください。

例:

set SoupduJour to {temperature:"cold",base:"tomato",asTextFormat:"Today's soup is a [[my temperature]] soup with a [[my base]] base."}
Log SoupduJour // Displays 'Today's soup is a cold soup with a tomato base.'

asText または asTextFormat プロパティが存在しない場合、SenseTalkはデフォルトで the propertyListFormat global property の値を使用してテキスト表現を作成します。詳細については Displaying Objects as Text および Object Properties を参照してください。このテキストは、デフォルトでは propertyListFormatprefixsuffix プロパティ(デフォルトでは開始と終了の中括弧)で囲まれます。プロパティは、keySeparator(デフォルトではコロン)でキーと値が区切られ、entrySeparator(デフォルトではカンマ)でキー/値のペアがそれぞれ区切られる形で列挙されます。値は引用符で囲まれます。空のプロパティリストのデフォルトのテキスト表現は emptyRepresentation プロパティ(デフォルトでは "{:}")で与えられます。 the propertyListFormat グローバルプロパティのこれらの値を設定することで、デフォルトの書式設定を変更することができます。これらは、あなたが好きなテキスト、または empty に設定することができます。

このデフォルトのテキスト表現は、propertyListFormatprefixおよびsuffixプロパティの値(デフォルトでは開き中括弧と閉じ中括弧)で囲まれています。プロパティは、keySeparator(デフォルトではコロン)でキーと値が区切られ、キー/値のペアはentrySeparator(デフォルトではカンマ)で区切られます。値は引用符で囲まれています。

空のプロパティリストのデフォルトのテキスト表現は、emptyRepresentationプロパティによって与えられます(デフォルトでは「{:}」です)。propertyListFormatのこれらの値を設定することで、デフォルトの書式設定を変更することができます。これらの値は、emptyを含む好きなテキストに設定することができます。

例:

set the propertyListFormat to {prefix:empty,suffix:empty,keySeparator:"#",entrySeparator:","}
set beverages to {water:"cold",tea:"hot"}
log beverages // Displays 'tea#hot,water#cold'

例:

set the propertyListFormat to {prefix:"The value of", keySeparator:"is", entryseparator: "and the value of", suffix:".",emptyRepresentation:"the value is empty."}
put {x:12,y:13} // Displays 'The value of x is "12" and the value of y is "13"'
ノート

スクリプトやヘルパーを含むプロパティリストを含むオブジェクトは、asText メッセージを処理して自身のテキスト表現を提供することで、このメカニズムを完全にオーバーライドすることができます。詳細については Objects and Messages を参照してください。

プロパティリストのすべてのキーと値のテキスト表現を取得するには、standardFormat 関数を使用して、ファイルに保存するのに適したプロパティリストの標準化された表現を生成します。このテキストは、そのテキストが value 関数によって評価されると、元のプロパティリストを再度生成する形式であります(Conversion of Valuesを参照)。

例:

put "C:\Users\Carrie\Desktop\Color.txt" into ColorsFile
put standardformat("Candy Floss":424,"Warley Rose":874,"Alyssum Wonder":737) into file ColorsFile
put value(file ColorsFile) into tempData
log tempData's "Warley Rose" // Displays '874'

テキストをプロパティリストに変換する

Working with Text で詳しく説明されている splitjoin コマンド、asText 関数、および対応する split by および joined by 演算子(Expressionsで説明されています)は、テキストをプロパティリストに明示的に変換する方法とその逆を提供します。

例:

put line 1 of file resourcePath("locations.txt") into Locations // Locations.txt stores a string, but not a property list, on line 1 that is formatted like 'Colorado:Fort Collins,Michigan:Lansing,Oregon:Bend' into variable Locations
split Locations using "," and ":" // Converts the string in Locations into a true property list based on the positions of commas and semicolons in the string
repeat with each of the keys of Locations // It is possible to iterate on the keys (Colorado, Michigan, Oregon) of Locations now that is has been converted to a property list
If it is "Oregon" then TypeText Locations.(it) // Types "Bend" into the SUT
end repeat

value function を使用して、standardFormat function に従う文字列をプロパティリストに戻すことができます。このアプローチは、プロパティリスト情報を外部データファイルに保存する際に便利です。

例:

repeat with each line Salary of file resourcePath("SalaryList.txt") // データファイルからの入力は常に文字列です。この場合、文字列は標準形式 '{Bob:@"$100,000.00"}' で保存されています
put value(Salary) into CurrentSalary // 文字列を標準形式に従って評価し、自動的にプロパティリストに変換します
put the keys of CurrentSalary // CurrentSalaryには真のプロパティリストが保存されているので、そのキーにアクセスすることができます
end repeat

asObject 関数(通常は as a property listas an object 演算子によって呼び出されます)は、標準形式の文字列をプロパティリストに変換するための別の方法で、最も単純なタイプのオブジェクトです。

例:

put line 1 of file resourcePath("SalaryList.txt") as a property list into Salary // データファイルは、標準形式でプロパティリスト '{Bob:@"$100,000.00"}' を保存しています
Log "The salary is "&Salary.bob&"." // Logs "The salary is $100,000.00."

オブジェクトとメッセージ

この時点までに取り上げた内容は、スクリプトを作成し、多くのタスクを達成するのに十分です。SenseTalkを完全に理解し、その力を最大限に引き出すためには、さらにいくつかの概念をマスターする必要があります:メッセージとハンドラ、オブジェクトとそのヘルパー。

Objects and Messages - オブジェクトという強力な概念を紹介し、それらを作成し、そのプロパティにアクセスし、それらにメッセージを送る方法を説明します。オブジェクトが他のオブジェクトを「支援」することを可能にするオブジェクトヘルパーも説明されています。