Monitor when app opens file

I am wondering if there is a way to monitor when a user presses open file (cmd+o) in any application.

I was thinking I could use addGlobalMonitorForEvents, but I was not sure how to get the specific on "Open File..." event.

2 answers

  • answered 2018-02-13 10:40 Moritz

    I was able to mostly solve the problem by using this answer:

        let keycode = UInt16(0x1F)
        let keymask: NSEvent.ModifierFlags = NSEvent.ModifierFlags.command
        //...
        let options = NSDictionary(object: kCFBooleanTrue, forKey: kAXTrustedCheckOptionPrompt.takeUnretainedValue() as NSString) as CFDictionary
        let trusted = AXIsProcessTrustedWithOptions(options)
        if (trusted) {
            NSEvent.addGlobalMonitorForEvents(matching: .keyUp, handler: self.handler)
        }
    

  • answered 2018-02-13 11:35 vikram singh

    id eventGloKeyboard = [NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyUpMask handler:^(NSEvent *event)
                                {
                                    if([[event charactersIgnoringModifiers] compare:@"c"] == 0)
                                    {
                                        NSLog(@"Command + c pressed");
                                        [self keyboardCopyContent];
                                    }
    
             }];
    

    I did it like above in objective c so change it in swift accordingly. If you found out any issue and going to down vote please let me know what was the issue.