Skip to content

Instantly share code, notes, and snippets.

@chobie
Created August 1, 2014 14:30
Show Gist options
  • Save chobie/22f347cbc3daff120dfc to your computer and use it in GitHub Desktop.
Save chobie/22f347cbc3daff120dfc to your computer and use it in GitHub Desktop.
osx dictionary
package main
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework CoreServices -framework Foundation
#import <CoreServices/CoreServices.h>
#import <Foundation/Foundation.h>
NSArray *DCSGetActiveDictionaries();
NSArray *DCSCopyAvailableDictionaries();
NSString *DCSDictionaryGetName(DCSDictionaryRef dictID);
NSString *DCSDictionaryGetShortName(DCSDictionaryRef dictID);
const char* dict(char *tmp){
DCSDictionaryRef dic = NULL;
NSString *dicname = @"Japanese-English";
NSArray *dicts = DCSCopyAvailableDictionaries();
for (NSObject *aDict in dicts) {
NSString *aShortName = DCSDictionaryGetShortName((DCSDictionaryRef)aDict);
if ([aShortName isEqualToString:dicname]) {
dic = (DCSDictionaryRef)aDict;
}
}
NSString *word = [NSString stringWithCString: tmp encoding:NSUTF8StringEncoding];
NSString *result = (NSString*)DCSCopyTextDefinition(dic,
(CFStringRef)word,
DCSGetTermRangeInString(dic, (CFStringRef)word, 0));
if (result != NULL) {
fprintf(stderr, "%s", [result UTF8String]);
return [result UTF8String];
}
return NULL;
}
*/
import "C"
import (
"fmt"
"os"
"unsafe"
)
func main() {
args := os.Args
if len(args) < 2 {
fmt.Fprintf(os.Stderr, "Usage: ./dict <word>\n")
return
}
words := C.CString(args[1])
result := C.dict(words)
if result != nil {
fmt.Printf("Result: %s\n", C.GoString(result))
C.free(unsafe.Pointer(words))
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment