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,94 @@
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-aem manual | 8. Examples</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_findapp.html">Previous</a> &bull;
18
+ <a href="index.html">Up</a>
19
+
20
+ <span>
21
+ <a href="../appscript-manual/index.html">appscript</a> /
22
+ <a href="../mactypes-manual/index.html">mactypes</a> /
23
+ <a href="../osax-manual/index.html">osax</a> /
24
+ <strong><a href="../aem-manual/index.html">aem</a></strong>
25
+ </span>
26
+ </div>
27
+
28
+ <!-- content -->
29
+ <div id="content">
30
+
31
+ <h2>8. Examples</h2>
32
+
33
+ <h3>Identifying applications</h3>
34
+
35
+ <pre><code># application "Macintosh HD:Applications:TextEdit.app"
36
+ textedit = AEM::Application.by_path('/Applications/TextEdit.app')
37
+
38
+ # application "TextEdit"
39
+ textedit = AEM::Application.by_path(FindApp.by_name('TextEdit'))
40
+
41
+ # application "TextEdit" of machine "eppc://my-mac.local"
42
+ textedit = AEM::Application.by_url('eppc://my-mac.local/TextEdit')</code></pre>
43
+
44
+
45
+ <h3>Building references</h3>
46
+
47
+ <pre><code># name (of application)
48
+ AEM.app.property('pnam')
49
+
50
+ # text of every document
51
+ AEM.app.elements('docu').property('ctxt')
52
+
53
+ # end of every paragraph of text of document 1
54
+ AEM.app.elements('docu').by_index(1).property('ctxt').elements('cpar').end
55
+
56
+ # paragraphs 2 thru last of first document
57
+ AEM.app.elements('docu').first.elements('cpar').by_range(
58
+ AEM.con.elements('cpar').by_index(2),
59
+ AEM.con.elements('cpar').last)
60
+
61
+ # paragraphs of document 1 where it != "\n"
62
+ AEM.app.elements('docu').by_index(1).elements('cpar').by_filter(AEM.its.ne("\n"))</code></pre>
63
+
64
+
65
+ <h3>Sending events</h3>
66
+
67
+ <pre><code># quit TextEdit
68
+ textedit.event('aevtquit').send
69
+
70
+ # name of TextEdit
71
+ p textedit.event('coregetd', {'----' =&gt; AEM.app.property('pnam')}).send
72
+
73
+ # count documents of TextEdit
74
+ p textedit.event('corecnte', {'----' =&gt; AEM.app.elements('docu')}).send
75
+
76
+ # make new document at end of documents of TextEdit
77
+ textedit.event('corecrel', {
78
+ 'kocl' =&gt; AEM::AEType.new('docu'),
79
+ 'insh' =&gt; AEM.app.elements('docu').end
80
+ }).send</code></pre>
81
+
82
+
83
+
84
+ </div>
85
+
86
+ <!-- bottom navigation -->
87
+
88
+ <div class="footer">
89
+ <a href="07_findapp.html">Previous</a> &bull;
90
+ <a href="index.html">Up</a>
91
+ </div>
92
+
93
+ </body>
94
+ </html>
@@ -0,0 +1,56 @@
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-aem manual | Contents</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="../index.html">Previous</a> &bull;
18
+ <a href="../index.html">Up</a> &bull;
19
+ <a href="01_introduction.html">Next</a>
20
+
21
+ <span>
22
+ <a href="../appscript-manual/index.html">appscript</a> /
23
+ <a href="../mactypes-manual/index.html">mactypes</a> /
24
+ <a href="../osax-manual/index.html">osax</a> /
25
+ <strong><a href="../aem-manual/index.html">aem</a></strong>
26
+ </span>
27
+ </div>
28
+
29
+ <!-- content -->
30
+ <div id="content">
31
+
32
+ <h2>Contents</h2>
33
+
34
+ <ol>
35
+ <li><a href="01_introduction.html">Introduction</a></li>
36
+ <li><a href="02_apioverview.html">API overview</a></li>
37
+ <li><a href="03_packingandunpackingdata.html">Packing and unpacking data</a></li>
38
+ <li><a href="04_references.html">References</a></li>
39
+ <li><a href="05_targetingapplications.html">Targeting applications</a></li>
40
+ <li><a href="06_buildingandsendingevents.html">Building and sending events</a></li>
41
+ <li><a href="07_findapp.html">Locating applications</a></li>
42
+ <li><a href="08_examples.html">Examples</a></li>
43
+ </ol>
44
+
45
+ </div>
46
+
47
+ <!-- bottom navigation -->
48
+
49
+ <div class="footer">
50
+ <a href="../index.html">Previous</a> &bull;
51
+ <a href="../index.html">Up</a> &bull;
52
+ <a href="01_introduction.html">Next</a>
53
+ </div>
54
+
55
+ </body>
56
+ </html>
@@ -0,0 +1,94 @@
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 | 1. Introduction</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="index.html">Previous</a> &bull;
18
+ <a href="index.html">Up</a> &bull;
19
+ <a href="02_aboutappscripting.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>1. Introduction</h2>
33
+
34
+ <h3>What is appscript?</h3>
35
+
36
+ <p>Ruby appscript (rb-appscript) is an easy-to-use Apple event bridge that allows 'AppleScriptable' applications to be controlled by ordinary Ruby scripts. Appscript makes Ruby an excellent alternative to Apple's own AppleScript language for automating your Mac.</p>
37
+
38
+ <p>For example, to get the value of the first paragraph of the topmost document in TextEdit:</p>
39
+
40
+ <pre><code>app('TextEdit').documents[1].paragraphs[1].get</code></pre>
41
+
42
+ <p>This is equivalent to the AppleScript statement:</p>
43
+
44
+ <pre><code>tell application "TextEdit"
45
+ get paragraph 1 of document 1
46
+ end tell</code></pre>
47
+
48
+
49
+ <p>The following script uses appscript to create a new "Hello World!" document in TextEdit:</p>
50
+
51
+ <pre><code>#!/usr/bin/env ruby
52
+
53
+ require "appscript"
54
+ include Appscript
55
+
56
+ app('TextEdit').documents.end.make(
57
+ :new => :document,
58
+ :with_properties => {:text => "Hello World!\n"}
59
+ )</code></pre>
60
+
61
+
62
+
63
+ <h3>Before you start...</h3>
64
+
65
+ <p>In order to use appscript effectively, you will need to understand the differences between the Apple event and Ruby object systems.</p>
66
+
67
+ <p>In contrast to the familiar object-oriented approach of other inter-process communication systems such as COM and Distributed Objects, Apple event IPC is based on a combination of <em>remote procedure calls</em> and <em>first-class queries</em> - somewhat analogous to using XPath over XML-RPC.</p>
68
+
69
+ <p>While appscript uses an object-oriented-like syntax for conciseness and readability, like AppleScript, it behaves according to Apple event rules. As a result, Ruby users will discover that some things work differently in appscript from what they're used to. For example:</p>
70
+
71
+ <ul>
72
+ <li>object elements are one-indexed, not zero-indexed like Ruby arrays</li>
73
+
74
+ <li>referencing a property of an application object does not automatically return the property's value (you need a <code>get</code> command for that)</li>
75
+
76
+ <li>many applications allow a single command to operate on multiple objects at the same time, providing significant performance benefits when manipulating large numbers of application objects.</li>
77
+
78
+ </ul>
79
+
80
+ <p>Chapters 2 and 3 of this manual provide further information on how Apple event IPC works and a tutorial-based introduction to the Ruby appscript bridge. Chapter 4 describes various ways of getting help when scripting applications. Chapters 5 through 12 cover the appscript API, and chapter 13 discusses techniques for optimising performance.</p>
81
+
82
+
83
+ </div>
84
+
85
+ <!-- bottom navigation -->
86
+
87
+ <div class="footer">
88
+ <a href="index.html">Previous</a> &bull;
89
+ <a href="index.html">Up</a> &bull;
90
+ <a href="02_aboutappscripting.html">Next</a>
91
+ </div>
92
+
93
+ </body>
94
+ </html>
@@ -0,0 +1,247 @@
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 | 2. About Application Scripting</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="01_introduction.html">Previous</a> &bull;
18
+ <a href="index.html">Up</a> &bull;
19
+ <a href="03_quicktutorial.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>2. About Application Scripting</h2>
33
+
34
+ <p>This chapter introduces the main concepts behind Apple event-based application scripting. However, if you just can't wait to begin using appscript, you can skip to the tutorial in <a href="03_quicktutorial.html">chapter 3</a> and return to this chapter later on.</p>
35
+
36
+ <h3>What are Apple events?</h3>
37
+
38
+ <p>Apple events are a high-level message-based form of Interprocess Communication (IPC; also known as Interapplication Communication, or IAC), used to communicate between local or remote application processes (and, in some cases, within the same process).</p>
39
+
40
+ <p>An Apple event contains typed data describing:</p>
41
+
42
+ <ul>
43
+ <li>how the event should be handled, such as the event's 'name' (specified by two four-letter codes [<a href="#f1">1</a>]) and whether or not a reply is required</li>
44
+
45
+ <li>any data to be passed as arguments to the event handler that receives the event.</li>
46
+ </ul>
47
+
48
+ <p>For descriptive purposes, the former are referred to as 'attributes' and the latter as 'parameters'.</p>
49
+
50
+ <p>Apple event datatypes include:</p>
51
+
52
+ <ul>
53
+ <li>common scalar types such as booleans, integers, floats, strings, dates and file references</li>
54
+
55
+ <li>ordered lists</li>
56
+
57
+ <li>records (key-value lists where each key is an four-letter code)</li>
58
+
59
+ <li>object specifiers, used to construct <em>first-class queries</em>, commonly referred to as application references, that identify objects within an application. These query objects are roughly comparable to XPath or CSS selectors.</li>
60
+ </ul>
61
+
62
+ <p>For example, when the user drag-n-drops a file onto TextEdit.app in the Finder, the Finder commands TextEdit to open that file by sending it an <code>aevt/odoc</code> event with a file reference as its main parameter:</p>
63
+
64
+ <p><img src="finder_to_textedit_event.gif" alt="Sending Apple event from Finder to TextEdit" /></p>
65
+
66
+ <p>With suitable bindings, scripting languages can also create and send Apple events. For example, when the code <code>app('iTunes').play</code> is executed in a Ruby interpreter, a <code>hook/Play</code> event is sent from the interpreter to iTunes, instructing it to start playing:</p>
67
+
68
+ <p><img src="ruby_to_itunes_event.gif" alt="Sending Apple event from Ruby to iTunes"/></p>
69
+
70
+ <p>Applications may respond to an incoming Apple event by sending a reply event back to the client application. The reply event may contain either a return value, if there is one, or an error description if it was unable to handle the event as requested. For example, executing the command <code>app('TextEdit').name.get</code> in a Ruby interpreter sends TextEdit a <code>core/getd</code> event containing an object specifier identifying the <code>name</code> property of its root <code>application</code> object. TextEdit processes this event, then sends a reply event containing the string '<tt>TextEdit</tt>' back to the Ruby interpreter, where it is displayed as the command's result. This exchange is usually performed synchronously, appearing to the user as a simple remote procedure call. Asynchronous messaging is also supported, though is not normally used in application scripting.</p>
71
+
72
+
73
+ <h3>What is a scriptable application?</h3>
74
+
75
+ <p>A scriptable (or 'AppleScriptable') application is an application that provides an Apple event interface intended for third-party (e.g. end-user) use. The application implements one or more event handlers that respond to corresponding events, and may also support the Apple Event Object Model. While this interface may be regarded as an API, the emphasis is on providing a high-level <em>user interface</em> that is peer to other users interfaces the application may have (GUI, CLI, web, etc.) and accessible to end-users as much as developers.</p>
76
+
77
+ <p>For example, iTunes.app implements two user interfaces, one graphical and one Apple event-based, that provide access to to much the same functionality but in very different ways:</p>
78
+
79
+ <p><img src="application_architecture.gif" alt="Application with Graphical and Apple event interfaces." /></p>
80
+
81
+ <p>A scriptable application also contains a built-in definition of its scripting interface in the form of an <code>aete</code> or <code>sdef</code> resource. This resource can be obtained programmatically and used to:</p>
82
+
83
+ <ul>
84
+ <li>support automatic translation of human-readable terminology to four-letter codes in high-level bridges such as AppleScript and appscript</li>
85
+
86
+ <li>generate basic human-readable documentation by applications such as AppleScript Editor and ASDictionary.</li>
87
+ </ul>
88
+
89
+ <p>(Note that the <code>aete</code> and <code>sdef</code> formats do not provide an exhaustive description of the application's scripting interface, and additional documentation is usually required - if not always provided - to form a complete understanding of that interface and how to use it effectively.)</p>
90
+
91
+
92
+ <h3>What is the Apple Event Object Model?</h3>
93
+
94
+ <p>The Apple Event Object Model (AEOM) is an idealised, user-oriented representation of the application's internal data model, allowing clients to identify and manipulate parts of that structure via Apple events. An incoming Apple event representing a particular command (get, set, move, etc.) is unpacked, and any object specifiers in its parameter list are evaluated against the application's AEOM to identify the user-level object(s) upon which the command should act. The command is then applied these objects, with the AEOM translating this into operations upon the application's implementation-level objects. These implementation-level objects are mostly user-data objects in the application's Model layer, plus a few GUI objects of interest to scripters (such as those representing document windows). The internal architecture of a typical scriptable desktop application might look something like this:</p>
95
+
96
+ <p><img src="application_architecture2.gif" alt="Internal architecture of application with Graphical and Apple event interfaces." /></p>
97
+
98
+ <div class="hilitebox">
99
+ <p>Note: while the Apple Event Object Model is sometimes described by third-parties as being similar to DOM, this is inaccurate as AEOM operates at a much higher level of abstraction than DOM.</p>
100
+
101
+ <ul>
102
+ <li>AEOM objects are identified by high-level queries (comparable to XPath or CSS selectors), not low-level chained method calls.</li>
103
+
104
+ <li>Commands operate upon objects, so a single command may invoke multiple method calls upon multiple implementation objects in order to perform relatively complex tasks.</li>
105
+
106
+ <li>Where a query specifies multiple objects, the command should perform the same action on each of them [<a href="#f2">2</a>].</li>
107
+
108
+ <li>AEOM objects never move across the bridge. Where a command identifies one or more AEOM objects as its result, the return value is a query (or queries) that will [hopefully] identify those objects in future, not the AEOM objects themselves.</li>
109
+ </ul>
110
+ </div>
111
+
112
+
113
+ <h3>How does the AEOM work?</h3>
114
+
115
+ <p>The AEOM is a tree structure made up of objects. These objects may have attributes (descriptive values such as class, name, id, size, bounds; usually primitive AE types but occasionally other application objects), e.g.:</p>
116
+
117
+ <pre><code>app('Finder').name
118
+ app('Finder').version
119
+ app('Finder').Finder_preferences</code></pre>
120
+
121
+ <p>and may 'contain' other objects, e.g.:</p>
122
+
123
+ <pre><code>app('Finder').Finder_windows
124
+ app('TextEdit').documents</code></pre>
125
+
126
+ <p>However, unlike other object models such as DOM, objects within the AEOM are associated with one another by <em>relationships</em> rather than simple physical containment. Think of AEOM as combining aspects of procedural RPC, object-oriented object model and relational database mechanics.</p>
127
+
128
+ <p>Relationships between objects may be one-to-one, e.g.:</p>
129
+
130
+ <pre><code>app('Finder').home
131
+ app('iTunes').current_track</code></pre>
132
+
133
+ <p>or one-to-many, e.g.:</p>
134
+
135
+ <pre><code>app('Finder').folders</code></pre>
136
+
137
+ <p>While relationships often follow the containment structure of the underlying data structures, e.g.</p>
138
+
139
+ <pre><code>app('TextEdit').documents</code></pre>
140
+
141
+ <p>this is not always the case. For example:</p>
142
+
143
+ <pre><code>app('Finder').files
144
+ app('Finder').desktop.files
145
+ app('Finder').disks['MacHD'].folders['Users'].folders['Jo'].folders['Desktop'].files</code></pre>
146
+
147
+ <p>would all identify the same objects (files on the user's desktop), though only one of these references describes their position according to physical containment.</p>
148
+
149
+ <p>Some references may identify different objects at different times, according to changes in the application's state, e.g.:</p>
150
+
151
+ <pre><code>app('iTunes').current_track</code></pre>
152
+
153
+ <p>References may identify objects that do not actually exist as discreet entities within the application's underlying data structures, but are interpreted on the fly as proxies to the relevant portions of implementation-level data structures, e.g.:</p>
154
+
155
+ <pre><code>app('TextEdit').documents[1].text.characters
156
+ app('TextEdit').documents[1].text.words
157
+ app('TextEdit').documents[1].text.paragraphs</code></pre>
158
+
159
+ <p>all refer to sections of data that's actually stored in a single <code>NSTextStorage</code> object within TextEdit's Model layer. This decoupling of the AEOM from the Model layer's structure allows applications to present data in a way that is convenient to the user, i.e. easy and intuitive to understand and use.</p>
160
+
161
+ <p>Finally, one-to-many relationships may be selective in identifying a subset of related elements according to their individual class or shared superclasses. For example:</p>
162
+
163
+ <pre><code>app('Finder').items</code></pre>
164
+
165
+ <p>identifies all objects that are a subclass of class 'item' (i.e. disks, folders, document files, alias files, etc.).</p>
166
+
167
+ <pre><code>app('Finder').files</code></pre>
168
+
169
+ <p>identifies all objects that are a subclass of class 'file' (i.e. document files, alias files, etc.).</p>
170
+
171
+ <pre><code>app('Finder').document_files</code></pre>
172
+
173
+ <p>identifies all objects of class 'document file' only.</p>
174
+
175
+ <div class="hilitebox">
176
+
177
+ <p>Understanding the structure of an application's AEOM is key to successfully manipulating it. To illustrate the above concepts, here is the AEOM for a simple hypothetical text editor:</p>
178
+
179
+ <p><img src="relationships_example.gif" alt="AEOM relationships in an simple text editor." /></p>
180
+
181
+ <p>The program has an application object as its root, which in turn has one-to-many relationships with its document and window objects.</p>
182
+
183
+ <p>Each document object has one-to-many relationships to the characters, words and paragraphs of the text it contains, each of which in turn has one-to-many relationships to the characters, words and paragraphs of the text it contains, and so on to infinity.</p>
184
+
185
+ <p>Finally, each window object has a one-to-one relationship to the document object whose content it displays.</p>
186
+
187
+ </div>
188
+
189
+ <h3>What is appscript?</h3>
190
+
191
+ <p>Appscript is a high-level Ruby-to-Apple Event Manager bridge, intended for use by both developers and end-users. The appscript architecture consists of three layers:</p>
192
+
193
+ <dl>
194
+ <dt><code>AE</code></dt><dd>A low-level, mostly procedural Ruby wrapper around Mac OS X's Apple Event Manager API.</dd>
195
+
196
+ <dt><code>AEM</code></dt><dd>A mid-level wrapper around <code>AE</code>, providing an object-oriented API for building relational AEOM queries and dispatching events.</dd>
197
+
198
+ <dt><code>Appscript</code></dt><dd>A high-level wrapper around <code>AEM</code>, providing automatic translation between human-readable application terminology and corresponding four-letter codes, and representing relational AEOM queries in an OO-like syntax for ease of use.</dd>
199
+ </dl>
200
+
201
+ <p>The <code>AE</code> extension primarily serves as a foundation for higher-level libraries to build on; it's rarely used directly as it takes a significant amount of code to perform all but the most trivial of tasks. The <code>AEM</code> module is largely intended for use by higher-level libraries and developers, though may also be used by end-users in cases where an application lacks terminology, or bugs within its terminology prevent its use by appscript. The <code>Appscript</code> module is intended for use by both developers and end-users, though developers may prefer aem for certain tasks as appscript doesn't expose all aspects of the aem API (such as the ability to do asynchronous messaging) and terminology translation imposes additional overheads and dependencies on client code.</p>
202
+
203
+ <p>For example, to set the size of the first character of every non-empty paragraph in every document of TextEdit to 24 pt:</p>
204
+
205
+ <ul>
206
+ <li>using aem:
207
+
208
+ <pre><code>AEM::Application.by_path('/Applications/TextEdit.app').event('coregetd', {
209
+ '----' =&gt; AEM.app.elements('docu').property('ctxt').elements('cpar'). \
210
+ by_filter(AEM.its.ne("\n")).elements('cha ').by_index(1).property('ptsz'),
211
+ 'data': 24
212
+ }).send</code></pre>
213
+ </li>
214
+
215
+ <li>using appscript:
216
+
217
+ <pre><code>app('TextEdit').documents.text.paragraphs[
218
+ its.ne("\n")].characters[1].size.set(24)</code></pre>
219
+ </li>
220
+ </ul>
221
+
222
+ <!--(AE equivalent not shown due to sheer length and ugliness.)-->
223
+
224
+
225
+
226
+ <h3>Additional resources</h3>
227
+
228
+ <p>Background information links can be found on the <a href="http://appscript.sourceforge.net">appscript website</a>.</p>
229
+
230
+ <hr />
231
+
232
+ <p><a name="f1"></a>[1] Really an <code>OSType</code>: a 32-bit code, often represented as a 4-character string. Used in Carbon APIs such as the Apple Event Manager. Mnemonic values are preferred, e.g. '<tt>docu</tt>' = 'document'.</p>
233
+
234
+ <p><a name="f2"></a>[2] Assuming a well-implemented AEOM; in practice most AEOM implementations suffer varying degrees of limitations in their ability to operate successfully on complex multi-object references. These limitations are generally not documented but discovered through trial and error.</p>
235
+
236
+ </div>
237
+
238
+ <!-- bottom navigation -->
239
+
240
+ <div class="footer">
241
+ <a href="01_introduction.html">Previous</a> &bull;
242
+ <a href="index.html">Up</a> &bull;
243
+ <a href="03_quicktutorial.html">Next</a>
244
+ </div>
245
+
246
+ </body>
247
+ </html>