Skip to content

Commit

Permalink
add workflow variables to manage commands paths
Browse files Browse the repository at this point in the history
Fix #12
  • Loading branch information
bmunoz89 committed Apr 25, 2021
1 parent 6cb979e commit abfad87
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 31 deletions.
39 changes: 37 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

# 💻 Installation 👾

Run this command on a terminal:
Install brew https://brew.sh/

And then run this command on a terminal:
```bash
brew install blueutil
```
Expand Down Expand Up @@ -35,6 +37,39 @@ Now [download][last release link] ⏬ the last release and open it 😎. That's
- Manually check for an update
- Clear data: Allowing to clear the blueutil path saved(just in case 🤷🏽‍♂️)

Besides that, you can add shortcuts into the preferences 👌🏽
# 🆘 Help

## - What should I do if a get the message "Change your blueutil or brew path"?

![](./screenshots/command_error.jpg)

To get them, run the following commands in your own terminal:
```bash
> which brew # paste this command
/usr/local/bin/brew # this is just an example result
> which blueutil # and paste this one
/usr/local/bin/blueutil # this is just an example result
```

Copy both results and follow the steps in the next point.

---

## - How to set my own `brew`/`blueutil` path?

### Step 1: Open your Alfred settings inside the workflows panel

![](./screenshots/settings_1.jpg)

### Step 2: Press the button "Configure workflow and variables"

![](./screenshots/settings_2.jpg)

### Step 3: Edit `bluetooth_command_path` or `brew_command_path` variables with your own

![](./screenshots/settings_3.jpg)


Easy peasy! 😋

[last release link]: https://github.com/bmunoz89/alfred-wf-bluetooth-manager/releases/latest/download/Bluetooth.manager.alfredworkflow
85 changes: 57 additions & 28 deletions bluetooth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import time
from subprocess import CalledProcessError

from workflow import ICON_INFO, Workflow3
from workflow import ICON_INFO, ICON_ERROR, Workflow3
from workflow.notify import notify
from workflow.util import run_command, set_config

Expand All @@ -31,7 +31,10 @@
class BluetoothManager:

__BREW_COMMAND_PATH = '/usr/local/bin/brew'
__BREW_ENVIRONMENT_KEY = 'brew_command_path'

__BLUETOOTH_COMMAND_PATH = '/usr/local/bin/blueutil'
__BLUETOOTH_ENVIRONMENT_KEY = 'bluetooth_command_path'

def __init__(self, wf):
self._wf = wf
Expand All @@ -42,41 +45,66 @@ def __init__(self, wf):
self._action = self._args[0]
self._action_args = self._args[1:]

self._set_bluetooth_command_path()
try:
self._set_commands_path()

self.main()
self.main()
except Exception as error:
message = 'Something went wrong'
if isinstance(error, OSError) and error.errno == 2:
message = 'Change your blueutil or brew path'

def _set_bluetooth_command_path(self):
if os.path.exists(self.__BLUETOOTH_COMMAND_PATH):
log.debug('blueutil command path exists')
return
log.error(message, exc_info=error)
self._wf.add_item(
title=message,
subtitle='Press enter to go to the help page',
icon=ICON_ERROR,
arg='workflow:help',
valid=True)
self._wf.send_feedback()

bluetooth_command_path = self._wf.stored_data('bluetooth_command_path')
if bluetooth_command_path is not None:
log.debug(
'blueutil command path stored in "%s" was restored from stored data' %
bluetooth_command_path)
def _set_commands_path(self):
bluetooth_command_path = os.getenv(self.__BLUETOOTH_ENVIRONMENT_KEY)

if bluetooth_command_path is None:
set_config(self.__BLUETOOTH_ENVIRONMENT_KEY, self.__BLUETOOTH_COMMAND_PATH)
bluetooth_command_path = self.__BLUETOOTH_COMMAND_PATH

if os.path.exists(bluetooth_command_path):
self.__BLUETOOTH_COMMAND_PATH = bluetooth_command_path
return
log.debug('blueutil command path exists')
return True

brew_command_path = os.getenv(self.__BREW_ENVIRONMENT_KEY)

if brew_command_path is None:
set_config(self.__BREW_ENVIRONMENT_KEY, self.__BREW_COMMAND_PATH)
brew_command_path = self.__BREW_COMMAND_PATH

if not os.path.exists(self.__BREW_COMMAND_PATH):
log.error('brew command path not found')
return
log.error('brew command path not found: %s' % brew_command_path)
return False

self.__BREW_COMMAND_PATH = brew_command_path

bluetooth_command_path = self._run_command([
brew_prefix_path = self._run_command([
self.__BREW_COMMAND_PATH,
'--prefix',
])

if bluetooth_command_path is None:
log.error('blueutil command path not found')
return
if brew_prefix_path is None:
log.error('brew prefix path not found')
return False

bluetooth_command_path += os.path.join(bluetooth_command_path, '/bin/blueutil')
bluetooth_command_path = os.path.join(brew_prefix_path, 'bin/blueutil')
if os.path.exists(bluetooth_command_path):
log.debug('blueutil command path stored in "%s"' % bluetooth_command_path)
self._wf.store_data('bluetooth_command_path', bluetooth_command_path)
set_config(self.__BLUETOOTH_ENVIRONMENT_KEY, bluetooth_command_path)
self.__BLUETOOTH_COMMAND_PATH = bluetooth_command_path
return True

log.error('blueutil command path not found: %s' % bluetooth_command_path)
return False

def _run_command(self, command):
log.debug('Command: "%s"' % ' '.join(command))
Expand Down Expand Up @@ -111,8 +139,6 @@ def main(self):
else:
action_method(*self._action_args)

return 0

def action_manager(self, is_on, selected_option=None):
if selected_option is None:
if is_on:
Expand Down Expand Up @@ -153,7 +179,7 @@ def action_manager(self, is_on, selected_option=None):
if selected_option_method is not None:
selected_option_method()
else:
raise Exception('"bm" action not')
raise Exception('"bm" action not found')
self._wf.send_feedback()

def _parse_device(self, raw_device):
Expand Down Expand Up @@ -299,10 +325,13 @@ def manager_clear_data(self):


if __name__ == '__main__':
wf = Workflow3(update_settings={
'github_slug': GITHUB_SLUG,
'frequency': UPDATE_FREQUENCY,
})
wf = Workflow3(
update_settings={
'github_slug': GITHUB_SLUG,
'frequency': UPDATE_FREQUENCY,
},
help_url='https://github.com/' + GITHUB_SLUG + '#-help',
)
log = wf.logger

can_update = os.getenv('can_update', 'true') == 'true'
Expand Down
2 changes: 1 addition & 1 deletion info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ brew install blueutil</string>
</dict>
</dict>
<key>version</key>
<string>2.2.0</string>
<string>2.3.0</string>
<key>webaddress</key>
<string>https://github.com/bmunoz89/alfred-wf-bluetooth-manager</string>
</dict>
Expand Down
Binary file added screenshots/command_error.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/settings_1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/settings_2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/settings_3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit abfad87

Please sign in to comment.