简体   繁体   English

如何从C ++程序执行简单的Applescript?

[英]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. pathfile都是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.

相关问题 从C ++程序执行Applescript - Execute Applescript from c++ program 如何从C ++程序执行命令行命令 - How can I execute a command line command from a C++ program 如何在Windows中从VS2008 / C ++应用程序执行一个程序,该程序替换调用程序并在xp / vista / 7上运行? - How can I execute a program from my VS2008/C++ application in windows that replaces the caller and runs on xp/vista/7? 从Java代码执行C ++程序时应如何测量其执行时间? - How should i measure execution time of a C++ program when I execute it from Java code? 如何解决程序崩溃的问题? - How can I fix my program from crashing in C++? 如何从C ++程序更改bash目录? - How can I change bash directory from C++ program? 如何配置Geany通过一个键来编译和执行C ++程序? - How can I configure Geany to compile & execute a C++ program by a single key? 如何在内存中加载外部文件/程序然后执行它(C ++和Unix)? - How can I load an external file/program in memory and then execute it (C++ and Unix)? 如何直接在Android设备上以可执行文件的形式运行简单的c ++程序? - How can I run a simple c++ program on android device directly as an executable? 如何从 Windows 中的 C++ 程序执行另一个 exe - How to execute another exe from a C++ program in Windows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM