rb-scrpt 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGES +497 -0
  3. data/README.md +8 -0
  4. data/Rakefile +32 -0
  5. data/doc/aem-manual/01_introduction.html +60 -0
  6. data/doc/aem-manual/02_apioverview.html +107 -0
  7. data/doc/aem-manual/03_packingandunpackingdata.html +135 -0
  8. data/doc/aem-manual/04_references.html +409 -0
  9. data/doc/aem-manual/05_targetingapplications.html +164 -0
  10. data/doc/aem-manual/06_buildingandsendingevents.html +229 -0
  11. data/doc/aem-manual/07_findapp.html +63 -0
  12. data/doc/aem-manual/08_examples.html +94 -0
  13. data/doc/aem-manual/aemreferenceinheritance.gif +0 -0
  14. data/doc/aem-manual/index.html +56 -0
  15. data/doc/appscript-manual/01_introduction.html +94 -0
  16. data/doc/appscript-manual/02_aboutappscripting.html +247 -0
  17. data/doc/appscript-manual/03_quicktutorial.html +167 -0
  18. data/doc/appscript-manual/04_gettinghelp.html +188 -0
  19. data/doc/appscript-manual/05_keywordconversion.html +106 -0
  20. data/doc/appscript-manual/06_classesandenums.html +192 -0
  21. data/doc/appscript-manual/07_applicationobjects.html +211 -0
  22. data/doc/appscript-manual/08_realvsgenericreferences.html +96 -0
  23. data/doc/appscript-manual/09_referenceforms.html +241 -0
  24. data/doc/appscript-manual/10_referenceexamples.html +154 -0
  25. data/doc/appscript-manual/11_applicationcommands.html +245 -0
  26. data/doc/appscript-manual/12_commandexamples.html +138 -0
  27. data/doc/appscript-manual/13_performanceissues.html +142 -0
  28. data/doc/appscript-manual/14_notes.html +80 -0
  29. data/doc/appscript-manual/application_architecture.gif +0 -0
  30. data/doc/appscript-manual/application_architecture2.gif +0 -0
  31. data/doc/appscript-manual/finder_to_textedit_event.gif +0 -0
  32. data/doc/appscript-manual/index.html +62 -0
  33. data/doc/appscript-manual/relationships_example.gif +0 -0
  34. data/doc/appscript-manual/ruby_to_itunes_event.gif +0 -0
  35. data/doc/full.css +106 -0
  36. data/doc/index.html +45 -0
  37. data/doc/mactypes-manual/01_introduction.html +54 -0
  38. data/doc/mactypes-manual/02_aliasclass.html +124 -0
  39. data/doc/mactypes-manual/03_fileurlclass.html +126 -0
  40. data/doc/mactypes-manual/04_unitsclass.html +100 -0
  41. data/doc/mactypes-manual/index.html +53 -0
  42. data/doc/osax-manual/01_introduction.html +67 -0
  43. data/doc/osax-manual/02_interface.html +147 -0
  44. data/doc/osax-manual/03_examples.html +73 -0
  45. data/doc/osax-manual/04_notes.html +61 -0
  46. data/doc/osax-manual/index.html +53 -0
  47. data/doc/rb-appscript-logo.png +0 -0
  48. data/extconf.rb +63 -0
  49. data/rb-scrpt.gemspec +16 -0
  50. data/sample/AB_export_vcard.rb +31 -0
  51. data/sample/AB_list_people_with_emails.rb +13 -0
  52. data/sample/Add_iCal_event.rb +21 -0
  53. data/sample/Create_daily_iCal_todos.rb +75 -0
  54. data/sample/Export_Address_Book_phone_numbers.rb +59 -0
  55. data/sample/Hello_world.rb +21 -0
  56. data/sample/List_iTunes_playlist_names.rb +11 -0
  57. data/sample/Make_Mail_message.rb +33 -0
  58. data/sample/Open_file_in_TextEdit.rb +13 -0
  59. data/sample/Organize_Mail_messages.rb +61 -0
  60. data/sample/Print_folder_tree.rb +16 -0
  61. data/sample/Select_all_HTML_files.rb +14 -0
  62. data/sample/Set_iChat_status.rb +24 -0
  63. data/sample/Simple_Finder_GUI_Scripting.rb +18 -0
  64. data/sample/Stagger_Finder_windows.rb +25 -0
  65. data/sample/TextEdit_demo.rb +130 -0
  66. data/sample/iTunes_top40_to_html.rb +69 -0
  67. data/src/SendThreadSafe.c +380 -0
  68. data/src/SendThreadSafe.h +139 -0
  69. data/src/lib/_aem/aemreference.rb +1021 -0
  70. data/src/lib/_aem/codecs.rb +661 -0
  71. data/src/lib/_aem/connect.rb +205 -0
  72. data/src/lib/_aem/encodingsupport.rb +74 -0
  73. data/src/lib/_aem/findapp.rb +85 -0
  74. data/src/lib/_aem/mactypes.rb +250 -0
  75. data/src/lib/_aem/send.rb +279 -0
  76. data/src/lib/_aem/typewrappers.rb +59 -0
  77. data/src/lib/_appscript/defaultterminology.rb +277 -0
  78. data/src/lib/_appscript/referencerenderer.rb +242 -0
  79. data/src/lib/_appscript/reservedkeywords.rb +116 -0
  80. data/src/lib/_appscript/safeobject.rb +250 -0
  81. data/src/lib/_appscript/terminology.rb +470 -0
  82. data/src/lib/aem.rb +253 -0
  83. data/src/lib/kae.rb +1489 -0
  84. data/src/lib/osax.rb +659 -0
  85. data/src/lib/rb-scrpt.rb +1073 -0
  86. data/src/lib/version.rb +3 -0
  87. data/src/rbae.c +979 -0
  88. data/test/README +3 -0
  89. data/test/test_aemreference.rb +115 -0
  90. data/test/test_appscriptcommands.rb +149 -0
  91. data/test/test_appscriptreference.rb +103 -0
  92. data/test/test_codecs.rb +181 -0
  93. data/test/test_findapp.rb +23 -0
  94. data/test/test_mactypes.rb +77 -0
  95. data/test/test_osax.rb +52 -0
  96. metadata +146 -0
