1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import "chrome/browser/ui/cocoa/applescript/apple_event_util.h"
7 #import <Carbon/Carbon.h>
9 #include "base/logging.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "base/values.h"
16 NSAppleEventDescriptor* ValueToAppleEventDescriptor(const base::Value* value) {
17 NSAppleEventDescriptor* descriptor = nil;
19 switch (value->GetType()) {
20 case base::Value::TYPE_NULL:
21 descriptor = [NSAppleEventDescriptor
22 descriptorWithTypeCode:cMissingValue];
25 case base::Value::TYPE_BOOLEAN: {
27 value->GetAsBoolean(&bool_value);
28 descriptor = [NSAppleEventDescriptor descriptorWithBoolean:bool_value];
32 case base::Value::TYPE_INTEGER: {
34 value->GetAsInteger(&int_value);
35 descriptor = [NSAppleEventDescriptor descriptorWithInt32:int_value];
39 case base::Value::TYPE_DOUBLE: {
41 value->GetAsDouble(&double_value);
42 descriptor = [NSAppleEventDescriptor
43 descriptorWithDescriptorType:typeIEEE64BitFloatingPoint
45 length:sizeof(double_value)];
49 case base::Value::TYPE_STRING: {
50 std::string string_value;
51 value->GetAsString(&string_value);
52 descriptor = [NSAppleEventDescriptor descriptorWithString:
53 base::SysUTF8ToNSString(string_value)];
57 case base::Value::TYPE_BINARY:
61 case base::Value::TYPE_DICTIONARY: {
62 const base::DictionaryValue* dictionary_value;
63 value->GetAsDictionary(&dictionary_value);
64 descriptor = [NSAppleEventDescriptor recordDescriptor];
65 NSAppleEventDescriptor* userRecord = [NSAppleEventDescriptor
67 for (base::DictionaryValue::Iterator iter(*dictionary_value);
70 [userRecord insertDescriptor:[NSAppleEventDescriptor
71 descriptorWithString:base::SysUTF8ToNSString(iter.key())]
73 [userRecord insertDescriptor:ValueToAppleEventDescriptor(&iter.value())
76 // Description of what keyASUserRecordFields does.
77 // http://lists.apple.com/archives/cocoa-dev/2009/Jul/msg01216.html
78 [descriptor setDescriptor:userRecord forKeyword:keyASUserRecordFields];
82 case base::Value::TYPE_LIST: {
83 const base::ListValue* list_value;
84 value->GetAsList(&list_value);
85 descriptor = [NSAppleEventDescriptor listDescriptor];
86 for (size_t i = 0; i < list_value->GetSize(); ++i) {
87 const base::Value* item;
88 list_value->Get(i, &item);
89 [descriptor insertDescriptor:ValueToAppleEventDescriptor(item)
100 } // namespace chrome