Skip to content

Commit

Permalink
Add option to remove URL query params; add support for Universal Actions
Browse files Browse the repository at this point in the history
- Also store default actions in main Python file instead of a its own JSON file
  • Loading branch information
pnlng committed Aug 8, 2021
1 parent 87869e6 commit f5987ca
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 11 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ does things with them. For example, it can

- Copy link in Markdown
- Add a task to OmniFocus Pro
- Clean up Amazon URLs
- Optionally remove URL query parameters

The actions are also customizable. It can

Expand Down Expand Up @@ -37,6 +37,7 @@ macOS notification</p>
- [Screenshots](#screenshots)
- [Contents](#contents)
- [Usage](#usage)
- [PopClip](#popclip)
- [Configuration](#configuration)
- [Variables](#variables)
- [Custom Actions](#custom-actions)
Expand All @@ -51,7 +52,13 @@ macOS notification</p>

## Usage

Select the desired URL (<kbd>⌘L</kbd> to get the URL of the current tab in a browser). Fire up the workflow's associated hotkey, which is <kbd>⇧^C</kbd> by default.
Select the desired URL (<kbd>⌘L</kbd> to get the URL of the current tab in a browser). Fire up the workflow's associated hotkey, which is <kbd>⇧^C</kbd> by default.

- Pressing <kbd>↵</kbd> will either copy the formatted string or open the URL scheme action.
- Pressing <kbd>⌘↵</kbd> will remove query parameters from the URL.
- Example: `https://www.alfredapp.com/?param=something` becomes `https://www.alfredapp.com/`

### PopClip

Alternatively, if you use [PopClip](https://pilotmoon.com/popclip/), you trigger Alfred through the PopClip extension.

Expand Down Expand Up @@ -82,9 +89,7 @@ You can supply custom actions that override the default actions in one of the fo
1. Put a JSON file in the workflow data directory (accessible via the keyword `urlact`). Let the value of `CUSTOM_ACTIONS_FILE` be the name of the file.
2. Let the value of `CUSTOM_ACTIONS` be a JSON string.

For a template, see [`default_actions.json`](https://github.com/pnlng/alfred-url-actions/blob/master/default_actions.json).

_Please do **not** directly edit the `default_actions.json` file in the workflow directory, as user changes will be overridden when the workflow is updated._
For a template, see [`actions_template.json`](https://github.com/pnlng/alfred-url-actions/blob/master/actions_template.json).

Example:

Expand Down
Binary file modified URL Actions.alfredworkflow
Binary file not shown.
38 changes: 32 additions & 6 deletions actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,35 @@
import json
from workflow import Workflow

DEFAULT_ACTIONS = [
{
'action_title': 'Copy as Markdown link',
'output': u'[{title}]({url})'
},
{
'action_title': 'Copy as Markdown list item',
'output': u'- [{title}]({url})'
},
{
'action_title': 'Add to OmniFocus',
'action_subtitle': '',
'output': u'omnifocus:///add?name={title}&note={url}',
'encode': True
},
{
'action_title': 'Copy Title',
'output': u'{title}'
},
{
'action_title': 'Copy URL',
'output': u'{url}'
},
{
'action_title': 'Copy HTML link',
'output': u'<a href="{url}">{title}</a>'
}
]

# Applescript stuff from Dr. Drang: https://leancrew.com/all-this/2013/03/combining-python-and-applescript/


Expand Down Expand Up @@ -53,12 +82,13 @@ def main(wf):
clean_amazon = os.environ.get('CLEAN_AMAZON')
if clean_amazon and clean_amazon.lower() == 'true':
url = clean_url(url)
actions = DEFAULT_ACTIONS
custom_actions_file = os.environ.get('CUSTOM_ACTIONS_FILE', '')
custom_actions = os.environ.get('CUSTOM_ACTIONS', '')
if custom_actions_file:
custom_file_path = wf.datafile(custom_actions_file)
if os.path.isfile(custom_file_path):
with open(custom_file_path, "r") as f:
with open(custom_file_path, 'r') as f:
try:
actions = json.load(f)
except ValueError:
Expand All @@ -72,10 +102,6 @@ def main(wf):
actions = json.loads(custom_actions)
except ValueError:
raise ValueError('The value of "CUSTOM_ACTIONS" is malformed.')
else:
config = wf.workflowfile('default_actions.json')
with open(config) as f:
actions = json.load(f)
for action in actions:
# Quote title if url is to be opened
if action.get('encode', False):
Expand All @@ -94,6 +120,6 @@ def main(wf):
return 0


if __name__ == u"__main__":
if __name__ == u'__main__':
wf = Workflow()
sys.exit(wf.run(main))
File renamed without changes.
11 changes: 11 additions & 0 deletions remove_query_params.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/python
# encoding: utf-8

import os
import re
import sys

result = sys.argv[1]
raw_url = os.getenv('raw_url')
clean_url = re.sub(r'\?.*', '', raw_url)
sys.stdout.write(result.replace(raw_url, clean_url))

0 comments on commit f5987ca

Please sign in to comment.