@@ -0,0 +1,211 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3
+ <head>
4
+
5
+ <title>rb-appscript manual | 7. Application Objects</title>
6
+
7
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
8
+ <style type="text/css" media="all"><!--@import url(../full.css);--></style>
9
+
10
+ </head>
11
+ <body>
12
+
13
+ <h1><img src="../rb-appscript-logo.png" alt="rb-appscript" title="rb-appscript" /></h1>
14
+
15
+ <!-- top navigation -->
16
+ <div class="navbar">
17
+ <a href="06_classesandenums.html">Previous</a> &bull;
18
+ <a href="index.html">Up</a> &bull;
19
+ <a href="08_realvsgenericreferences.html">Next</a>
20
+
21
+ <span>
22
+ <strong><a href="../appscript-manual/index.html">appscript</a></strong> /
23
+ <a href="../mactypes-manual/index.html">mactypes</a> /
24
+ <a href="../osax-manual/index.html">osax</a> /
25
+ <a href="../aem-manual/index.html">aem</a>
26
+ </span>
27
+ </div>
28
+
29
+ <!-- content -->
30
+ <div id="content">
31
+
32
+ <h2>7. Application Objects</h2>
33
+
34
+ <h3>Creating application objects</h3>
35
+
36
+
37
+ <p>Before you can communicate with a scriptable application you must create an <code>Application</code> object, for example:</p>
38
+
39
+ <pre><code>textedit = app.by_id('com.apple.textedit')</code></pre>
40
+
41
+
42
+ <p>To target an application you must call one of the <code>app</code> object's <code>by_*</code> methods with the appropriate argument:</p>
43
+
44
+ <dl>
45
+ <dt><code>app.by_name(<var>name</var>)</code> / <code>app(<var>name</var>)</code></dt>
46
+ <dd>The application's filename or POSIX path, e.g. <code>'TextEdit'</code>, <code>'TextEdit.app'</code>, <code>'/Applications/TextEdit.app'</code>. Where a filename is provided, appscript uses LaunchServices to locate the application. An <code>.app</code> suffix is optional. e.g. Given the name <code>'TextEdit'</code>, appscript first searches for an application with that exact filename; if none is found, it automatically adds an <code>.app</code> suffix (<code>'TextEdit.app'</code>) and tries again.</dd>
47
+
48
+ <dt><code>app.by_id(<var>id</var>)</code></dt>
49
+ <dd>The application's bundle id, e.g. <code>'com.apple.textedit'</code>. Cocoa applications often have unique bundle ids.</dd>
50
+
51
+ <dt><code>app.by_creator(<var>creator</var>)</code></dt>
52
+ <dd>A four-character string containing the application's creator type, e.g. <code>'ttxt'</code>. Older Carbon-based applications and some Cocoa-based applications have unique creator types.</dd>
53
+
54
+ <dt><code>app.by_pid(<var>pid</var>)</code></dt>
55
+ <dd>A valid Unix process id, e.g. <code>5687</code>.</dd>
56
+
57
+ <dt><code>app.by_url(<var>url</var>)</code></dt>
58
+ <dd>A URL of form <code>eppc://[user[:password]@host/Application%20Name[?[uid=#]&amp;[pid=#]</code>, e.g. <code>eppc://johnsmith:foobar@office-mac.local/TextEdit?uid=501</code>. The host name/IP address and the process name (case-sensitive) are required. The username and password are optional: if omitted, the OS will obtain this information from the user's keychain or display a dialog asking the user to input one or both values. The user id and process id are also optional; if the process id is given, the process name will be ignored.</dd>
59
+
60
+ <dt><code>app.by_aem_app(<var>aem_app</var>)</code></dt>
61
+ <dd>An <code>AEM::Application</code> instance.</dd>
62
+
63
+ <dt><code>app.current</code></dt>
64
+ <dd>The host application.</dd>
65
+ </dl>
66
+
67
+
68
+ <p>Examples:</p>
69
+
70
+ <pre><code>require "appscript"
71
+ include Appscript
72
+
73
+ ical = app('iCal')
74
+
75
+ textedit = app('TextEdit.app')
76
+
77
+ safari = app('/Applications/Safari')
78
+
79
+ addressbook = app.by_id('com.apple.addressbook')
80
+
81
+ quicktimeplayer = app.by_creator('TVOD')
82
+
83
+ finder = app.by_url('eppc://192.168.10.1/Finder')
84
+
85
+ itunes = app.by_url('eppc://Jan%20Smith@G4.local/iTunes')</code></pre>
86
+
87
+
88
+ <h3>Basic commands</h3>
89
+
90
+ <p>All applications should respond to the following commands:</p>
91
+
92
+ <pre><code>run -- Run an application. Most applications will open an empty,
93
+ untitled window.
94
+
95
+ launch -- Launch an application without sending it a 'run' event.
96
+ Applications that normally open a new, empty document
97
+ upon launch won't. [1]
98
+
99
+ activate -- Bring the application to the front.
100
+
101
+ reopen -- Reactivate a running application. Some applications
102
+ will open a new untitled window if no window is open.
103
+
104
+ open(<var>value</var>) -- Open the specified object(s).
105
+ anything -- list of objects to open, typically a list of
106
+ MacTypes::Alias
107
+
108
+ print(<var>value</var>) -- Print the specified object(s).
109
+ anything -- list of objects to print, typically a list of
110
+ MacTypes::Alias
111
+
112
+ quit(saving => <var>value</var>) -- Quit an application.
113
+ saving : :yes | :ask | :no -- specifies whether to save
114
+ currently open documents
115
+ </code></pre>
116
+
117
+ <p><small>[1] The <code>launch</code> command should be used before constructing references or sending other commands, otherwise appscript will run the application normally.</small></p>
118
+
119
+ <p>Some applications may provide their own definitions of some or all of these commands, so check their terminology before use.</p>
120
+
121
+ <p>Appscript also defines <code>get</code> and <code>set</code> commands for any scriptable application that doesn't supply its own definitions:</p>
122
+
123
+ <pre><code>get(<var>value</var>) -- Get the data for an object.
124
+ reference -- the object for the command
125
+ Result: anything -- the reply for the command
126
+
127
+ set(<var>value</var>, :to =&gt; <var>value</var>) -- Set an object's data.
128
+ reference -- the object for the command
129
+ to : anything -- The new value.</code></pre>
130
+
131
+ <p>Note that these commands are only useful in applications that define an Apple Event Object Model as part of their Apple event interface.</p>
132
+
133
+
134
+ <h3>Transaction support</h3>
135
+
136
+ <p>Application objects implement three additional methods, <code>begin_transaction</code>, <code>end_transaction</code> and <code>abort_transaction</code> that allow a sequence of related commands to be handled as a single operation by applications that support transactions, e.g. FileMaker Pro.</p>
137
+
138
+ <p>Once the application object's <code>begin_transaction</code> method is called, all subsequent commands to that application will be sent as part of the same transaction until <code>end_transaction</code> or <code>abort_transaction</code> is called.</p>
139
+
140
+ <p>The <code>begin_transaction</code> method takes an optional <code>session</code> argument that indicates the specific transaction session to open (in applications that support this).</p>
141
+
142
+ <p class="hilitebox">Remember to call <code>end_transaction</code> or <code>abort_transaction</code> at the end of every transaction. (This includes transactions interrupted by a raised exception.) If a transaction is accidentally left open, appscript will try to end it automatically when the application object is garbage-collected, but this is not guaranteed to succeed.</p>
143
+
144
+
145
+ <h3>Local application launching notes</h3>
146
+
147
+ <p>Note: the following information only applies to local applications as appscript cannot directly launch applications on a remote Mac. To control a remote application, the application must be running beforehand or else launched indirectly (e.g. by using the remote Mac's Finder to open it).</p>
148
+
149
+
150
+ <h4>How applications are identified</h4>
151
+
152
+ <p>When you create an Application object by application name, bundle id or creator type, appscript uses LaunchServices to locate an application matching that description. If you have more than one copy of the same application installed, you can identify the one you want by providing its full path, otherwise LaunchServices will identify the newest copy for you.</p>
153
+
154
+ <p>Appscript identifies running applications by their process ids so it's possible to control multiple versions of an application running at the same time if their Application objects are created using process ids or eppc URLs.</p>
155
+
156
+
157
+ <h4>Checking if an application is running</h4>
158
+
159
+ <p>You can check if the application specified by an Application object is currently running by calling its <code>#is_running?</code> method. This is useful if you don't want to perform commands on an application that isn't already running. For example:</p>
160
+
161
+ <pre><code>te = app('TextEdit')
162
+ # Only perform TextEdit-related commands if it's already running:
163
+ if te.is_running?
164
+ # all TextEdit-related code goes here...
165
+ end</code></pre>
166
+
167
+ <p class="hilitebox">Remember that appscript automatically launches a non-running application the first time your script makes reference to any of its properties, elements or commands. To avoid accidental launches, <em>all</em> code relating to that application must be included in a conditional block that only executes if <code>#is_running?</code> returns <code>true</code>.</p>
168
+
169
+
170
+ <h4>Launch errors</h4>
171
+
172
+ <p>If the application can't be launched for some reason (e.g. if it's in the Trash), an <code>Appscript::CantLaunchApplicationError</code> error will be raised. This provides a description of the problem (if it's a standard LaunchServices error) along with the original OS error number, which you can also obtain via the <code>CantLaunchApplicationError#to_i</code> method.</p>
173
+
174
+
175
+ <h4>Using <code>launch</code> vs <code>run</code></h4>
176
+
177
+ <p>When appscript launches a non-running application, it normally sends it a <code>run</code> command as part of the launching process. If you wish to avoid this, you should start the application by sending it a <code>launch</code> command before doing anything else. For example:</p>
178
+
179
+ <pre><code>te = app('TextEdit')
180
+ te.launch
181
+ # other TextEdit-related code goes here...</code></pre>
182
+
183
+ <p>This is useful when you want to start an application without it going through its normal startup procedure. For example, the above script will launch TextEdit without causing it to display a new, empty document (its usual behaviour).</p>
184
+
185
+ <p><code>launch</code> is also useful if you need to send a non-running application an <code>open</code> command as its very first instruction. For example, to send an <code>open</code> command to a non-stay-open application that batch processes one or more dropped files then quits again, you must first start it using <code>launch</code>, then send it an <code>open</code> command with the files to process. If you forget to do this, the application will first receive a <code>run</code> command and quit again before the <code>open</code> command can be handled.</p>
186
+
187
+
188
+ <h4>Restarting applications</h4>
189
+
190
+ <p>As soon as you start to construct a reference or command using a newly created Application objects, if the application is not already running then appscript will automatically launch it in order to obtain its terminology.</p>
191
+
192
+ <p>If the target application has stopped running since the Application object was created, trying to send it a command using that Application object will result in an invalid connection error (-609), unless that command is <code>run</code> or <code>launch</code>. This restriction prevents appscript accidentally restarting an application that has unexpectedly quit while a script is controlling it. Scripts can restart an application by sending an explicit <code>run</code> or <code>launch</code> command, or by creating a new Application object for it.</p>
193
+
194
+ <p>Note that you can still use Application objects to control applications that have been quit <em>and</em> restarted since the Application object was created. Appscript will automatically update the Application object's process id information as needed.</p>
195
+
196
+
197
+ <p class="hilitebox">There is a known problem with quitting and immediately relaunching an application via appscript, where the relaunch instruction is sent to the application before it has actually quit. This timing issue appears to be the OS's fault; the current workaround for scripts that need to quit and restart applications is to insert a few seconds' delay between the <code>quit</code> and <code>run</code>/<code>launch</code> using Ruby's <code>sleep</code> function.</p>
198
+
199
+
200
+ </div>
201
+
202
+ <!-- bottom navigation -->
203
+
204
+ <div class="footer">
205
+ <a href="06_classesandenums.html">Previous</a> &bull;
206
+ <a href="index.html">Up</a> &bull;
207
+ <a href="08_realvsgenericreferences.html">Next</a>
208
+ </div>
209
+
210
+ </body>
211
+ </html>
@@ -0,0 +1,96 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3
+ <head>
4
+
5
+ <title>rb-appscript manual | 8. Real vs. Generic References</title>
6
+
7
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
8
+ <style type="text/css" media="all"><!--@import url(../full.css);--></style>
9
+
10
+ </head>
11
+ <body>
12
+
13
+ <h1><img src="../rb-appscript-logo.png" alt="rb-appscript" title="rb-appscript" /></h1>
14
+
15
+ <!-- top navigation -->
16
+ <div class="navbar">
17
+ <a href="07_applicationobjects.html">Previous</a> &bull;
18
+ <a href="index.html">Up</a> &bull;
19
+ <a href="09_referenceforms.html">Next</a>
20
+
21
+ <span>
22
+ <strong><a href="../appscript-manual/index.html">appscript</a></strong> /
23
+ <a href="../mactypes-manual/index.html">mactypes</a> /
24
+ <a href="../osax-manual/index.html">osax</a> /
25
+ <a href="../aem-manual/index.html">aem</a>
26
+ </span>
27
+ </div>
28
+
29
+ <!-- content -->
30
+ <div id="content">
31
+
32
+ <h2>8. Real vs. Generic References</h2>
33
+
34
+ <h3>Real vs. generic references</h3>
35
+
36
+ <p>References may be either 'real' or 'generic'. A real reference relates to a specific application, while a generic reference doesn't. Generic references provide a convenient shortcut for writing literal references without having to specify an application each time.</p>
37
+
38
+ <p>A real reference begins with an <code>app</code> call, followed by a <code>by_name</code>, <code>by_id</code>, <code>by_creator</code>, <code>by_pid</code>, <code>by_url</code> call that identifies the application whose object(s) it refers to, e.g.:</p>
39
+
40
+ <pre><code>app.by_name('TextEdit').documents.end
41
+ app.by_url('eppc://my-mac.local/Finder').home.folders.name</code></pre>
42
+
43
+ <p>(Remember than <code>app.by_name('TextEdit')</code> can be shortened to <code>app('TextEdit')</code> for convenience.</p>
44
+
45
+ <p>A generic reference begins with <code>app</code>, <code>con</code> or <code>its</code> and does not identify the application to which it relates, e.g.:</p>
46
+
47
+ <pre><code>app.documents.end
48
+ con.word[3]
49
+ its.name.begins_with('d')</code></pre>
50
+
51
+ <p>Generic references are only evaluated when used used within real references, either as selectors:</p>
52
+
53
+ <pre><code>app('Finder').home.folders[<em>its.name.begins_with('d')</em>].get
54
+
55
+ app('Tex-Edit Plus').windows[1].text[<em>con.words[2]</em>, <em>con.words[-2]</em>].get</code></pre>
56
+
57
+ <p>or as command parameters:</p>
58
+
59
+ <pre><code>app('TextEdit').make(
60
+ :new =&gt; :word,
61
+ :at =&gt; <em>app.documents[1].words.end</em>,
62
+ :with_data =&gt; 'Hello')
63
+
64
+ app('Finder').desktop.duplicate(
65
+ :to =&gt; <em>app.home.folders['Desktop Copy']</em>)</code></pre>
66
+
67
+
68
+ <h3>Comparing and hashing references</h3>
69
+
70
+ <p>Application references can be compared for equality and are hashable (so can be used as dictionary keys). For two real references to be considered equal, both must have the same application path or url and reference structure. Examples:</p>
71
+
72
+ <pre><code>puts app('TextEdit').documents[1] == \
73
+ app.by_id('com.apple.textedit').documents[1].get
74
+ # Result: true; both references evaluate to the same
75
+ # application path and reference
76
+
77
+ puts app('Finder').home == app('Finder').home.get
78
+ # Result: false; app('Finder').home.get returns a
79
+ # different reference to the same location</code></pre>
80
+
81
+ <p>For two generic references to be equal, both must have the same reference structure. Note that comparing generic references to real references will always return a false result.</p>
82
+
83
+
84
+
85
+ </div>
86
+
87
+ <!-- bottom navigation -->
88
+
89
+ <div class="footer">
90
+ <a href="07_applicationobjects.html">Previous</a> &bull;
91
+ <a href="index.html">Up</a> &bull;
92
+ <a href="09_referenceforms.html">Next</a>
93
+ </div>
94
+
95
+ </body>
96
+ </html>
@@ -0,0 +1,241 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3
+ <head>
4
+
5
+ <title>rb-appscript manual | 9. Reference Forms</title>
6
+
7
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
8
+ <style type="text/css" media="all"><!--@import url(../full.css);--></style>
9
+
10
+ </head>
11
+ <body>
12
+
13
+ <h1><img src="../rb-appscript-logo.png" alt="rb-appscript" title="rb-appscript" /></h1>
14
+
15
+ <!-- top navigation -->
16
+ <div class="navbar">
17
+ <a href="08_realvsgenericreferences.html">Previous</a> &bull;
18
+ <a href="index.html">Up</a> &bull;
19
+ <a href="10_referenceexamples.html">Next</a>
20
+
21
+ <span>
22
+ <strong><a href="../appscript-manual/index.html">appscript</a></strong> /
23
+ <a href="../mactypes-manual/index.html">mactypes</a> /
24
+ <a href="../osax-manual/index.html">osax</a> /
25
+ <a href="../aem-manual/index.html">aem</a>
26
+ </span>
27
+ </div>
28
+
29
+ <!-- content -->
30
+ <div id="content">
31
+
32
+ <h2>9. Reference Forms</h2>
33
+
34
+ <h3>Property reference</h3>
35
+
36
+ <p>A property either contains a simple value or represents a one-to-one relationship between two application objects. Properties either describe the application object (its name, class, size, color, preferences, etc.) or provide a convenient reference to other object(s) of interest to users (e.g. home, current_track).</p>
37
+
38
+ <p>Syntax:</p>
39
+ <pre><code>reference.property</code></pre>
40
+
41
+ <p>Examples:</p>
42
+ <pre><code>textedit.<em>name</em>
43
+ textedit.documents[1].<em>text</em>
44
+ finder.<em>home</em>.files.<em>name</em></code></pre>
45
+
46
+
47
+
48
+ <h3>Element references</h3>
49
+
50
+ <p>Elements represent a one-to-many relationship between application objects. Elements usually reflect the containment structure of the application object model and generally provide multiple ways of referencing some or all of those objects (e.g. characters/words/paragraphs of documents by index/relative-position/range/filter).</p>
51
+
52
+ <h4>All Elements</h4>
53
+
54
+ <p>Syntax:</p>
55
+ <pre><code>reference.elements</code></pre>
56
+
57
+ <p>Examples:</p>
58
+ <pre><code>finder.home.<em>folders</em>
59
+ textedit.<em>windows</em>
60
+ textedit.<em>documents</em>.<em>paragraphs</em>.<em>words</em></code></pre>
61
+
62
+
63
+ <h4>by Name</h4>
64
+
65
+ <p>Syntax:</p>
66
+ <pre><code>elements[selector]
67
+ selector : String -- name of object (as matches its 'name' property)</code></pre>
68
+
69
+ <p>Examples:</p>
70
+ <pre><code>disks['Macintosh HD']
71
+ files['index.html']</code></pre>
72
+
73
+ <p class="hilitebox">Note: applications usually treat object names as case-insensitive. Where multiple element have the same name, a by-name reference only identifies the first element found with that name. (Tip: to identify <em>all</em> elements with a particular name, use a by-filter reference instead.)</p>
74
+
75
+
76
+ <h4>by Index</h4>
77
+
78
+ <p>Syntax:</p>
79
+ <pre><code>elements[selector]
80
+ selector : Fixnum | Bignum -- index of object</code></pre>
81
+
82
+ <p>Examples:</p>
83
+ <pre><code>words[3]
84
+ items[-1]</code></pre>
85
+
86
+ <p class="hilitebox">Note: elements are <em>one-indexed</em> (AEM-style indexing), not zero-indexed (Ruby-style indexing).</p>
87
+
88
+
89
+ <h4>by ID</h4>
90
+
91
+ <p>Syntax:</p>
92
+ <pre><code>elements.ID(selector)
93
+ selector : anything -- object's id</code></pre>
94
+
95
+ <p>Examples:</p>
96
+ <pre><code>windows.ID(4321)</code></pre>
97
+
98
+
99
+ <h4>by Absolute Position</h4>
100
+
101
+ <p>Syntax:</p>
102
+ <pre><code>elements.first -- first element
103
+ elements.middle -- middle element
104
+ elements.last -- last element
105
+ elements.any -- random element</code></pre>
106
+
107
+ <p>Examples:</p>
108
+ <pre><code>documents.first
109
+ paragraphs.last
110
+ files.any</code></pre>
111
+
112
+
113
+ <h4>by Relative Position</h4>
114
+
115
+ <p>Syntax:</p>
116
+ <pre><code>elements[selector].previous(class) -- nearest element of a given class to appear
117
+ before the specified element
118
+ elements[selector].next(class) -- nearest element of a given class to appear
119
+ after the specified element
120
+ class : Symbol -- class of element (see <a href="06_classesandenums.html">Classes and Enumerated Types</a>)</code></pre>
121
+
122
+ <p>Examples:</p>
123
+ <pre><code>words[3].next(:word)
124
+ paragraphs[-1].previous(:character)</code></pre>
125
+
126
+
127
+ <h4>by Range</h4>
128
+
129
+ <p>Range references select all elements between and including two references indicating the start and stop of the range. The start and stop references are normally declared relative to the container of the elements being selected. Appscript defines an object, <code>con</code> (short for 'container'), to indicate this container; for example, to indicate the third paragraph of the currrent container object:</p>
130
+
131
+ <pre><code>con.paragraphs[3]</code></pre>
132
+
133
+ <p>For convenience, the range reference also allows start and stop references to be written in shorthand form where their element class is the same as the elements being selected; thus:</p>
134
+
135
+ <pre><code>ref.paragraphs[con.paragraphs[3], con.paragraphs[-1]]</code></pre>
136
+
137
+ <p>can also be written as:</p>
138
+
139
+ <pre><code>ref.paragraphs[3:-1]</code></pre>
140
+
141
+ <p>Some applications can handle more complex range references. For example, the following will work in Tex-Edit Plus:</p>
142
+
143
+ <pre><code>ref.words[con.characters[5], con.paragraphs[-2]]</code></pre>
144
+
145
+ <p>Syntax:</p>
146
+ <pre><code>elements[start, stop]
147
+ start : Fixnum | Bignum | String | Reference -- beginning of range
148
+ stop : Fixnum | Bignum | String | Reference -- end of range</code></pre>
149
+
150
+ <p>Examples:</p>
151
+ <pre><code>folders['Documents', 'Movies']
152
+ documents[1, 3]
153
+ text[con.characters[5], con.words[-2]]</code></pre>
154
+
155
+ <p class="hilitebox">Note: ranges are specified as [start-position, <em>stop-position</em>] (AEM-style ranges), not [start-position, <em>length</em>] (Ruby-style ranges).</p>
156
+
157
+ <h4>by Filter</h4>
158
+
159
+ <p>A reference to each element that satisfies one or more conditions specified by a test expression:</p>
160
+
161
+ <pre><code>elements[testExpression]</code></pre>
162
+
163
+ <p>Test expressions consist of the following:</p>
164
+
165
+ <ul>
166
+ <li><p>A reference to each element being tested, represented by appscript's <code>its</code> object. This object supports all valid reference forms, allowing you to construct references to its properties and elements. For example:</p>
167
+
168
+ <pre><code>its
169
+ its.size
170
+ its.words.first</code></pre></li>
171
+
172
+ <li><p>One or more conditional tests, implemented as methods on the reference being tested. Each method takes a test reference or a value as its sole argument.</p>
173
+
174
+ <p>Syntax:</p>
175
+ <pre><code>reference.lt(value) # &lt;
176
+ reference.le(value) # &lt;=
177
+ reference.eq(value) # ==
178
+ reference.ne(value) # !=
179
+ reference.gt(value) # &gt;
180
+ reference.ge(value) # &gt;=
181
+ reference.begins_with(value)
182
+ reference.ends_with(value)
183
+ reference.contains(value)
184
+ reference.is_in(value)
185
+ reference.does_not_begin_with(value)
186
+ reference.does_not_end_with(value)
187
+ reference.does_not_contain(value)
188
+ reference.is_not_in(value)
189
+ value : reference or value</code></pre>
190
+
191
+ <p>Examples:</p>
192
+ <pre><code>its.eq('')
193
+ its.size.gt(1024)
194
+ its.words.first.begins_with('A')
195
+ its.characters.first.eq(its.characters.last)</code></pre>
196
+
197
+ <li><p>Zero or more logical tests, implemented as properties/methods on conditional tests. The <code>and</code> and <code>or</code> methods take one or more conditional or logic tests as arguments.</p>
198
+
199
+ <p>Syntax:</p>
200
+ <pre><code>test.and(test, ...)
201
+ test.or(test, ...)
202
+ test.not</code></pre>
203
+
204
+ <p>Examples:</p>
205
+ <pre><code>its.eq('').not
206
+ its.size.gt(1024).and(its.size.lt(10240))
207
+ its.words[1].begins_with('A').or(
208
+ its.words[2].contains('ce'),
209
+ its.words[1].eq('Foo'))</code></pre></li>
210
+ </ul>
211
+
212
+
213
+
214
+ <h3>Insertion location</h3>
215
+
216
+ <p>Insertion locations can be specified at the beginning or end of all elements, or before or after a specified element or element range.</p>
217
+
218
+ <p>Syntax:</p>
219
+ <pre><code>elements.beginning
220
+ elements.end
221
+ elements[selector].before
222
+ elements[selector].after</code></pre>
223
+
224
+ <p>Examples:</p>
225
+ <pre><code>documents.end
226
+ paragraphs[1].before</code></pre>
227
+
228
+
229
+
230
+ </div>
231
+
232
+ <!-- bottom navigation -->
233
+
234
+ <div class="footer">
235
+ <a href="08_realvsgenericreferences.html">Previous</a> &bull;
236
+ <a href="index.html">Up</a> &bull;
237
+ <a href="10_referenceexamples.html">Next</a>
238
+ </div>
239
+
240
+ </body>
241
+ </html>