diff --git a/README.md b/README.md index a57c9b1..4986fba 100644 --- a/README.md +++ b/README.md @@ -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 @@ -37,6 +37,7 @@ macOS notification

- [Screenshots](#screenshots) - [Contents](#contents) - [Usage](#usage) + - [PopClip](#popclip) - [Configuration](#configuration) - [Variables](#variables) - [Custom Actions](#custom-actions) @@ -51,7 +52,13 @@ macOS notification

## Usage -Select the desired URL (⌘L to get the URL of the current tab in a browser). Fire up the workflow's associated hotkey, which is ⇧^C by default. +Select the desired URL (⌘L to get the URL of the current tab in a browser). Fire up the workflow's associated hotkey, which is ⇧^C by default. + +- Pressing will either copy the formatted string or open the URL scheme action. +- Pressing ⌘↵ 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. @@ -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: diff --git a/URL Actions.alfredworkflow b/URL Actions.alfredworkflow index 5272f4c..45f7eae 100644 Binary files a/URL Actions.alfredworkflow and b/URL Actions.alfredworkflow differ diff --git a/actions.py b/actions.py index 3a86d72..70b5d3a 100644 --- a/actions.py +++ b/actions.py @@ -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}¬e={url}', + 'encode': True + }, + { + 'action_title': 'Copy Title', + 'output': u'{title}' + }, + { + 'action_title': 'Copy URL', + 'output': u'{url}' + }, + { + 'action_title': 'Copy HTML link', + 'output': u'{title}' + } +] + # Applescript stuff from Dr. Drang: https://leancrew.com/all-this/2013/03/combining-python-and-applescript/ @@ -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: @@ -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): @@ -94,6 +120,6 @@ def main(wf): return 0 -if __name__ == u"__main__": +if __name__ == u'__main__': wf = Workflow() sys.exit(wf.run(main)) diff --git a/default_actions.json b/actions_template.json similarity index 100% rename from default_actions.json rename to actions_template.json diff --git a/remove_query_params.py b/remove_query_params.py new file mode 100644 index 0000000..004f768 --- /dev/null +++ b/remove_query_params.py @@ -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))