Giter Club home page Giter Club logo

Comments (3)

deanishe avatar deanishe commented on July 28, 2024

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:

  1. Add a workflow function that symlinks the appropriate script(s) to somewhere on PATH (probably /usr/local/bin)
  2. Upload your CLI version to PyPI as a standalone program in addition to bundling it with your workflow.

from alfred-workflow.

fractaledmind avatar fractaledmind commented on July 28, 2024

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.

deanishe avatar deanishe commented on July 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.