rb-scpt 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGES +497 -0
  3. data/doc/aem-manual/01_introduction.html +60 -0
  4. data/doc/aem-manual/02_apioverview.html +107 -0
  5. data/doc/aem-manual/03_packingandunpackingdata.html +135 -0
  6. data/doc/aem-manual/04_references.html +409 -0
  7. data/doc/aem-manual/05_targetingapplications.html +164 -0
  8. data/doc/aem-manual/06_buildingandsendingevents.html +229 -0
  9. data/doc/aem-manual/07_findapp.html +63 -0
  10. data/doc/aem-manual/08_examples.html +94 -0
  11. data/doc/aem-manual/aemreferenceinheritance.gif +0 -0
  12. data/doc/aem-manual/index.html +56 -0
  13. data/doc/appscript-manual/01_introduction.html +94 -0
  14. data/doc/appscript-manual/02_aboutappscripting.html +247 -0
  15. data/doc/appscript-manual/03_quicktutorial.html +167 -0
  16. data/doc/appscript-manual/04_gettinghelp.html +188 -0
  17. data/doc/appscript-manual/05_keywordconversion.html +106 -0
  18. data/doc/appscript-manual/06_classesandenums.html +192 -0
  19. data/doc/appscript-manual/07_applicationobjects.html +211 -0
  20. data/doc/appscript-manual/08_realvsgenericreferences.html +96 -0
  21. data/doc/appscript-manual/09_referenceforms.html +241 -0
  22. data/doc/appscript-manual/10_referenceexamples.html +154 -0
  23. data/doc/appscript-manual/11_applicationcommands.html +245 -0
  24. data/doc/appscript-manual/12_commandexamples.html +138 -0
  25. data/doc/appscript-manual/13_performanceissues.html +142 -0
  26. data/doc/appscript-manual/14_notes.html +80 -0
  27. data/doc/appscript-manual/application_architecture.gif +0 -0
  28. data/doc/appscript-manual/application_architecture2.gif +0 -0
  29. data/doc/appscript-manual/finder_to_textedit_event.gif +0 -0
  30. data/doc/appscript-manual/index.html +62 -0
  31. data/doc/appscript-manual/relationships_example.gif +0 -0
  32. data/doc/appscript-manual/ruby_to_itunes_event.gif +0 -0
  33. data/doc/full.css +106 -0
  34. data/doc/index.html +45 -0
  35. data/doc/mactypes-manual/01_introduction.html +54 -0
  36. data/doc/mactypes-manual/02_aliasclass.html +124 -0
  37. data/doc/mactypes-manual/03_fileurlclass.html +126 -0
  38. data/doc/mactypes-manual/04_unitsclass.html +100 -0
  39. data/doc/mactypes-manual/index.html +53 -0
  40. data/doc/osax-manual/01_introduction.html +67 -0
  41. data/doc/osax-manual/02_interface.html +147 -0
  42. data/doc/osax-manual/03_examples.html +73 -0
  43. data/doc/osax-manual/04_notes.html +61 -0
  44. data/doc/osax-manual/index.html +53 -0
  45. data/doc/rb-appscript-logo.png +0 -0
  46. data/extconf.rb +65 -0
  47. data/rb-scpt.gemspec +14 -0
  48. data/sample/AB_export_vcard.rb +31 -0
  49. data/sample/AB_list_people_with_emails.rb +13 -0
  50. data/sample/Add_iCal_event.rb +21 -0
  51. data/sample/Create_daily_iCal_todos.rb +75 -0
  52. data/sample/Export_Address_Book_phone_numbers.rb +59 -0
  53. data/sample/Hello_world.rb +21 -0
  54. data/sample/List_iTunes_playlist_names.rb +11 -0
  55. data/sample/Make_Mail_message.rb +33 -0
  56. data/sample/Open_file_in_TextEdit.rb +13 -0
  57. data/sample/Organize_Mail_messages.rb +61 -0
  58. data/sample/Print_folder_tree.rb +16 -0
  59. data/sample/Select_all_HTML_files.rb +14 -0
  60. data/sample/Set_iChat_status.rb +24 -0
  61. data/sample/Simple_Finder_GUI_Scripting.rb +18 -0
  62. data/sample/Stagger_Finder_windows.rb +25 -0
  63. data/sample/TextEdit_demo.rb +130 -0
  64. data/sample/iTunes_top40_to_html.rb +71 -0
  65. data/src/SendThreadSafe.c +380 -0
  66. data/src/SendThreadSafe.h +139 -0
  67. data/src/lib/_aem/aemreference.rb +1022 -0
  68. data/src/lib/_aem/codecs.rb +662 -0
  69. data/src/lib/_aem/connect.rb +205 -0
  70. data/src/lib/_aem/encodingsupport.rb +77 -0
  71. data/src/lib/_aem/findapp.rb +85 -0
  72. data/src/lib/_aem/mactypes.rb +251 -0
  73. data/src/lib/_aem/send.rb +279 -0
  74. data/src/lib/_aem/typewrappers.rb +59 -0
  75. data/src/lib/_appscript/defaultterminology.rb +277 -0
  76. data/src/lib/_appscript/referencerenderer.rb +245 -0
  77. data/src/lib/_appscript/reservedkeywords.rb +116 -0
  78. data/src/lib/_appscript/safeobject.rb +249 -0
  79. data/src/lib/_appscript/terminology.rb +471 -0
  80. data/src/lib/aem.rb +253 -0
  81. data/src/lib/appscript.rb +1075 -0
  82. data/src/lib/kae.rb +1489 -0
  83. data/src/lib/osax.rb +659 -0
  84. data/src/rbae.c +979 -0
  85. data/test/README +3 -0
  86. data/test/test_aemreference.rb +118 -0
  87. data/test/test_appscriptcommands.rb +152 -0
  88. data/test/test_appscriptreference.rb +106 -0
  89. data/test/test_codecs.rb +186 -0
  90. data/test/test_findapp.rb +26 -0
  91. data/test/test_mactypes.rb +79 -0
  92. data/test/test_osax.rb +54 -0
  93. data/test/testall.sh +10 -0
  94. metadata +145 -0
