Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pasting fails on High Sierra #161

Closed
p0deje opened this issue Sep 4, 2020 · 11 comments
Closed

Pasting fails on High Sierra #161

p0deje opened this issue Sep 4, 2020 · 11 comments
Labels
bug Something isn't working

Comments

@p0deje
Copy link
Owner

p0deje commented Sep 4, 2020

When trying to paste on High Sierra - nothing happens. It might the issue of focusing the application back.

@p0deje p0deje added the bug Something isn't working label Sep 4, 2020
@p0deje
Copy link
Owner Author

p0deje commented Sep 8, 2020

I nailed the issue down to enabling the sandbox 😞

It seems like sending a "CMD+V" event silently fails on High Sierra while succeeding in Catalina. What a mess.
I've submitted a report to Apple Developer asking if it's possible to deal with this somehow.

@p0deje
Copy link
Owner Author

p0deje commented Sep 9, 2020

FTR Pasting works fine on Mojave so it's only High Sierra issue.

p0deje added a commit that referenced this issue Sep 9, 2020
@p0deje
Copy link
Owner Author

p0deje commented Sep 28, 2020

@adrianbj Could you try running this version of Maccy and let me know if it fixes the problem?

@p0deje
Copy link
Owner Author

p0deje commented Sep 28, 2020

Anyone else who's still running High Sierra and has similar issue is welcome to try ☝️ too!

@adrianbj
Copy link

I'm on vacation this week but will test next week. Thanks.

@p0deje
Copy link
Owner Author

p0deje commented Sep 30, 2020

The current implementation of pasting is as simple as "simulate CMD+V keyboard press":

  // Based on https://github.com/Clipy/Clipy/blob/develop/Clipy/Sources/Services/PasteService.swift.
  func paste() {
    guard accessibilityAllowed else {
      Maccy.returnFocusToPreviousApp = false
      // Show accessibility window async to allow menu to close.
      DispatchQueue.main.async(execute: showAccessibilityWindow)
      return
    }

    DispatchQueue.main.async {
      let vCode = UInt16(kVK_ANSI_V)
      let source = CGEventSource(stateID: .combinedSessionState)
      // Disable local keyboard events while pasting
      source?.setLocalEventsFilterDuringSuppressionState([.permitLocalMouseEvents, .permitSystemDefinedEvents],
                                                         state: .eventSuppressionStateSuppressionInterval)

      let keyVDown = CGEvent(keyboardEventSource: source, virtualKey: vCode, keyDown: true)
      let keyVUp = CGEvent(keyboardEventSource: source, virtualKey: vCode, keyDown: false)
      keyVDown?.flags = .maskCommand
      keyVUp?.flags = .maskCommand
      keyVDown?.post(tap: .cgAnnotatedSessionEventTap)
      keyVUp?.post(tap: .cgAnnotatedSessionEventTap)
    }
  }

Apple Developer support believes it's a bug that it does nothing in the sandboxed version of Maccy in High Sierra. Given it is okay in the newer macOS version - it won't be fixed.

As an alternative, I found an AppleScript that can simulate pasting (https://stackoverflow.com/questions/25747253/applescript-to-paste-clipboard):

tell application "System Events" to keystroke "v" using command down

It works in all OSes but suffers from the flaw that it also takes into account other pressed key modifiers. So, if someone pastes by pressing OPTION+5, it might fail if OPTION is not released yet - the app would receive CMD+OPTION+V which is wrong. There are various hacks that might improve this, but I'm not sure I want to go that route.

Another possible solution would be of course to simply disable the sandbox, but that has 2 problems:

  1. Everybody who upgraded to the sandboxed version will lose their data as soon as a new non-sandboxed version is installed. Probably can be worked around with some migration script.
  2. It renders my idea of distributing in the App Store impossible.

I'm going to give a shot and submit the app as-is to the App Store. It might still be blocked due to CGEvent and accessibility usage. If it happens, I'll see if the AppleScript-based version can pass the review. If yes, I'll do my best to hack around the key modifiers issue in AppleScript.

I'll revert to disabling sandbox if in the very end there will be no way to pass the App Store review. Though there are various clipboard managers out there that somehow work and passed the review, so I haven't lost the hope yet.

@p0deje
Copy link
Owner Author

p0deje commented Oct 2, 2020

Ok, the application has passed the review in the App Store. With that said, I lean towards the easiest route which is to drop support for High Sierra at all. One can still keep using the old version that works without a sandbox (v0.14.1).

if anyone can come up with a different solution where I can work it around in High Sierra, I'll be happy to bring support back. I understand how disappointed High Sierra users might be and I'd love to make it work, but I just don't see a way.

p0deje added a commit that referenced this issue Oct 2, 2020
Since v0.15.0 the sandbox has been enabled in the app. This in fact
broke pasting on High Sierra. Unfortunately, the sandbox prevents
emitted paste event from reaching the application on this particular
version of macOS. Apple Developer support stated that this is a bug
which has probably been fixed in Mojave, but won't even be in High
Sierra.

At the same time, I haven't found any reliable alternative to pasting
event. One of those was to use AppleScript but it suffers from other
problems. See discussion in #161 for more details.

In the very end, it seems like there are 2 ways: either stop supporting
High Sierra as pasting doesn't work there or disable the sandbox. Given
I plan to distribute the app in the App Store, the former is really the
only way to go for now.
p0deje added a commit that referenced this issue Oct 2, 2020
@p0deje p0deje closed this as completed Oct 3, 2020
@adrianbj
Copy link

adrianbj commented Oct 5, 2020

@p0deje - I guess there really isn't much point me testing now. I would upgrade from High Sierra, but it's not important enough for me to bother with the possible issues on my 2011 MBP - happy to just keep it ticking along for a while longer 👍

Thanks for your efforts though. For now, I've gone back to Clipy.

@mul14
Copy link

mul14 commented Oct 7, 2020

@adrianbj Could you try running this version of Maccy and let me know if it fixes the problem?

I still use High Sierra. That version working properly for me.

@adrianbj
Copy link

adrianbj commented Oct 8, 2020

@mul14 - that version does seem to work as expected - thanks! Is this a version you modified yourself?

@mul14
Copy link

mul14 commented Oct 11, 2020

@adrianbj I don't modify anything. Just download from here and use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants