我想从C ++程序执行Applescript命令tell application "Finder" to open POSIX file */path/to/somefilename*
。看起来我可能想要使用OSACompileExecute
,但我无法找到如何使用它的示例。我一直在寻找如何使用OSACompile
终端命令的示例。有人可以提供示例或示例链接吗?
答案 0 :(得分:5)
好的,诀窍是不打算尝试编译和执行Applescript,而只是使用osascript
系统命令:
sprintf(cmd, "osascript -e 'tell app \"Finder\" to open POSIX file \"%s/%s\"'", getcwd(path, MAXPATHLEN), file);
system(cmd);
path
和file
都是char []变量。
我从 Applescript:The Definitive Guide 中获得了this excerpt的线索。
答案 1 :(得分:1)
以下是使用AppleScript从Finder读取Get Info注释的示例C函数。
您可以根据需要修改它。
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;