2012-02-01 69 views
3

我想从C++程序执行Applescript命令tell application "Finder" to open POSIX file */path/to/somefilename*。它看起来像我可能想要使用OSACompileExecute,但我一直无法找到如何使用它的例子。我一直在找到如何使用OSACompile终端命令的例子。有人可以提供一个例子或一个例子的链接?如何从C++程序执行简单的Applescript?

回答

4

好,诀窍是白费力气试图编译和执行的AppleScript,而是简单地使用osascript系统命令:

sprintf(cmd, "osascript -e 'tell app \"Finder\" to open POSIX file \"%s/%s\"'", getcwd(path, MAXPATHLEN), file); 
    system(cmd); 

pathfile都的char []变量。

我从this excerpt得知线索:权威指南

1

下面是一个示例C函数,用于使用AppleScript从查找器读取Get Info注释。

您可以根据需要对其进行修改。

NSString * readFinderCommentsForFile(NSString * theFile){ 
/* Need to use AppleScript to read or write Finder Get Info Comments */ 

/* Convert POSIX file path to hfs path */ 
NSURL * urlWithPOSIXPath = [NSURL fileURLWithPath:theFile]; 
NSString * hfsStylePathString = 
(__bridge_transfer NSString *)CFURLCopyFileSystemPath((__bridge CFURLRef) urlWithPOSIXPath, kCFURLHFSPathStyle); 

/* Build an AppleScript string */ 
NSString *appleScriptString = @"tell application \"Finder\"\r get comment of file "; 
appleScriptString = [appleScriptString stringByAppendingString:@"\""]; 
appleScriptString = [appleScriptString stringByAppendingString:hfsStylePathString]; 
appleScriptString = [appleScriptString stringByAppendingString:@"\""]; 
appleScriptString = [appleScriptString stringByAppendingString:@"\r end tell\r"]; 


NSString *finderComment; 

NSAppleScript *theScript = [[NSAppleScript alloc] initWithSource:appleScriptString]; 

NSDictionary *theError = nil; 
finderComment = [[theScript executeAndReturnError: &theError] stringValue]; 
NSLog(@"Finder comment is %@.\n", finderComment); 


return finderComment; 
+1

对我来说看起来不像C ... – JustSid 2012-02-01 19:35:40

+1

它是使用NSAppleScript对象的Objective-C代码示例,不能在C++中使用 – 2012-02-01 19:40:12

+0

对于此项目,我希望避免使用Cocoa和Objective-C if可能。 – SSteve 2012-02-01 19:40:41