簡體   English   中英

如何從C ++程序執行簡單的Applescript?

[英]How can I execute a simple Applescript 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* 看起來我可能想要使用OSACompileExecute ,但我還沒有找到如何使用它的示例。 我一直在尋找如何使用OSACompile Terminal命令的示例。 有人可以提供示例或示例鏈接嗎?

好吧,訣竅是不打算嘗試編譯和執行Applescript,而只是使用osascript系統命令:

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

pathfile都是char []變量。

我從Applescript:The Definitive Guide的 摘錄中得到了線索。

這是一個示例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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM