Как я могу выполнить простой Applescript из программы на C ++? - PullRequest
4 голосов
/ 01 февраля 2012

Я хотел бы выполнить команду Applescript tell application "Finder" to open POSIX file */path/to/somefilename* из программы на C ++.Похоже, я мог бы использовать OSACompileExecute, но я не смог найти пример того, как его использовать.Я продолжаю находить примеры использования команды терминала OSACompile.Может кто-нибудь привести пример или ссылку на пример?

Ответы [ 2 ]

5 голосов
/ 02 февраля 2012

Хорошо, уловка была не в том, чтобы пытаться скомпилировать и выполнить 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: Полное руководство .

1 голос
/ 01 февраля 2012

Вот пример функции C для чтения комментария Get Info из искателя с использованием AppleScript.

Вы можете изменить его так, как вам нужно.

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;
...