@@ -0,0 +1,192 @@
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 | 6. Classes and Enumerated Types</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="05_keywordconversion.html">Previous</a> &bull;
18
+ <a href="index.html">Up</a> &bull;
19
+ <a href="07_applicationobjects.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>6. Classes and Enumerated Types</h2>
33
+
34
+ <h3>Appscript keywords</h3>
35
+
36
+ <p>For your convenience, appscript represents Apple event type names and application-specific class and enumerator names as Ruby symbols. Examples:</p>
37
+
38
+ <pre><code># AEM-defined data types:
39
+ :boolean
40
+ :unicode_text
41
+ :list
42
+
43
+ # Application-defined class names:
44
+ :document
45
+ :window
46
+ :disk
47
+
48
+ # Application-defined enumerators:
49
+ :yes
50
+ :no
51
+ :ask</code></pre>
52
+
53
+ <p>Occasionally an application dictionary defines a type or enumerator without providing it with a corresponding name name. In these cases, the value will be represented as a raw <code>AEM::AEType</code> or <code>AEM::AEEnum</code> object instead of a symbol, e.g.:</p>
54
+
55
+ <pre><code>AEM::AEType.new('abcd')
56
+
57
+ AEM::AEEnum.new('xyz ')</code></pre>
58
+
59
+ <p>See the aem manual for more information on these lower-level objects, should you need to use them.</p>
60
+
61
+
62
+ <h3>Common AE types</h3>
63
+
64
+ <table width="100%" summary="AE-Ruby type mappings">
65
+ <thead>
66
+ <tr><th>AE type</th><th>appscript name</th><th>Ruby class</th></tr>
67
+ </thead>
68
+ <tbody>
69
+ <tr><td><code>typeNull</code></td><td><code>:null</code></td><td><code>NilClass</code></td></tr>
70
+ <tr><td><code>typeBoolean</code></td><td><code>:boolean</code></td><td><code>TrueClass/FalseClass</code></td></tr>
71
+ <tr><td><code>typeInteger</code></td><td><code>:integer</code></td><td><code>Fixnum/Bignum</code></td></tr>
72
+ <tr><td><code>typeFloat</code></td><td><code>:float</code></td><td><code>Float</code></td></tr>
73
+ <tr><td><code>typeChar</code> *</td><td><code>:string</code></td><td><code>String</code></td></tr>
74
+ <tr><td><code>typeStyledText</code> *</td><td><code>:styled_text</code></td><td><code>String</code></td></tr>
75
+ <tr><td><code>typeIntlText</code> *</td><td><code>:international_text</code></td><td><code>String</code></td></tr>
76
+ <tr><td><code>typeUnicodeText</code></td><td><code>:unicode_text</code></td><td><code>String</code></td></tr>
77
+ <tr><td><code>typeAEList</code></td><td><code>:list</code></td><td><code>Array</code></td></tr>
78
+ <tr><td><code>typeAERecord</code></td><td><code>:record</code></td><td><code>Hash</code></td></tr>
79
+ <tr><td><code>typeLongDateTime</code></td><td><code>:date</code></td><td><code>Time</code></td></tr>
80
+ <tr><td><code>typeAlias</code></td><td><code>:alias</code></td><td><code>MacTypes::Alias</code></td></tr>
81
+ <tr><td><code>typeFileURL</code></td><td><code>:file_url</code></td><td><code>MacTypes::FileURL</code></td></tr>
82
+ <tr><td><code>typeFSRef</code></td><td><code>:file_ref</code></td><td><code>MacTypes::FileURL</code></td></tr>
83
+ <tr><td><code>typeFSS</code> *</td><td><code>:file_specification</code></td><td><code>MacTypes::FileURL</code></td></tr>
84
+ <tr><td><code>typeObjectSpecifier</code></td><td><code>:reference</code></td><td><code>Appscript::Reference</code></td></tr>
85
+ <tr><td><code>typeInsertionLoc</code></td><td><code>:location_reference</code></td><td><code>Appscript::Reference</code></td></tr>
86
+ <tr><td><code>typeType</code></td><td><code>:type_class</code></td><td><code>Symbol</code></td></tr>
87
+ <tr><td><code>typeEnumerated</code></td><td><code>:enumerator</code></td><td><code>Symbol</code></td></tr>
88
+ <tr><td><small>unit types; e.g. </small> <code>typeFeet</code></td><td><small>unit names; e.g.</small> <code>:feet</code></td><td><code>MacTypes::Units</code></td></tr>
89
+ </tbody>
90
+ </table>
91
+
92
+ <p>(Note that types marked with '*' are officially deprecated and/or their use discouraged in Mac OS X. They remain supported to ensure backwards compatibility with older applications that may use them.)</p>
93
+
94
+
95
+ <h3>Type mapping notes</h3>
96
+
97
+ <p>While AEM-Ruby type conversions generally work quite seamlessly, it is sometimes useful to know some of the details involved, particularly when troubleshooting code that deals with older or buggy applications.</p>
98
+
99
+ <h4>Numbers</h4>
100
+
101
+ <p>When packing a <code>Bignum</code>, appscript will pack it either as a 32-bit integer (most preferred), 64-bit integer, or 64-bit float (least preferred), depending on the value's size.</p>
102
+
103
+
104
+ <h4>Strings (Ruby 1.8)</h4>
105
+
106
+ <p>Because Ruby 1.8 lacks native Unicode support, appscript unpacks all text-related AE types (<code>typeChar</code>, <code>typeUnicodeText</code>, etc.) as String instances containing UTF8-encoded data.</p>
107
+
108
+ <p>Appscript packs String instances containing UTF8-encoded data as the <code>typeUnicodeText</code> AE type. If the string does not contain valid UTF-8 data, a TypeError will be raised.</p>
109
+
110
+ <div class="hilitebox">
111
+ <p>You can enable a degree of UTF-8 support in Ruby's <code>String</code> class via the Jcode module (included in Ruby's standard library). To use Jcode, add the following lines to the start of your Ruby scripts, irb sessions, etc.:</p>
112
+ <pre><code>require "jcode"
113
+ $KCODE = "UTF8"</code></pre>
114
+
115
+ <p>This will improve the display of non-ASCII characters in irb, amongst other things.</p>
116
+
117
+ </div>
118
+
119
+ <p>Note that while <code>typeUnicodeText</code> is formally deprecated in Mac OS X 10.4+ in favour of <code>typeUTF8Text</code> and <code>typeUTF16ExternalRepresentation</code>, it is still in common usage so appscript continues to use it to ensure the broadest compatibility with existing scriptable applications.</p>
120
+
121
+
122
+ <h4>Strings (Ruby 1.9+)</h4>
123
+
124
+ <p>Each String instance in Ruby 1.9+ contains an <code>Encoding</code> instance that identifies the character set used. By default, appscript unpacks a text descriptors as a String instance containing UTF-8 encoded data and a UTF-8 Encoding, and encodes a string as UTF-8 prior to packing its data into a text descriptor. To disable this behavior for individual <code>Application</code> instances:</p>
125
+
126
+ <pre><code>some_app = Appscript.app('SomeApp')
127
+ some_app.AS_app_data.use_ascii_8bit
128
+ # Send commands here...</code></pre>
129
+
130
+ <p>Once invoked, Strings will be treated as UTF-8 encoded data with ASCII-8BIT Encoding; this is equivalent to the Ruby 1.8 behavior.</p>
131
+
132
+
133
+ <h4>Dates and Times</h4>
134
+
135
+ <p>Appscript packs Ruby <code>Time</code>, <code>Date</code> and <code>DateTime</code> instances as <code>typeLongDateTime</code> descriptors. (Note that AE dates does not include locale information.)</p>
136
+
137
+ <p>By default, <code>typeLongDateTime</code> descriptors are unpacked as Ruby <code>Time</code> instances. This is sufficient for most purposes; however, where the AEDesc contains a date value below or above the range supported by Ruby's <code>Time</code> class, an error will occur. To unpack dates as <code>DateTime</code> instances for individual <code>Application</code> instances:</p>
138
+
139
+ <pre><code>some_app = Appscript.app('SomeApp')
140
+ some_app.AS_app_data.use_datetime
141
+ # Send commands here...</code></pre>
142
+
143
+
144
+ <h4>Filesystem references</h4>
145
+
146
+ <p>The <code>typeAlias</code> AE type represents a filesystem object, and will continue to point to that object even if it's renamed or moved to another location on the same disk. The <code>typeFSRef</code> and <code>typeFileURL</code> types represent a filesystem location. Both can represent existing locations; the <code>typeFileURL</code> type can also be used to specify locations that don't already exist.</p>
147
+
148
+ <p>FSSpecs are also supported for sake of backwards-compatibility with older Carbon applications that sometimes still use them. They're deprecated in OS X, however, due to lack of proper Unicode and long filename support, and their use is discouraged.</p>
149
+
150
+ <p>Note that appscript unpacks FSRefs, FileURLs and FSSpecs as <code>MacTypes::FileURL</code> objects; users do not normally need to worry about this. <code>MacTypes::FileURL</code> objects created in Ruby will always pack as type <code>:FileURL</code>, however. See the <a href="../mactypes-manual/index.html">mactypes manual</a> for more information on <code>MacTypes::Alias</code> and <code>MacTypes::FileURL</code>.</p>
151
+
152
+ <p>When asking an application to coerce a return value into a file type you must specify the exact type (<code>:alias</code>, <code>:file_url</code>, <code>:file_ref</code>, or <code>:file_specification</code>) in the <code>get</code> command. e.g. To get a <code>MacTypes::FileURL</code> object for the current user's home folder, you would usually use:</p>
153
+
154
+ <pre><code>app('Finder').home.get(:result_type=&gt;:file_url)</code></pre>
155
+
156
+ <p>Also be aware that some applications may not support coercions to all AE file types; you'll need to experiment to find out which coercions are supported.</p>
157
+
158
+
159
+ <h4>Records</h4>
160
+
161
+ <p>The <code>typeAERecord</code> AE type is a struct-like data structure containing zero or more properties. Appscript represents AE records as Ruby hashes. The keys in this hash are usually symbols representing appscript-style property names, although they may also be <code>AEM::AEType</code> values or strings.</p>
162
+
163
+ <p>If a hash contains a <code>:class_</code> (or <code>AEM::AEType.new('pcls')</code>) key, appscript will pack the remaining items into an AE record, then coerce it to the type specified by this 'class' property. Similarly, when unpacking an record-like AEDesc with a custom type code, appscript will unpack it as a hash with the type represented by a <code>:class_</code> entry.</p>
164
+
165
+
166
+ <h4>Types and enumerators</h4>
167
+
168
+ <p>The <code>typeType</code> and <code>typeEnumerated</code> AE types are unpacked as Ruby symbols when the corresponding terminology is available, otherwise they are unpacked as <code>AEM::AEType</code> and <code>AEM::AEEnum</code> respectively.</p>
169
+
170
+
171
+ <h4>Unit types</h4>
172
+
173
+ <p>Unit type values are represented by instances of the <code>MacTypes::Units</code> class, e.g. <code>MacTypes::Units.inches(3.0)</code>. See the <a href="../mactypes-manual/index.html">mactypes manual</a> for more information.</p>
174
+
175
+
176
+ <h4>Miscellaneous types</h4>
177
+
178
+ <p>The Apple Event Manager defines many other AE types whose names and codes are defined by appscript for completeness. A few of these types are of occasional interest to users, the rest can simply be ignored. In most cases, values of these types will be represented by raw <code>AE::AEDesc</code> instances as appscript doesn't automatically convert them to native objects.</p>
179
+
180
+
181
+ </div>
182
+
183
+ <!-- bottom navigation -->
184
+
185
+ <div class="footer">
186
+ <a href="05_keywordconversion.html">Previous</a> &bull;
187
+ <a href="index.html">Up</a> &bull;
188
+ <a href="07_applicationobjects.html">Next</a>
189
+ </div>
190
+
191
+ </body>
192
+ </html>
@@ -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>