76: Automating with Alfred

I did some digging and found someone else had created a tagging workflow using James Berry’s tag command at GitHub - StarBax89/alfredworkflow.addatag: Alfred 3 workflow for tagging files with Alfred. One caveat is that I needed to edit the Applescript from this:
set theFilePath to POSIX path of (thisPath as text)
to this:
set theFilePath to quoted form of the POSIX path of (thisPath as text)
as noted in the “issues” section of the Github page.

So now I can tag files from within Alfred. Also because my tags have multiword names, e.g. Send to Archive, I have to enclose them in single-quotes. And to cut down on the amount of typing I have to do I set up Textexpander snippets for each tag name. Voilà!

Thanks for the help.

As Dom mentioned, one of my favorite features is a feature that I just did not understand for the longest time.

I had always understood the Alfred file workflow to be:

  1. Invoke Alfred
  2. type the name of a file until Alfred finds the right doc
  3. Right arrow to get to the action menu, where I can do things like move the file, email it, Open With, etc.

All well and good, but sometimes I want to do the same thing before I invoke Alfred, to a file that I am staring at in a finder window or on my desktop. This is the feature Dom is talking about.

My regular Alfred trigger is the standard ⌘Space. But if I want to jump right to the actions menu for files that I have already selected in the finder, I have mapped ⌥⌘ Space.

My favorite use of this is cleaning up a cluttered desktop of downloads folder. I can select all the files that I want to move to one folder, ⌥⌘ Space, select move, type in the name of the destination file and hit Return. Boom, they are gone! I don’t have to open up a 2nd finder window every time I want to move files. And as I said, this is very, very handy when dealing with multiple files at the same time.

I hope this helps!

2 Likes

Loved the episode. Just came here to say one of my most used workflows is probably one of the simplest, think I got it from one of the Alfred forums forever ago. It uses a hot key to get straight to Alfred’s prefs. One keystroke instead of 2! Maybe shaving milliseconds each time but might have helped @MacSparky and @RosemaryOrchard every time they had to say ‘command space, command comma’ :grin: Great show guys, reminded me why I love Alfred and gave me some new ideas!

Thanks for the suggestion. Taking a quick look around suggests that text in the subject and body needs to be % escaped, which would be a pain. But it also led me to this site https://mailtolink.me/ which looks interesting. Not sure whether I could drive that from a script.

As a long term project, a workflow that asks for email, subject and body and constructs the link would be something to aim at. For now, I’ll just keep doing it manually in plain text.

This is the site I used for my first mailto links :slight_smile:

If you happen to know python, here is some code to generate a link for you. Alfred can run Python. But of course you can use your language of choice. Important is just the function that “quotes” text, ie the % escaping.

import urllib.parse

def generateMailtoLink(to, cc, subject, body):
    
    mailparts = {
        'to': to,
        'cc': cc,
        'subject': subject,
        'body': body
    }

    for key, value in mailparts.items():
        mailparts[key] = urllib.parse.quote(value)
            
    mailToLink = "mailto:{to}?cc={cc}&subject={subject}&body={body}".format(**mailparts)
            
    return mailToLink

If you want to design a workflow, you need to think how you want to handle the input, ie do your want four promt individual prompts for each field (assuming to, cc, subject, body) or do you want one promt, and parse your input into four parts.

I would suggest using four Alfred prompts, storing each part in an Alfred variable and using a script action after the last one to generate a link and adding it to your task manager of choice.

1 Like

Great episode! I thought I knew Alfred pretty well, but I still picked up lots of good ideas. Here are some of my most-used Alfred actions that I don’t think were mentioned:

  • Built-in features:

    • Copy Path to Clipboard. I use this constantly when writing scripts where I need to pull in data from other files.

    • Dictionary. Sometimes I can’t remember how to spell a word, so I just type “spell” and then start typing a word to quickly access macOS’ built-in dictionary. Choose the word from the suggestions, hit return, and the word is copied to the clipboard.

  • Workflows. So many good ones. Some that I use regularly:

    • Quick file access. I have several that take me directly to documents I often want to look at, e.g. my “Today” to-do list in Drafts, or my year planning calendar in Excel

    • CaseConverter by dfay: Amazing little tool. Say you copy something that is written in ALL CAPS and you want to convert it lower case, or sentence case, or title case… just type cc, and CaseConverter does it.

    • Add to Due (by the maker of Due, Lin Junjie). I think of something I need to remember, and it’s just cmd-space “due”, write the item and time, hit return, and it is saved in Due.

1 Like

Glad to see CaseConverter getting some love! I built it to solve my own issues & use it daily.

Wow, thanks for that. I’ve just started using python, so making that work would be a great exercise for me.

I already know how to append text to a file :grinning:

1 Like

Copy Path to Clipboard is very handy. I also use Copy name by Maclej Skrzyypczak, tied to hyper-C, to pass the names of files around the place.

1 Like

Not Alfred but Cardhop has a nice feature for this: Invoke with keyboard shortcut and just type mail followed by the name of the person in your contacts you want to mail. Works with call, FaceTime etc too.

If you mean just open a window to a write a blank email message without opening the Main Mail window, a URL could do the trick mailto: with no further info.

In order to use that into Alfred, you could:
(1) create a Custom Web Search (if you rather want to type a command on Alfred window); or
(2) create a workflow (if you want it to be triggered by a shortcut or snippet)

The fastest rout will be to create a custom search like this one:

Then, every time you type email into Alfred, you’ll get that window waiting for you to type on.

The trick here is that “Custom Searches” can be used even without an argument to it, just to invoke an URL. In fact, that same trick can be done for every single URL you want Alfred to have quick access.

Thanks. I’ve never really looked Cardhop because I don’t really do all that much with contacts. On the other hand, I have and love Fantastical, which I bought 6 years ago, so it might be worth considering a Premium subscription.

1 Like

Thanks for that. The problem is that it opens Mail.app which shows me the inbox, and that is what I want to avoid. I do not want to be tempted to check mail except during my “mail” time blocks.

I know the real answer is personal discipline, but that is in short supply here.

I loved this episode. I have a question about Alfred. How can I get it to open an app or file on the ACTIVE display and not the default display. I have changed the Alfred appearance options and Alfred itself will open it’s search box on the active display but from there when I launch an app it won’t launch it on the active display. Anyone know how to do that?

1 Like

I fell your pain. If you happen to have Keyboard Maestro on your tool belt, take a look here:

The trick for this macro to work is find a reliable way to set the “Focus” variable to on or off depending on your work routines. Mine is tied to Focus App: whenever a Focus session is starts/stops it will trigger a Keyboard Maestro Macro that will set this variable to on/off.

If your hours are more fixed, you could create a Macro that will turn the “Focus” variable on/off depending on certain hours. Or even use those Mail Hours instead of the “Focus” variable altoghether. Like this:

In that case, Mail will close its inbox window between 15:00 and 16:00. You can set your own schedule accordingly.

That’s very clever but frankly, a bit more than I want to get into. I don’t currently have KM, although I am sure that if I did get it I would find things to do with it. For now, I think the python solution will be a good one for me to use to learn too.

Surely.

Take a look at ScriptingBridge module. It allows for window manipulation if you want to reproduce the logic of those macros on Python.

1 Like

Wouldn’t Drafts be a good solution?

Sure, for many cases it totally is.

For me I’d rather stick to my Rube Goldbergian approach for two reasons:

  1. Using Mail’s compose lets me use its address autocompletion feature;

  2. It lets me open links to a specific message (which will open in ifs own window) without opening the inbox window.

Oooh…you want Keyboard Maestro, you just don’t realize it yet. Promise!

2 Likes