5

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

4

2 回答 2

6

好的,诀窍是不要费心尝试编译和执行 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 的这段摘录中得到了线索。

于 2012-02-01T20:16:53.460 回答
1

这是一个示例 C 函数,用于使用 AppleScript 从 finder 读取 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;
于 2012-02-01T19:28:49.637 回答