Jump to content

How to learn to write workflows?


Recommended Posts

I would consider myself a power user, but when it comes to programming about the most I know is basic command line stuff as well as slowly working my way through Apple’s Swift playground. I know there is documentation on the Alfred site about how to create workflows, but it still seems geared towards people who know what they are doing.

I would really like to create a workflow to manage my VPN (NordVPN), among others, but I don’t even know where to begin. Do I need to learn Python? Bash? Keep plugging away with Swift? Something else? Any advice would be appreciated.

Edited by Neberheim
Fragment from a thought I never finished?
Link to comment
4 hours ago, Neberheim said:

but it still seems geared towards people who know what they are doing. Similar to how people say

 

The Alfred docs are limited to the workflow-y bits. The rest of building a workflow is UNIX command-line programming. Realistically, that's way beyond the scope of Alfred's docs, as it's a very broad topic. There are fundamentals that apply across languages, like environment variables, STDIN, STDOUT & STDERR and ARGV (command-line arguments), but really you should be off learning how to write Python or Ruby (or Swift).

 

Swift is a fine language—with several advantages on the Mac over non-Apple languages—and runs fast. You could use just about any language, however, and I'm not sure I'd recommend Swift as a first language (unless you want to write Mac/iOS apps): the language itself is a good one, but Apple's (Mac) libraries that it uses are complicated and badly documented. Indeed, the docs are similar in nature to Alfred's: they're great if you already know what's going on and not much help at all if you don't…

 

You get much more done more quickly with a language like Ruby or Python, and they're more flexible, too. Python in particular is a great first language with lots of docs aimed at beginners. Unfortunately, Apple ships a really old version of Python 2, not the current Python 3, and there are some important differences between the two. I'm not sure I'd recommend learning Python 2 at this point (it's dead).

 

As regards a workflow to manage your NordVPN connections, is this OpenVPN workflow no good?

 

If you want their IKEv2 VPN, you'll need to use Apple's APIs to manipulate your network settings. That can be done with AppleScript or Swift. AppleScript is probably a lot easier, if it's capable of what you need. Swift uses much lower-level APIs and will be much harder to use.

 

Here's an AppleScript to connect to a VPN via Network Preferences.

Link to comment

Hey @Neberheim. I also had and still have difficulty in writing Alfred workflows to solve my own problems but I got a little better at it over time.

 

I wrote an article on how you can start writing workflows using Go language and AwGo library. I find Go language really nice to work with and the amount of libraries you can use with Go is immense. 


The article goes step by step how you can use AwGo with some Go code to make Web Searches workflow as well my process in creating Alfred workflows. See if you like it. ? 

Edited by nikivi
Link to comment

Personally, I wouldn't recommend Go as a good first language. It's a fine language, but almost all the docs and tutorials assume a solid knowledge of programming in general.

 

AwGo is still in beta, and its API isn't stable yet. I'm 99% certain that the next update will break any workflow you've based on it.

Link to comment
37 minutes ago, deanishe said:

almost all the docs and tutorials assume a solid knowledge of programming in general

This can be solved by reading more on things and googling though. I also found that Go enforces a style in how you can write code which I feel is great for newcomers. Also the tooling you get, since Go is a statically typed language is amazing. Python has a lot more freedom but freedom means you are not sure what the 'best' way to do things is.

Edited by nikivi
Link to comment
1 minute ago, nikivi said:

This can be solved by reading more on things and googling though.

 

"Learn to code, then come back to this article" isn't much of a solution…

 

2 minutes ago, nikivi said:

I also found that Go enforces a style in how you can write code which I feel is great for newcomers. Python has a lot more freedom but freedom means you are not sure what the 'best' way to do things is.

 

That's not really true. Go enforces formatting, not how you actually program. The nature of the language itself encourages you to do things a certain way, but that is also true of Python. Both languages make it difficult to write the kind of unreadable code that Ruby and (especially) Perl are known for.


Go is a much smaller and simpler language than Python, but it's also much lower level, so one line of Python is often 5+ lines of Go. And Python's error handling and error messages are much more newbie-friendly.

 

IMO, idiomatic Python is easier to read that idiomatic Go (but not by much).

Link to comment

I also dislike how with writing Alfred workflows in Python, you are 'forced' to write them in Python 2 (which is soon to die) if you want to publish them to other people. With Go I get a nice binary I can just ship with the workflow.

Link to comment

Can you expand on this Python 2 and 3? I DL'd a trial of CodeRunner yesterday, since Atom doesn’t handle Swift and it has Python 3 as a language. Can I not use that with Alfred? Thanks a ton for the advice. I know it’s not exactly the purview of the forum, but maybe other users will find their way here wanting to foray into programming after getting hooked on Alfred!

Link to comment
39 minutes ago, Neberheim said:

Can you expand on this Python 2 and 3? I DL'd a trial of CodeRunner yesterday, since Atom doesn’t handle Swift and it has Python 3 as a language. Can I not use that with Alfred?

 

You can use almost any language to write a workflow because they use JSON/XML and standard UNIX interface. The issue with Python is that MacOS ships with a really old version of Python 2 built in, whereas Python 3 needs to be installed. So while Python 2 is effectively dead as a language, it Just Works on MacOS. That is an incredibly important factor if you want to share your workflows. (Python is an interpreted language, so you need the interpreter to run it. And it's too big to bundle with a workflow. Go generates standalone executables, so you don't need to bundle any interpreter/runtime—although the executables are huge compared to Python scripts.)

 

Apart from Python 2 being dead, the main difference is the way they handle strings. As far as writing workflows goes, Python 3 Just Works, while Python 2 requires you to be mindful of the difference between encoded strings and Unicode.

 

If you're going to learn Python now, you should learn Python 3. But that puts you at a disadvantage re workflows: they won't run on any system that doesn't have extra software installed (Python 3) and the available workflow libraries (e.g. my Alfred-Workflow) don't work with Python 3.

 

That said, not having a workflow library isn't the problem is was with version 2 of Alfred. You can easily live without a library using Python with Alfred 3. Go is a different matter, due to the way the language behaves. If you want to write a well-behaved Script Filter in Go, you probably should use a library like AwGo (or steal the relevant bits of code from it).

Link to comment
  • 2 years later...

@deanishe do you know where I can find the JSON interface? I'd like to write some workflows in Dart because I'm comfortable with it and would rather deal with the protocol directly. It also lets me build self-contained binaries as Go does but I find the language a bit easier to use than Go.

Edited by Venkat Dinavahi
Link to comment

Throwing in my two-cents, I learn best by dissecting other people's workflows and seeing how they work. Lately, I've been dissecting the Alfred Todoist Workflow which is showing me how someone wrote a workflow using Typescript. I obviously wouldn't recommend this workflow to someone just starting out, but since I code in typescript for my day job, it was quite interesting to see someone structure a workflow app using Typescript.

 

If you're just starting out, there are some great workflow examples built into Alfred. When you press the plus button in the workflows tab, there are "Getting Started" and "Example" workflows. 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...