【问题标题】:Applescript and Cocoa modal sessionApplescript 和 Cocoa 模态会话
【发布时间】:2016-12-06 17:59:48
【问题描述】:

OSX 10.12 这是场景:有一个应用程序在启动时显示带有进度条的模式表。然后,一段时间后,工作表关闭,应用程序准备就绪。 接下来是负责这个的代码:

-(void) open{
    [self showWindow:nil];

    [self.sheet setPreventsApplicationTerminationWhenModal:NO];
    [self.mainWindow beginSheet: self.sheet
      completionHandler:^(NSModalResponse returnCode) {
          [NSApp stopModalWithCode: returnCode];
    }];

    [self.progbar setIndeterminate:YES];
    [self.progbar startAnimation:nil];
}

-(void) close{
    if(self.sheet){
        [self.progbar stopAnimation:nil];
        [self.mainWindow endSheet:self.sheet];
    }
}

模态表引入了已按下的取消按钮关闭表。 我也有启动这个应用程序的 Applescript 程序:

tell application "xyz.app"
    activate
    do something
end tell

此脚本会启动应用程序,并且模式表会根据需要出现和消失。 然而,这个脚本

tell application "xyz.app"
    do something
end tell

启动应用程序,工作表出现并且不会消失。按下按钮取消没有帮助 - 模式表保持 卡住。 谁能解释一下?

更新:方法关闭总是执行

【问题讨论】:

  • close方法执行了吗?

标签: objective-c cocoa modal-dialog applescript nswindow


【解决方案1】:

我遇到了类似的皱纹问题 - 我想要一个带有默认返回值的模态警报框淡出。这不会卡住,它会执行模态块,直到淡出完成或用户选择其中一个按钮。

    - (BOOL) warningAlert:(NSString* )messageText informText:(NSString*)informText {
  // Displays a fading, then self-dismissing view within the top level window

  __block NSInteger alertReturn = 0; // __block, so it can be accessed by the completion blocks

  // Define the alert
  NSAlert* __block generalAlert = [[NSAlert alloc] init];
  [generalAlert addButtonWithTitle:@"Continue?"];
  [generalAlert addButtonWithTitle:@"Cancel"];
  [generalAlert setMessageText:messageText];
  [generalAlert setInformativeText:informText];
  [generalAlert setAlertStyle:NSAlertStyleWarning];

  // Animate the fading box, with the passed completion criteria alertReturn = NSModalResponseStop
  [NSAnimationContext beginGrouping];
  // Animate alpha fade
  [[NSAnimationContext currentContext] setDuration:5.0];
  [[NSAnimationContext currentContext] setCompletionHandler:^{
    // Dismiss the alert
    NSWindow *window = [NSApp mainWindow];
    [[window attachedSheet] close];
    NSLog(@"Closed the Sheet");
    alertReturn = NSModalResponseStop;
  }];
  [[generalAlert.window animator] setAlphaValue:0.0];
  [NSAnimationContext endGrouping];

  // Begin the alert.
  // For the completion criteria, speed up the animation to it's conclusion, but not before we return to the calling method
  [generalAlert beginSheetModalForWindow:self.mySwiftCentral.sxWindowController.window completionHandler:^(NSInteger result) {
    [NSAnimationContext beginGrouping];
    [[NSAnimationContext currentContext] setDuration:0.01];
    [[generalAlert.window animator] setAlphaValue:0.0];
    [NSAnimationContext endGrouping];
    alertReturn = result;
    NSLog(@"Success, alertReturn = %ld",(long)alertReturn);
  }];

  // Create a NSModalSession using the global Application Instance variable NSApp
  NSModalSession session = [NSApp beginModalSessionForWindow:
                            self.mywindow];
  alertReturn = NSModalResponseContinue;

  // Loop here until the user responds or the animation completes, either of which will modify alertReturn and break out of the loop
  // Without the NSModalSession, this loop would block user button actions or animation timeout.
  for (;;) {
    [NSApp runModalSession:session];
    if (alertReturn != NSModalResponseContinue)
      break;
    // Break on timeout here
  }
  [NSApp endModalSession:session];

  NSLog(@"Alert Return = %ld",(long)alertReturn);
  if (alertReturn == NSAlertFirstButtonReturn ||
      alertReturn == NSModalResponseStop) {
    return YES;
  }
  else return NO;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多