MrMoose Posted May 19, 2022 Share Posted May 19, 2022 I've never had a Mac before and just got Alfred a few days ago; I'm blown away by all the features and by this wonderful community. I've made some simple workflows, but I'm not sure how to do a "if, then"-type workflow that checks a condition and acts accordingly, without additional user input. There are a lot I'd like to make once I get it down but here's the first example that came to mind: I'd like to use the keyword "is it friday?" to trigger a check of the day; if it is, in fact, Friday, the "Friday then, it's Saturday, Sunday" Mufasa video plays (I have it downloaded as an .mp4). Otherwise a negative response pops up, something like "nope ☹️ ". Is this something I can do within the Alfred Workflow setup alone or will I have to write a day-checking script or something? If so, how would I go about learning how to do this, especially for use in Alfred? I found the "conditional" utility block but got stuck after that. I've seen some very generous posts here so I also just wanted to mention that I'd rather do this myself - I'd like to learn how to program and this seems like a good intro project. Just need a bit of advice (unless it's way more complicated than I think...)! Thanks! Link to comment
vitor Posted May 20, 2022 Share Posted May 20, 2022 Welcome @MrMoose, and thank you for a clear post. In learning to program, being able to accurately describe a problem is a skill which will serve you exceptionally. 2 hours ago, MrMoose said: Is this something I can do within the Alfred Workflow setup alone Yes. No need for programming for this one. Set up an Argument and Variables Utility with {date:EEEE} as the argument (see documentation). That will pass along the day of the week to the next object, which will be your Conditional. Then you can use an Open File Action to play the file or a Post Notification Output otherwise. The Argument and Variables Utility and the Open File Action could easily be code instead. One line for each. I can walk you through those if you want, but they’re not particularly exciting. If you’re starting simple, the next one where you do need programming might be a better fit. MrMoose 1 Link to comment
MrMoose Posted May 20, 2022 Author Share Posted May 20, 2022 Thanks so much, @vitor. This was just enough info to send me down the right educational rabbitholes, much appreciated. Wouldn't mind seeing the code either if you don't mind. I really tried to do this without asking again but I'm stuck. Tried every combination I could think of (adding variables to the Arg/Vars utility like {date}, {date:EEEE}, {query}, etc., adding the same to the "if" blank in the Conditional, every combo of both...} I'm not sure if I need to add a variable, and what that would be: {date}, {date:EEEE}, {query}? My gut says no. I'm also assuming the output would still be "Friday" (vs. a representation in Unicode or something?) so that's what I've been putting in that spot. I know this is super simple in practice, just hung up on the minor points. Here's my best guess: https://github.com/MrMoose117/is-it-friday if you'd like to see (made a github account and learned how to create a repository, too, which is nice. Coolest signup process ever.). Thanks again! Link to comment
vitor Posted May 20, 2022 Share Posted May 20, 2022 Remove {date:EEEE} from the Conditional, leaving that field blank. It will default to {query}, which is the output from the previous object. Also, change your Keyword from Argument Required to No Argument. 8 hours ago, MrMoose said: I'm also assuming the output would still be "Friday" Correct. But don’t assume, use the debugger to see what data gets passed around. 8 hours ago, MrMoose said: Wouldn't mind seeing the code either if you don't mind. The code would be added to Run Script Actions with default settings. To get the day of the week, you’d use date +%A | tr -d '\n'. That’s a bit cryptic, admittedly. date +%A gives you the day of the week as an output; | is a pipe, it means “use the output of the command on the left as the input to the command on the right”; tr -d '\n' means “remove newline characters”. Necessary in this case because date outputs with a newline at the end and you don’t want that when comparing in the Conditional. Another way would be just date +%A then a Transform Utility to trim whitespace. To open the file with the default app, it would be open /PATH/TO/YOUR/FILE. To learn more about these, open a terminal and run man NAME_OF_TOOL (e.g. man tr; man open). On that new page, use the arrow keys to move up or down, / to search, and q to quit. Most tools have instructions you can check that way or via NAME_OF_TOOL -h (-h and --help being common help flags). Don’t worry about it if that’s too much to wrap around your head around at the moment, it’s the type of thing which clicks when you need it. MrMoose 1 Link to comment
MrMoose Posted May 20, 2022 Author Share Posted May 20, 2022 Perfect, thank you. The debugger is super helpful. I'm starting my coding journey with JavaScript this weekend and it's good to know Alfred has that as an option in the Run Script Action (down the line). Have a great weekend @vitor! Link to comment
vitor Posted May 20, 2022 Share Posted May 20, 2022 When you feel ready to apply the JavaScript in the context of macOS automation, check the official Mac Automation Scripting Guide (Apple’s documentation is pretty lacking in general, but that guide is good) and the JavaScript for Automation Cookbook wiki. MrMoose 1 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now