Jump to content
Neberheim

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?

Share this post


Link to post
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.

Share this post


Link to post

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

Share this post


Link to post

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.

Share this post


Link to post
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

Share this post


Link to post
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).

Share this post


Link to post

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.

Share this post


Link to post

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!

Share this post


Link to post
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).

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...