定义-是否显示Spotlight搜索字段

时间:2019-02-04 16:03:23

标签: swift xcode macos cocoa spotlight

我尝试从macOS应用程序(Swift)进行定义-是否显示Spotlight搜索字段?

我制作了一个简单的单独项目草图-名称为“ MyTestApp”的可可应用程序-一个带有一个按钮的ViewController。

// the code of the button pressing processing
@IBAction func buttonClick(_ sender: NSButton) {
        print("The Detect button was pressed - it's time to press Command+Space!")
        let timer = Timer.scheduledTimer(withTimeInterval: 3, repeats: false) {_ in
            print("The delay time is over!")
            let runningApps = NSWorkspace.shared.runningApplications
            for runningApp in runningApps {
                // Print current active app name
                if runningApp.isActive {
                    print("localizedName: \(runningApp.localizedName)")
                    print("bundleIdentifier: \(runningApp.bundleIdentifier)")
                    print("processIdentifier: \(runningApp.processIdentifier)")
                    print("isActive: \(runningApp.isActive)")
                    print("isHidden: \(runningApp.isHidden)")
                    print("***")
                }
                // Print Spotlight state
                if let localizedName = runningApp.localizedName {
                    if localizedName == "Spotlight" {
                        print("bundleIdentifier: \(runningApp.bundleIdentifier)")
                        print("processIdentifier: \(runningApp.processIdentifier)")
                        print("isActive: \(runningApp.isActive)")
                        print("isHidden: \(runningApp.isHidden)")
                        print("***")                        
                    }
                }
            }
    }
}

在此应用程序中按下按钮后,我按键盘上的Command + Space键启动Spotlight,并且它已启动,因此我看到并使用了Spotlight搜索字段。 由于这段代码的延迟,当Spotlight处于最前沿时,我检查了正在运行的应用程序的状态,但是Xcode日志输出中的结论却并非如此:

The Detect button was pressed - it's time to press Command+Space!
The delay time is over!
bundleIdentifier: Optional("com.apple.Spotlight")
processIdentifier: 275
isActive: false
isHidden: false
***
localizedName: Optional("MyTestApp")
bundleIdentifier: Optional("noname.MyTestApp")
processIdentifier: 1560
isActive: true
isHidden: false
***    

那么,如何确定是否显示Spotlight搜索字段?还是Spotlight中有键盘?还是现在正在输入什么应用程序?还是哪个应用现在具有键盘焦点?

1 个答案:

答案 0 :(得分:0)

答案标题:

正在运行的应用程序上有1个KVO:

<table style="width:100%">
  <tr>
    <th style="background:gray;">ID Servizio</th>
<th style="background:yellow;">Nome Servizio</th>
<th style="background:green;">Descrizione</th>
  </tr>
  <tr>
     <td>1</td>
	<td>searchCostruttore</td>
	<td>servizio che serve per ottenere il costruttore tramite sigcos e dessig</td>
  </tr>
  <tr>
    <td>2</td>
                <td>searchStructureAgent</td>
                <td>servizio che serve per ottenere la struttura dell'agente tramite codice e descrizione</td>
  </tr>
  <tr>
     <td>3</td>
                <td>getStructureAgent</td>
                <td>servizio che serve per ottenere la struttura dell'agente tramite il codice</td>
  </tr>
</table>

2。获取屏幕窗口列表。

NSArray<NSRunningApplication *> *runningApplications = [[NSWorkspace sharedWorkspace] runningApplications];
for (NSRunningApplication *app in runningApplications) {
    NSURL *URL = [app bundleURL];
    NSBundle *bundle = [NSBundle bundleWithURL:URL];
    if ([[app bundleIdentifier] isEqualToString:@"com.apple.Spotlight"]) {
        [app addObserver:self forKeyPath:@"active" options:NSKeyValueObservingOptionNew context:nil];
        _app = app;
    }
}



- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    //WARNING: LAZY CODE USE PROPER OBSERVATION WITH CONTEXT AND CALLING SUPER!
    NSLog(@"%@", keyPath);
    NSLog(@"%@", object);
    NSLog(@"isActive: %d", [_app isActive]);
}

- (void)dealloc
{
  if (_app) {
    [_app removeObserver:self]
  }
}

PS:别忘了释放。 完整的源代码包含在Apple的示例应用程序中:“ Son of Grab”

CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);

如果您需要的还不止此(读取已键入的内容等),则将需要Accessibility API并获得用户的允许(沙盒)