[英]How can I execute a simple Applescript from a C++ program?
I would like to execute the Applescript command tell application "Finder" to open POSIX file */path/to/somefilename*
from a C++ program. 我想执行Applescript命令
tell application "Finder" to open POSIX file */path/to/somefilename*
从C ++程序tell application "Finder" to open POSIX file */path/to/somefilename*
。 It looks like I might want to use OSACompileExecute
, but I haven't been able to find an example of how to use it. 看起来我可能想要使用
OSACompileExecute
,但我还没有找到如何使用它的示例。 I keep finding examples of how to use the OSACompile
Terminal command. 我一直在寻找如何使用
OSACompile
Terminal命令的示例。 Can someone provide an example or a link to an example? 有人可以提供示例或示例链接吗?
Ok, the trick was to not bother trying to compile and execute the Applescript but to simply use the osascript
system command: 好吧,诀窍是不打算尝试编译和执行Applescript,而只是使用
osascript
系统命令:
sprintf(cmd, "osascript -e 'tell app \"Finder\" to open POSIX file \"%s/%s\"'", getcwd(path, MAXPATHLEN), file);
system(cmd);
path
and file
are both char[] variables. path
和file
都是char []变量。
I got the clue from this excerpt from Applescript: The Definitive Guide . 我从Applescript:The Definitive Guide的 摘录中得到了线索。
Here's an example C function for reading a Get Info comment from the finder using AppleScript. 这是一个示例C函数,用于使用AppleScript从finder读取Get Info注释。
You could modify it for what you want. 您可以根据需要修改它。
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.