AppleScriptController.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
  3. * Copyright (C) 2006 Alexey Proskuryakov (ap@nypop.com)
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  15. * its contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #import "config.h"
  30. #import "AppleScriptController.h"
  31. #import <WebKit/WebView.h>
  32. #import <WebKit/WebViewPrivate.h> // for aeDescByEvaluatingJavaScriptFromString, which is pending API review
  33. @implementation AppleScriptController
  34. + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
  35. {
  36. if (aSelector == @selector(doJavaScript:))
  37. return NO;
  38. return YES;
  39. }
  40. + (NSString *)webScriptNameForSelector:(SEL)aSelector
  41. {
  42. if (aSelector == @selector(doJavaScript:))
  43. return @"doJavaScript";
  44. return nil;
  45. }
  46. - (id)initWithWebView:(WebView *)wv
  47. {
  48. self = [super init];
  49. webView = wv;
  50. return self;
  51. }
  52. static id convertAEDescToObject(NSAppleEventDescriptor *aeDesc)
  53. {
  54. id value = nil;
  55. DescType descType = [aeDesc descriptorType];
  56. switch (descType) {
  57. case typeUnicodeText:
  58. value = [NSString stringWithFormat:@"\"%@\"", [aeDesc stringValue]];
  59. break;
  60. case typeLongDateTime:
  61. if ([[aeDesc data] length] == sizeof(LongDateTime)) {
  62. LongDateTime d;
  63. [[aeDesc data] getBytes:&d];
  64. value = [NSString stringWithFormat:@"%016llX", (unsigned long long)d];
  65. }
  66. break;
  67. case typeAEList:
  68. value = [NSMutableString stringWithString:@"("];
  69. int numItems = [aeDesc numberOfItems];
  70. for (int i = 0; i < numItems; ++i) {
  71. if (i != 0)
  72. [(NSMutableString*)value appendString:@", "];
  73. id obj = convertAEDescToObject([aeDesc descriptorAtIndex:(i + 1)]);
  74. [(NSMutableString*)value appendString:[obj description]];
  75. }
  76. [(NSMutableString*)value appendString:@")"];
  77. break;
  78. case typeType: {
  79. OSType type = [aeDesc typeCodeValue];
  80. char typeStr[5];
  81. typeStr[0] = type >> 24;
  82. typeStr[1] = type >> 16;
  83. typeStr[2] = type >> 8;
  84. typeStr[3] = type;
  85. typeStr[4] = 0;
  86. value = [NSString stringWithFormat:@"'%s'", typeStr];
  87. break;
  88. }
  89. }
  90. if (!value)
  91. value = [aeDesc stringValue];
  92. if (!value)
  93. value = [aeDesc data];
  94. return value;
  95. }
  96. - (NSString *)doJavaScript:(NSString *)aString
  97. {
  98. NSAppleEventDescriptor *aeDesc = [webView aeDescByEvaluatingJavaScriptFromString:aString];
  99. if (!aeDesc)
  100. return @"(null)";
  101. DescType descType = [aeDesc descriptorType];
  102. char descTypeStr[5];
  103. descTypeStr[0] = descType >> 24;
  104. descTypeStr[1] = descType >> 16;
  105. descTypeStr[2] = descType >> 8;
  106. descTypeStr[3] = descType;
  107. descTypeStr[4] = 0;
  108. return [NSString stringWithFormat:@"%@ ('%s')", convertAEDescToObject(aeDesc), descTypeStr];
  109. }
  110. @end