nomeata / arbtt

arbtt, the automatic rule-based time-tracker
http://arbtt.nomeata.de/
GNU General Public License v2.0
317 stars 36 forks source link

Doesn't capture window titles in macOS Catalina #30

Open waldyrious opened 4 years ago

waldyrious commented 4 years ago

Ever since I upgraded to the latest macOS Catalina, arbtt ceased to be able to capture the window titles. The problem looks exactly like what I described a while ago in #3:

❯ arbtt-dump -l 10
2020-01-29 14:12:26 (339ms inactive):
2020-01-29 14:13:26 (142ms inactive):
2020-01-29 14:14:26 (53ms inactive):
2020-01-29 14:15:26 (1080ms inactive):
2020-01-29 14:16:26 (136ms inactive):
2020-01-29 14:17:26 (45ms inactive):
2020-01-29 14:18:26 (146ms inactive):
2020-01-29 14:19:26 (49ms inactive):
2020-01-29 14:20:26 (363ms inactive):
2020-01-29 14:21:26 (88ms inactive):

However, I'm opening a new issue because the problem seems to be slightly different this time. arbtt-capture is running and continues to be loaded automatically on startup:

❯ ps -A | grep arbtt
 2792 ??         0:22.08 /Users/waldyrious/.cabal/bin/arbtt-capture
74267 ttys009    0:00.00 /usr/local/bin/ggrep --color=auto arbtt

❯ launchctl list | grep arbtt
2792    0   de.nomeata.arbtt

❯ launchctl load ~/Library/LaunchAgents/de.nomeata.arbtt.plist
/Users/waldyrious/Library/LaunchAgents/de.nomeata.arbtt.plist: service already loaded

It clearly happened when I upgraded to Catalina:

⋮
    (*) Firefox:        zyedidia/micro: A modern and intuitive terminal-based text editor
    (*) Firefox:        Home · zyedidia/micro Wiki
    (*) Firefox:        Keybindings not present in command mode (ctr-e) · Issue #635 · zyedidia/micro
    (*) Firefox:        Add `show-whitespace` toggle button · Issue #2240 · sindresorhus/refined-github
    (*) Firefox:        GoboHide source code link points nowhere · Issue #14 · gobolinux/gobolinux.org
    (*) Firefox:        Mozilla Firefox
    (*) System Preferences: Software Update
    (*) Install macOS Catalina: Install macOS Catalina
2020-01-12 01:00:55 (683ms inactive):
2020-01-12 02:00:51 (155583ms inactive):
2020-01-12 11:00:35 (257ms inactive):
2020-01-12 12:00:25 (1ms inactive):
⋮

Indeed, macOS Catalina introduced new permissions for capturing information from the screen (e.g. for video calls) and that is likely affecting arbtt. In System Preferences, we can navigate to Security and Privacy > Privacy > Screen Recording, and will see something like this:

Screenshot 2020-01-29 at 14 18 39

I suppose Arbtt needs to be granted permissions here in order to function properly. @vrasneur do you have any suggestions about how we can make an Arbtt entry appear on this screen?

nomeata commented 4 years ago

NB: I don't have a mac and have never used it on a Mac. Y’all mac users are on your own. I am happy to accept PRs agains the code or docs to help you.

waldyrious commented 4 years ago

I understand. I'm happy to help out with whatever I can (e.g. debugging, testing fixes, etc.). Thanks for the fast response!

oguzhanogreden commented 4 years ago

I can verify that @waldyrious identified the cause correctly. I happened to get iTerm2 into that list and when I run arbtt from iTerm2, it just works.

waldyrious commented 4 years ago

@oguzhanogreden Thanks for the hint! May I ask how you managed to add iTerm2 to the list? I couldn't find a way to manually add a new entry, and didn't find a configuration key to use with defaults (the closest I managed to find was a way to disable the prompt, but that felt like the wrong approach). I ended up triggering a screenshot from iTerm2 by running screencapture /tmp/foo.png, which did bring up the prompt, but I wonder if you had a less roundabout way of doing that.

In any case, this solution didn't seem to work for me, since I don't run arbtt manually from iTerm2; I set it up using launchd, as described in the installation instructions. Are you using a different method for automatically starting the program?

oguzhanogreden commented 4 years ago

I believe I had the iTerm2 added by a similar route a while ago. It was pure coincidence that it was helpful here.

I surveyed some StackOverflow links, including the one you posted, and came up with a Swift script that helps get the prompt. Unfortunately, it gets the prompt for the application from which it is called.

I'm not familiar with the mechanics of launchd. Perhaps worth if you give it a shot and see if you can get the prompt for that process. Once you have the prompt, you may also ditch the script and call arbtt-capture executable directly, as far as I understand. If you get to establish a reasonable workflow, I'd love to hear.

waldyrious commented 4 years ago

I've found two similar questions, on Stack Overflow and Apple developer forums, but neither had an answer 😞.

waldyrious commented 4 years ago

Update: I've managed to get a permission prompt for "sh", after attempting one of the workarounds suggested by the second link. First I created a test script:

echo -e '#!/bin/sh\n/usr/sbin/screencapture /tmp/foobar.png' > ~/screenshot.sh
chmod +x ~/screenshot.sh

Then I made the following change in ~/Library/LaunchAgents/de.nomeata.arbtt.plist:

-    <string>/Users/waldyrious/.cabal/bin/arbtt-capture</string>
+    <string>/Users/waldyrious/screenshot.sh</string>

Then I logged out and logged in again, and the permission prompt showed up. I have now restored the .plist file to point to arbtt-capture, let's see if it starts working now! 🤞

waldyrious commented 4 years ago

Turns out it wasn't enough to simply reset the original .plist contents and have arbtt work again. So what I did was edit the screenshot.sh script:

#!/bin/sh
-/usr/sbin/screencapture /tmp/foobar.png
+$HOME/.cabal/bin/arbtt-capture

And let .plist file continue to invoke the screenshot.sh script instead of calling arbtt directly. That seems to work (yay! 🎉), but it's clearly a dirty hack. I'll update if I find any better way of doing this.

LiamHz commented 4 years ago

Thanks @waldyrious. I finally got arbtt working on MacOS Catalina because of you :)

For people in the future, here's what I had to do to get arbtt working:

  1. Run vim ~/Library/LaunchAgents/com.username.arbtt.plist and paste the text in this file, replacing "liamhinzman" with whatever your username is

  2. Run this command echo -e '#!/bin/sh\n/usr/sbin/screencapture /tmp/foobar.png' > ~/screenshot.sh

  3. Run this command chmod +x ~/screenshot.sh

  4. Log out, and then back in. MacOS will prompt you to grant sh permission to record your screen. Enable this.

  5. Run vim ~/screenshot.sh and replace this line /usr/sbin/screencapture /tmp/foobar.png with this $HOME/.cabal/bin/arbtt-capture

  6. Log out, and log back in

  7. After waiting one minute, run arbtt-dump, and you should see that everything is working.

Note: You can use any command line text editor instead of vim. E.g. nano

waldyrious commented 4 years ago

Glad that helped @LiamHz. Just to be sure, your steps are exactly what I did, but presented as an easy to follow list, right?

In that case, I'm afraid we should wait for a more adequate solution to pop up (that we could add to the documentation and therefore close this issue), since this is a very hacky way to get it to work. Agreed?

nomeata commented 4 years ago

It seems that screencapture is doing something to request the necessary permission. Maybe someone can figure out what that is and let arbtt-capture just do that?

LiamHz commented 4 years ago

Glad that helped @LiamHz. Just to be sure, your steps are exactly what I did, but presented as an easy to follow list, right?

Yep, just an easy to follow version of what you said to do.

In that case, I'm afraid we should wait for a more adequate solution to pop up (that we could add to the documentation and therefore close this issue), since this is a very hacky way to get it to work. Agreed?

Agreed, it's still hacky. There's a 30% chance I'll do a pull request to fix this in the next month.

ghost commented 4 years ago

@waldyrious you can give it a try to allow screen recording permissions using command line.

sudo sqlite3 /Library/Application\ Support/com.apple.TCC/TCC.db "INSERT or REPLACE INTO access VALUES('kTCCServiceScreenCapture','your binary path',1,1,1,NULL,NULL,NULL,'UNUSED',NULL,0,1547635615)"

however .sh solution shared above also works but if you're changing binary then you might need to do the whole process again.

rjmk commented 1 month ago

You can now manually add to the Settings > Privacy & Security > Screen & System Audio Recording. There's a plus button, and you can navigate to ~/.cabal/bin and add .arbtt-capture. Seems to work great :-)

waldyrious commented 1 month ago

I am not longer forced to use a macbook for my work, so I don't have a way to try this out anymore. @oguzhanogreden, @LiamHz, @rjmk — do you think we could close this issue by adding @rjmk's instructions to the documentation?