prasmussen / chrome-cli

Control Google Chrome from the command line
MIT License
2.61k stars 89 forks source link

Doesn't work from LaunchAgents #60

Open lexicalunit opened 3 years ago

lexicalunit commented 3 years ago

I have a LaunchAgent that attempts to look for meet.google.com in my browser tabs so that I can turn on a red "in-meeting" light.

$ cat $HOME/Library/LaunchAgents/com.lexicalunit.zoomwatcher.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>com.lexicalunit.zoomwatcher</string>

  <key>UserName</key>
  <string>my_user_name</string>

  <key>ProgramArguments</key>
  <array>
    <string>/Users/my_user_name/bin/zoomwatcher</string>
  </array>

  <key>Nice</key>
  <integer>1</integer>

  <key>KeepAlive</key>
  <true/>

  <key>RunAtLoad</key>
  <true/>

  <key>StandardErrorPath</key>
  <string>/tmp/zoomwatcher.err</string>

  <key>StandardOutPath</key>
  <string>/tmp/zoomwatcher.out</string>
</dict>
</plist>

Within my zoomwatcher script I do:

#!/bin/bash

export PATH="/usr/local/bin:$PATH"
echo "########"
whoami
brave-cli version
brave-cli list links
echo "########"

# ... go on to either turn on or turn off the red light ...

And if I watch /tmp/zoomwatcher.out I see the "########" output, I see "my_user_name" as output by the whoami command, and I see "1.7.0" output as the version. But that's it. No links are listed. There are also no errors in /tmp/zoomwatcher.err. The command seems to be working... it's just not finding any tabs/links.

However If I just run brave-cli list links in a terminal window, it works:

$ brave-cli list links
[331:352] https://github.com/prasmussen/chrome-cli/issues/new
[331:355] https://meet.google.com/
lexicalunit commented 3 years ago

I attempted to also run brave-cli info and that actually does cause a crash. Here's what I get in zoomwatcher.err:

2021-06-15 12:44:52.202 chrome-cli[62428:9448538] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[SBProxyByCode activeTab]: object has not been added to a container yet; selector not recognized [self = 0x7fd787934720]'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff2051698b __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x00007fff2024ed92 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff205167ef +[NSException raise:format:] + 189
    3   CoreFoundation                      0x00007fff2047e9bb ___forwarding___ + 1448
    4   CoreFoundation                      0x00007fff2047e388 _CF_forwarding_prep_0 + 120
    5   chrome-cli                          0x0000000101ff684b -[App activeTab] + 48
    6   chrome-cli                          0x0000000101ff50f4 -[App printActiveTabInfo:] + 30
    7   chrome-cli                          0x0000000101ff7ca2 -[Handler call:] + 364
    8   chrome-cli                          0x0000000101ff7350 -[Argonaut run] + 85
    9   chrome-cli                          0x0000000101ff83f0 main + 1577
    10  libdyld.dylib                       0x00007fff203bef3d start + 1
)
libc++abi: terminating with uncaught exception of type NSException
/usr/local/bin/brave-cli: line 4: 62428 Abort trap: 6           chrome-cli "$@"

When I run it from a terminal window I get:

Id: 352
Title: Doesn't work from LaunchAgents · Issue #60 · prasmussen/chrome-cli
Url: https://github.com/prasmussen/chrome-cli/issues/60
Loading: No
jerguslejko commented 2 years ago

@lexicalunit have you managed to fix this? experiencing the same issue

lexicalunit commented 2 years ago

No solution using chrome-cli, but I was able to do it using osascript:

BRAVE="osascript -e 'tell application \"System Events\" to (name of processes) contains \"Brave Browser\"'"
if test "$(eval "$BRAVE")" = "true"; then
    osascript <<EOF
set titleString to ""

tell application "Brave Browser"
set window_list to every window # get the windows
repeat with the_window in window_list # for every window
    set tab_list to every tab in the_window # get the tabs
    repeat with the_tab in tab_list # for every tab
        set the_url to the URL of the_tab # grab the URL
        set titleString to titleString & the_url & "\n"
    end repeat
end repeat
end tell
EOF
fi

And this seems to work fine within the context of a launch agent.