tisfeng/Easydict

在m1电脑上面,请问使用EZScriptExecutor类时,用到NSAppleScript。NSAppleEventsUsageDescription权限要怎么解决?

Closed this issue · 1 comments

Jakn commented
在m1电脑上面,请问使用EZScriptExecutor类时,用到NSAppleScript。NSAppleEventsUsageDescription权限要怎么解决?

我也遇到过,不知道啥原因,也没找解决办法,代码中有写注释,所以建议使用更底层的 NSTask 去执行 NSAppleScript,这个它会自动申请权限。

/// Use NSAppleScript to run AppleScript, faster than NSTask.
/// !!!: Note that this method may fail due to execution permissions, it will not automatically apply for permissions when I test.
- (void)runAppleScript:(NSString *)script completionHandler:(void (^)(NSString *result, EZError * _Nullable error))completionHandler {
    NSAppleScript *appleScript = [[NSAppleScript alloc] initWithSource:script];
    CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        @try {
            EZError *error = nil;
            NSDictionary *errorInfo = nil;
            // ???: Sometimes it will crash in this line
            NSAppleEventDescriptor *result = [appleScript executeAndReturnError:&errorInfo];
            NSString *resultString = [result stringValue];
            resultString = [resultString trim];
            if (errorInfo) {
                MMLogInfo(@"runAppleScript errorInfo: %@", errorInfo);
                NSString *errorString = errorInfo[NSAppleScriptErrorMessage];
                EZErrorType type = EZErrorTypeAPI;
                if ([errorString containsString:kEasydictTranslatShortcutName]) {
                    type = EZErrorTypeParam;
                }
                error = [EZError errorWithType:type description:errorString];
            }
            
            CFAbsoluteTime endTime = CFAbsoluteTimeGetCurrent();
            MMLogInfo(@"run AppleScript cost: %.1f ms", (endTime - startTime) * 1000);
            
            dispatch_async(dispatch_get_main_queue(), ^{
                completionHandler(resultString, error);
            });
        } @catch (NSException *exception) {
            MMLogError(@"exception: %@", exception);
            [self runAppleScriptWithTask:script completionHandler:completionHandler];
             
#if Debug
            [EZToast showToast:exception.reason];
#endif
        }
    });
}