Use NSUserScriptTask

This commit is contained in:
1024jp 2014-11-04 18:58:12 +01:00
parent 391ea6fb45
commit d2f6f02459
3 changed files with 49 additions and 46 deletions

View File

@ -2,6 +2,15 @@
Change Log
==========================
2.1.0
--------------------------
### Additions/Changes
- Improve to display gear icon in menu bar while executing a script.
2.0.3
--------------------------

View File

@ -589,17 +589,14 @@ typedef NS_ENUM(NSUInteger, CEScriptInputType) {
- (void)runAppleScript:(NSURL *)URL
//------------------------------------------------------
{
NSDictionary *errorInfo = nil;
NSError *error = nil;
NSUserAppleScriptTask *task = [[NSUserAppleScriptTask alloc] initWithURL:URL error:&error];
NSAppleScript *appleScript = [[NSAppleScript alloc] initWithContentsOfURL:URL error:&errorInfo];
[appleScript executeAndReturnError:&errorInfo];
//
if (errorInfo) {
[self showAlertWithMessage:[NSString stringWithFormat:NSLocalizedString(@"%@\nErrorNumber: %@", nil),
errorInfo[NSAppleScriptErrorMessage],
errorInfo[NSAppleScriptErrorNumber]]];
}
[task executeWithAppleEvent:nil completionHandler:^(NSAppleEventDescriptor *result, NSError *error) {
if (error) {
[self showAlertWithMessage:[error localizedDescription]];
}
}];
}
@ -608,10 +605,12 @@ typedef NS_ENUM(NSUInteger, CEScriptInputType) {
- (void)runShellScript:(NSURL *)URL
//------------------------------------------------------
{
NSError *error = nil;
NSUserUnixTask *task = [[NSUserUnixTask alloc] initWithURL:URL error:&error];
NSString *script = [self stringOfScript:URL];
//
if ([script length] == 0) {
if (!task || [script length] == 0) {
[self showAlertWithMessage:[NSString stringWithFormat:NSLocalizedString(@"Could not read the script “%@”.", nil), URL]];
return;
}
@ -621,50 +620,33 @@ typedef NS_ENUM(NSUInteger, CEScriptInputType) {
BOOL hasError = NO;
__block NSString *input = [[self class] documentStringWithInputType:inputType error:&hasError];
if (hasError) {
[self showScriptError:@"NO document, no Input."];
[self showScriptError:@"No document, no Input."];
return;
}
//
CEScriptOutputType outputType = [[self class] scanOutputType:script];
//
// task Perl 5.8.xuse encoding 'utf8'
// print使2009-03-31
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:[URL path]];
[task setCurrentDirectoryPath:NSHomeDirectory()];
// Standard Input
NSPipe *inPipe = [NSPipe pipe];
NSPipe *outPipe = [NSPipe pipe];
NSPipe *errPipe = [NSPipe pipe];
[task setStandardInput:[inPipe fileHandleForReading]];
[task setStandardOutput:[outPipe fileHandleForWriting]];
[task setStandardError:[errPipe fileHandleForWriting]];
// set input data asynchronously if available
if ([input length] > 0) {
[task setStandardInput:[NSPipe pipe]];
[[[task standardInput] fileHandleForWriting] setWriteabilityHandler:^(NSFileHandle *handle) {
[handle writeData:[input dataUsingEncoding:NSUTF8StringEncoding]];
[[inPipe fileHandleForWriting] setWriteabilityHandler:^(NSFileHandle *handle) {
NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding];
[handle writeData:data];
[handle closeFile];
}];
}
// Standard Error
__weak typeof(self) weakSelf = self;
[task setStandardError:[NSPipe pipe]];
[[[task standardError] fileHandleForReading] setReadabilityHandler:^(NSFileHandle *handle) {
typeof(self) strongSelf = weakSelf;
NSString *error = [[NSString alloc] initWithData:[handle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
if ([error length] > 0) {
dispatch_async(dispatch_get_main_queue(), ^{
[strongSelf showScriptError:error];
});
}
}];
// Standard Output
// read output asynchronously for safe with huge output
[task setStandardOutput:[NSPipe pipe]];
[[[task standardOutput] fileHandleForReading] readToEndOfFileInBackgroundAndNotify];
[[outPipe fileHandleForReading] readToEndOfFileInBackgroundAndNotify];
[[NSNotificationCenter defaultCenter] addObserverForName:NSFileHandleReadToEndOfFileCompletionNotification
object:[[task standardOutput] fileHandleForReading]
object:[outPipe fileHandleForReading]
queue:nil
usingBlock:^(NSNotification *note)
{
@ -675,18 +657,31 @@ typedef NS_ENUM(NSUInteger, CEScriptInputType) {
}
}];
[task launch];
// execute
__weak typeof(self) weakSelf = self;
[task executeWithCompletionHandler:^(NSError *error) {
typeof(self) strongSelf = weakSelf;
// error
NSData *errorData = [[errPipe fileHandleForReading] readDataToEndOfFile];
NSString *errorMsg = [[NSString alloc] initWithData:errorData encoding:NSUTF8StringEncoding];
if ([errorMsg length] > 0) {
dispatch_async(dispatch_get_main_queue(), ^{
[strongSelf showScriptError:errorMsg];
});
}
}];
}
// ------------------------------------------------------
///
- (void)showScriptError:(NSString *)newError
- (void)showScriptError:(NSString *)errorString
// ------------------------------------------------------
{
[[CEScriptErrorPanelController sharedController] showWindow:nil];
[[CEScriptErrorPanelController sharedController] addErrorString:[NSString stringWithFormat:@"[%@]\n%@",
[[NSDate date] description], newError]];
[[NSDate date] description], errorString]];
}
@end

View File

@ -338,7 +338,6 @@
// Alert message
"The script “%@” does not exist.\n\nCheck it and do “Update Script Menu”." = "スクリプト“%@”は存在しません。\n\nファイルを確認し、“スクリプトメニューを更新”してください。";
"%@\nErrorNumber: %i" = "%@\nエラー番号%i";
"Could not open the script file “%@”." = "スクリプトファイル“%@”を開けませんでした。";
"Cannnot execute the script “%@”.\nShell script requires execute permission.\n\nCheck permission of the script file." = "スクリプト“%@”を実行できません。\nシェルスクリプトの場合は実行権が必要です。\n\nアクセス権を確認してください。";
"Could not read the script “%@”." = "スクリプト“%@”を読み込めませんでした。";