@nkuyu, , , applescript... , ( ), .
Easily run applescript from objc using NSApplescript. And if you return the string from your applescript, it is even easier to get the result, because you can get the "stringValue" from the NSAppleEventDescriptor. Therefore, I am returning "posix paths" from applescript. Please note that NSApplescript is not thread safe, so in multi-threaded applications you should always monitor it in the main thread. Try it...
-(IBAction)runApplescript:(id)sender {
[self performSelectorOnMainThread:@selector(getFrontFinderWindowTarget) withObject:nil waitUntilDone:NO];
}
-(void)getFrontFinderWindowTarget {
NSString* theTarget = nil;
NSString* cmd = @"tell application \"Finder\"\ntry\nset dir to the target of the front window\nreturn POSIX path of (dir as text)\non error\nreturn \"/\"\nend try\nend tell";
NSAppleScript* theScript = [[NSAppleScript alloc] initWithSource:cmd];
NSDictionary* errorDict = nil;
NSAppleEventDescriptor* result = [theScript executeAndReturnError:&errorDict];
[theScript release];
if (errorDict) {
theTarget = [NSString stringWithFormat:@"Error:%@ %@", [errorDict valueForKey:@"NSAppleScriptErrorNumber"], [errorDict valueForKey:@"NSAppleScriptErrorMessage"]];
} else {
theTarget = [result stringValue];
}
[self getFrontFinderWindowTargetResult:theTarget];
}
-(void)getFrontFinderWindowTargetResult:(NSString*)result {
NSLog(@"result: %@", result);
}
source
share