2012-02-01 70 views
3

我想從C++程序執行Applescript命令tell application "Finder" to open POSIX file */path/to/somefilename*。它看起來像我可能想要使用OSACompileExecute,但我一直無法找到如何使用它的例子。我一直在找到如何使用OSACompile終端命令的例子。有人可以提供一個例子或一個例子的鏈接?如何從C++程序執行簡單的Applescript?

回答

4

好,訣竅是白費力氣試圖編譯和執行的AppleScript,而是簡單地使用osascript系統命令:

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

pathfile都的char []變量。

我從this excerpt得知線索:權威指南

1

下面是一個示例C函數,用於使用AppleScript從查找器讀取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; 
+1

對我來說看起來不像C ... – JustSid 2012-02-01 19:35:40

+1

它是使用NSAppleScript對象的Objective-C代碼示例,不能在C++中使用 – 2012-02-01 19:40:12

+0

對於此項目,我希望避免使用Cocoa和Objective-C if可能。 – SSteve 2012-02-01 19:40:41