Comments (3)
So, first of all, it is most definitely—by definition—outside of the purview of Alfred-Workflow. That's why it's called Alfred-Workflow, not Alfred-Workflow-and-also-Command-Line-Apps… 😉
That notwithstanding, read on…
It's entirely trivial for a script to tell if it has been called from a shell.
Alfred-Workflow already does this: if you look at the end of Workflow.args()
(around line 1090), you'll see that it doesn't output XML if it's been called from a shell.
Alfred-Workflow can't do anything generally meaningful in a command-line context with its output. However, optionally generating CLI output is a couple of lines in your workflow's code:
if sys.stdout.isatty():
# Command line stuff here
else:
# Alfred stuff here
Alfred-Workflow also needs to output XML if run from a shell, so you can check/verify its XML.
If something makes sense as both a workflow and a command-line application, then you should do it properly and either implement it as a library and add separate workflow and command-line front-ends to it, or build it as a command-line program in the first place and base the workflow around that.
The latter is what I tend to do with most of my workflows, anyway. It moves complexity from Alfred's workflow UI (which rapidly becomes a PITA as things get complicated) into my code editor, which is much more capable.
For example, version 2 of my MailTo workflow is basically a wrapper around the mailto.py
script, which should work just fine as a command-line program on its own. To simplify the workflow in Alfred, it even handles escaping all the arguments itself instead of relying on Alfred to do so.
As you mention, it's trivial enough to tell if the script is being called from Alfred via Alfred's environmental variables (as opposed to from another non-shell app). A simpler and more explicit alternative in that case, however, might be to add an --alfred
argument that specifies Alfred XML output and otherwise defaults to text output: if you've written something that is truly useful as a command-line app, it might make more sense to treat it as such and Alfred as the special case.
As for adding a workflow's directory to PATH
, I think that's a recipe for disaster. It's quite possible that a user has 20+ workflows using Alfred-Workflow (I know of at least 40 workflows that use it), And it's quite likely that many of them will have clashing script names, seeing as workflow authors quite reasonably assume their workflows run in isolation. How many update.py
scripts will end up on your PATH
that way?
There are two ways to enable your workflow to be used as a CLI program that I would consider correct:
- Add a workflow function that symlinks the appropriate script(s) to somewhere on
PATH
(probably/usr/local/bin
) - Upload your CLI version to PyPI as a standalone program in addition to bundling it with your workflow.
from alfred-workflow.
Hmm... Lot's of great points.
First, I'm happy to see that I now have a much better sense of your likely response ;)
Second, you are right, I probably need to rewrite a couple of workflows as CLI programs, and then write Alfred stuff on top of that.
Third, I like thinking of Alfred as the special case in the instances where I do want a standalone CLI program.
Fourth, this will require some work on my end. :)
from alfred-workflow.
First, I'm happy to see that I now have a much better sense of your likely response ;)
Well, you're gaining a better sense of the goals of Alfred-Workflow (which I suppose are my goals for Alfred-Workflow*), and more importantly, the Zen of Python 😉
I probably need to rewrite a couple of workflows as CLI programs, and then write Alfred stuff on top of that.
Seriously, this is soooo much easier. Especially when you're looking at a workflow with a large amount of functionality, which is basically your trademark.
Instead of messing around with dozens of elements in Alfred's UI, you can replace most of the "second level" with one Run Script and have your Script Filter generate the appropriate flags and arguments to your CLI program. pipes.quote()
is your friend.
* I'm always open to persuasion wrt Alfred-Workflow's scope and goals, but "small and broadly useful" and "proper Python" are the principles I hold most dearly.
from alfred-workflow.
Related Issues (20)
- Cache Image HOT 5
- Basic auth HOT 3
- Pass parameter to subprocess HOT 5
- Tutorial options for keywords need to be updated for Alfred 4 HOT 11
- set_config raises error when the bundle id is null HOT 4
- Setting only arg on Variables adds line break HOT 1
- will it support python3? HOT 1
- python3 has no cpickle HOT 1
- cant get output HOT 11
- chr() arg not in range(256) error when trying to use Beautiful Soup 4 HOT 1
- workflow:magic not working?
- API functionality question
- AlertCautionIcon.icns does not exist on Big Sur
- ERROR: [Script Filter] JSON error
- Google SDK
- Can't get Script Filter to find the pinboard.py file from the tutorial HOT 1
- [Feature request] Possible to open bookmarks from root?
- Not working on the latest MacOS 12.3 HOT 11
- How to fetch chrome cookie?
- Issues with notify.notify in release version HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from alfred-workflow.