rubyosa 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/src/rbosa.h ADDED
@@ -0,0 +1,51 @@
1
+ /*
2
+ * Copyright (c) 2006, Apple Computer, Inc. All rights reserved.
3
+ *
4
+ * Redistribution and use in source and binary forms, with or without
5
+ * modification, are permitted provided that the following conditions
6
+ * are met:
7
+ * 1. Redistributions of source code must retain the above copyright
8
+ * notice, this list of conditions and the following disclaimer.
9
+ * 2. Redistributions in binary form must reproduce the above copyright
10
+ * notice, this list of conditions and the following disclaimer in the
11
+ * documentation and/or other materials provided with the distribution.
12
+ * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
13
+ * its contributors may be used to endorse or promote products derived
14
+ * from this software without specific prior written permission.
15
+ *
16
+ * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
17
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
+ * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
20
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
+ * POSSIBILITY OF SUCH DAMAGE.
27
+ */
28
+
29
+ #ifndef __RBOSA_H_
30
+ #define __RBOSA_H_
31
+
32
+ #include "osx_ruby.h"
33
+ #include <Carbon/Carbon.h>
34
+ #include <sys/param.h>
35
+
36
+ /* rbosa_sdef.c */
37
+ VALUE rbosa_scripting_info (VALUE self, VALUE criterion, VALUE value);
38
+
39
+ /* rbosa_conv.c */
40
+ FourCharCode rbobj_to_fourchar (VALUE obj);
41
+ VALUE rbosa_four_char_code (VALUE self, VALUE val);
42
+ void rbobj_to_alias_handle (VALUE obj, AliasHandle *alias);
43
+
44
+ /* helper macros */
45
+ #define RVAL2CSTR(x) (StringValueCStr (x))
46
+ #define CSTR2RVAL(x) (rb_str_new2 (x))
47
+ #define RVAL2CBOOL(x) (RTEST(x))
48
+ #define CBOOL2RVAL(x) (x ? Qtrue : Qfalse)
49
+ #define RVAL2FOURCHAR(x) (rbobj_to_fourchar(x))
50
+
51
+ #endif /* __RBOSA_H_ */
data/src/rbosa_conv.c ADDED
@@ -0,0 +1,97 @@
1
+ /*
2
+ * Copyright (c) 2006, Apple Computer, Inc. All rights reserved.
3
+ *
4
+ * Redistribution and use in source and binary forms, with or without
5
+ * modification, are permitted provided that the following conditions
6
+ * are met:
7
+ * 1. Redistributions of source code must retain the above copyright
8
+ * notice, this list of conditions and the following disclaimer.
9
+ * 2. Redistributions in binary form must reproduce the above copyright
10
+ * notice, this list of conditions and the following disclaimer in the
11
+ * documentation and/or other materials provided with the distribution.
12
+ * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
13
+ * its contributors may be used to endorse or promote products derived
14
+ * from this software without specific prior written permission.
15
+ *
16
+ * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
17
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
+ * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
20
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
+ * POSSIBILITY OF SUCH DAMAGE.
27
+ */
28
+
29
+ #include "rbosa.h"
30
+
31
+ FourCharCode
32
+ rbobj_to_fourchar (VALUE obj)
33
+ {
34
+ FourCharCode result = 0;
35
+
36
+ #define USAGE_MSG "requires 4 length size string/symbol or integer"
37
+
38
+ if (rb_obj_is_kind_of (obj, rb_cInteger)) {
39
+ result = NUM2UINT (obj);
40
+ }
41
+ else {
42
+ if (rb_obj_is_kind_of (obj, rb_cSymbol))
43
+ obj = rb_obj_as_string (obj);
44
+
45
+ if (rb_obj_is_kind_of (obj, rb_cString)) {
46
+ if (RSTRING (obj)->len != 4)
47
+ rb_raise (rb_eArgError, USAGE_MSG);
48
+ result = *(FourCharCode*)(RSTRING (obj)->ptr);
49
+ result = CFSwapInt32HostToBig (result);
50
+ }
51
+ else {
52
+ rb_raise (rb_eArgError, USAGE_MSG);
53
+ }
54
+ }
55
+
56
+ #undef USAGE_MSG
57
+
58
+ return result;
59
+ }
60
+
61
+ VALUE
62
+ rbosa_four_char_code (VALUE self, VALUE val)
63
+ {
64
+ return INT2NUM (RVAL2FOURCHAR (val));
65
+ }
66
+
67
+ void
68
+ rbobj_to_alias_handle (VALUE obj, AliasHandle *alias)
69
+ {
70
+ FSRef ref;
71
+ CFURLRef URL;
72
+ Boolean ok;
73
+ OSErr error;
74
+
75
+ Check_Type (obj, T_STRING);
76
+ *alias = NULL;
77
+
78
+ URL = CFURLCreateFromFileSystemRepresentation (kCFAllocatorDefault,
79
+ (const UInt8 *)RSTRING (obj)->ptr,
80
+ RSTRING (obj)->len,
81
+ 0 /* XXX: normally passing 0 even if it's a directory should
82
+ not hurt, as we are just getting the FSRef. */);
83
+ if (URL == NULL)
84
+ rb_raise (rb_eArgError, "Invalid path given");
85
+ ok = CFURLGetFSRef (URL, &ref);
86
+ CFRelease (URL);
87
+ if (ok) {
88
+ error = FSNewAlias (NULL, &ref, alias);
89
+ if (error != noErr)
90
+ rb_raise (rb_eArgError, "Cannot create alias handle for given filename '%s' : %s (%d)",
91
+ RSTRING (obj)->ptr, GetMacOSStatusErrorString (error), error);
92
+ }
93
+ else {
94
+ rb_raise (rb_eArgError, "Cannot obtain the filesystem reference for given filename '%s'",
95
+ RSTRING (obj)->ptr);
96
+ }
97
+ }
data/src/rbosa_sdef.c ADDED
@@ -0,0 +1,150 @@
1
+ /*
2
+ * Copyright (c) 2006, Apple Computer, Inc. All rights reserved.
3
+ *
4
+ * Redistribution and use in source and binary forms, with or without
5
+ * modification, are permitted provided that the following conditions
6
+ * are met:
7
+ * 1. Redistributions of source code must retain the above copyright
8
+ * notice, this list of conditions and the following disclaimer.
9
+ * 2. Redistributions in binary form must reproduce the above copyright
10
+ * notice, this list of conditions and the following disclaimer in the
11
+ * documentation and/or other materials provided with the distribution.
12
+ * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
13
+ * its contributors may be used to endorse or promote products derived
14
+ * from this software without specific prior written permission.
15
+ *
16
+ * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
17
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
+ * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
20
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
+ * POSSIBILITY OF SUCH DAMAGE.
27
+ */
28
+
29
+ #include <ApplicationServices/ApplicationServices.h>
30
+ #include <CoreFoundation/CoreFoundation.h>
31
+ #include <unistd.h>
32
+ #include "rbosa.h"
33
+
34
+ static void
35
+ rbosa_app_name_signature (CFURLRef URL, VALUE *name, VALUE *signature)
36
+ {
37
+ CFBundleRef bundle;
38
+ CFDictionaryRef info;
39
+ CFStringRef str;
40
+
41
+ bundle = CFBundleCreate (kCFAllocatorDefault, URL);
42
+ info = CFBundleGetInfoDictionary (bundle);
43
+
44
+ if (NIL_P(*name)) {
45
+ str = CFDictionaryGetValue (info, CFSTR ("CFBundleName"));
46
+ if (str == NULL) {
47
+ /* Try 'CFBundleExecutable' if 'CFBundleName' does not exist (which is a bug). */
48
+ str = CFDictionaryGetValue (info, CFSTR ("CFBundleExecutable"));
49
+ }
50
+ *name = str != NULL ? CSTR2RVAL (CFStringGetCStringPtr (str, CFStringGetFastestEncoding (str))) : Qnil;
51
+ }
52
+ if (NIL_P(*signature)) {
53
+ str = CFDictionaryGetValue (info, CFSTR ("CFBundleSignature"));
54
+ *signature = str != NULL ? CSTR2RVAL (CFStringGetCStringPtr (str, CFStringGetFastestEncoding (str))) : Qnil;
55
+ }
56
+
57
+ CFRelease (bundle);
58
+ }
59
+
60
+ static bool
61
+ rbosa_translate_app (VALUE criterion, VALUE value, VALUE *app_signature, VALUE *app_name, FSRef *fs_ref, const char **error)
62
+ {
63
+ OSStatus err;
64
+ CFURLRef URL;
65
+
66
+ *app_name = Qnil;
67
+ *app_signature = Qnil;
68
+ err = noErr;
69
+
70
+ if (criterion == ID2SYM (rb_intern ("by_signature"))) {
71
+ err = LSFindApplicationForInfo (RVAL2FOURCHAR (value), NULL, NULL, fs_ref, &URL);
72
+ *app_signature = value; /* Don't need to get the app signature, we already have it. */
73
+ }
74
+ else {
75
+ CFMutableStringRef str;
76
+
77
+ str = CFStringCreateMutable (kCFAllocatorDefault, 0);
78
+ CFStringAppendCString (str, RVAL2CSTR (value), kCFStringEncodingUTF8);
79
+
80
+ if (criterion == ID2SYM (rb_intern ("by_path"))) {
81
+ err = FSPathMakeRef ((const UInt8 *)RVAL2CSTR (value), fs_ref, NULL);
82
+ if (err == noErr) {
83
+ URL = CFURLCreateWithFileSystemPath (kCFAllocatorDefault, str, kCFURLPOSIXPathStyle, FALSE);
84
+ }
85
+ }
86
+ else if (criterion == ID2SYM (rb_intern ("by_name"))) {
87
+ CFStringRef dot_app;
88
+
89
+ dot_app = CFSTR (".app");
90
+ if (!CFStringHasSuffix (str, dot_app))
91
+ CFStringAppend (str, dot_app);
92
+
93
+ err = LSFindApplicationForInfo (kLSUnknownCreator, NULL, str, fs_ref, &URL);
94
+ *app_name = value; /* Don't need to get the app name, we already have it. */
95
+ }
96
+ else if (criterion == ID2SYM (rb_intern ("by_bundle_id"))) {
97
+ err = LSFindApplicationForInfo (kLSUnknownCreator, str, NULL, fs_ref, &URL);
98
+ }
99
+ else {
100
+ *error = "Invalid criterion";
101
+ CFRelease (str);
102
+ return FALSE;
103
+ }
104
+
105
+ CFRelease (str);
106
+ }
107
+
108
+ if (err != noErr) {
109
+ *error = "Error when translating the application";
110
+ return FALSE;
111
+ }
112
+
113
+ rbosa_app_name_signature (URL, app_name, app_signature);
114
+
115
+ CFRelease (URL);
116
+
117
+ if (NIL_P (*app_signature)) {
118
+ *error = "Error when getting the application signature";
119
+ return FALSE;
120
+ }
121
+
122
+ return TRUE;
123
+ }
124
+
125
+ VALUE
126
+ rbosa_scripting_info (VALUE self, VALUE criterion, VALUE value)
127
+ {
128
+ const char * error;
129
+ VALUE ary;
130
+ VALUE name;
131
+ VALUE signature;
132
+ FSRef fs;
133
+ OSAError osa_error;
134
+ CFDataRef sdef_data;
135
+
136
+ if (!rbosa_translate_app (criterion, value, &signature, &name, &fs, &error))
137
+ rb_raise (rb_eRuntimeError, error);
138
+
139
+ osa_error = OSACopyScriptingDefinition (&fs, kOSAModeNull, &sdef_data);
140
+ if (osa_error != noErr)
141
+ rb_raise (rb_eRuntimeError, "Cannot get scripting definition : error %d", osa_error);
142
+
143
+ ary = rb_ary_new3 (3, name, signature,
144
+ rb_str_new ((const char *)CFDataGetBytePtr (sdef_data),
145
+ CFDataGetLength (sdef_data)));
146
+
147
+ CFRelease (sdef_data);
148
+
149
+ return ary;
150
+ }
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: rubyosa
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2006-10-25 00:00:00 +02:00
8
+ summary: A Ruby/AppleEvent bridge.
9
+ require_paths:
10
+ - lib
11
+ email: lsansonetti@apple.com
12
+ homepage: http://rubyosa.rubyforge.org
13
+ rubyforge_project: rubyosa
14
+ description: RubyOSA is a bridge that connects Ruby to the Apple Event Manager, automatically populating the API according to the target application's scriptable definition.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: false
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Laurent Sansonetti
31
+ files:
32
+ - README
33
+ - COPYRIGHT
34
+ - AUTHORS
35
+ - extconf.rb
36
+ - src/rbosa.c
37
+ - src/rbosa.h
38
+ - src/rbosa_conv.c
39
+ - src/rbosa_sdef.c
40
+ - src/lib/rbosa.rb
41
+ - sample/Finder_show_desktop.rb
42
+ - sample/iChat_uptime.rb
43
+ - sample/iTunes_control.rb
44
+ - sample/iTunes_fade_volume.rb
45
+ - sample/iTunes_inspect.rb
46
+ - sample/QT_playall.rb
47
+ - sample/sdef.rb
48
+ test_files: []
49
+
50
+ rdoc_options: []
51
+
52
+ extra_rdoc_files: []
53
+
54
+ executables:
55
+ - rdoc-osa
56
+ extensions:
57
+ - extconf.rb
58
+ requirements: []
59
+
60
+ dependencies:
61
+ - !ruby/object:Gem::Dependency
62
+ name: libxml-ruby
63
+ version_requirement:
64
+ version_requirements: !ruby/object:Gem::Version::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 0.3.8
69
+ version: