Danny Hope Posted January 5, 2018 Share Posted January 5, 2018 I have a folder of files relating to various projects. That list of files changes all the time. I’d like to be able to open all of the files in that folder at once with just a single keyboard command. I don’t know how to tell Alfred to open all of the files in a folder. Instead, I’ve resorted to making an Apple Automator workflow (attached) which: goes to the specified folder, gets the list of files in that folder and then opens all of them. I’d prefer to drop the Automator workflow and have it work entirely within Alfred. Can this be done in Alfred without writing a script? If this can’t be done in without writing a script, can anybody give me a script that does this, i.e. goes to the specified folder, gets the list of files in that folder and then opens all of them? Thanks for your help ? Link to comment
deanishe Posted January 5, 2018 Share Posted January 5, 2018 1 hour ago, Danny Hope said: Can this be done in Alfred without writing a script? No. 1 hour ago, Danny Hope said: can anybody give me a script that does this Put this in a Run Script action with Language = /usr/bin/python from fnmatch import fnmatch import os from subprocess import call # Directory whose contents should be opened DIRPATH = '~/Dropbox/Current projects' # glob-style patterns for file-/dirnames to exclude from opening EXCLUDES = [ '.*', # dotfiles 'Icon\r', # directory icons ] # Replace ~ with real path to home directory root = os.path.expanduser(DIRPATH) for name in os.listdir(root): for pat in EXCLUDES: if fnmatch(name, pat): break else: # name didn't match any patterns call(['open', os.path.join(root, name)]) Danny Hope 1 Link to comment
politicus Posted January 6, 2018 Share Posted January 6, 2018 Another excellent workflow from @deanishe! Thank you very much ! Is there a way to change the value of the DIRPATH variable without having to modify it in the Run script action? Link to comment
deanishe Posted January 6, 2018 Share Posted January 6, 2018 Change the script to read DIRPATH = os.getenv('DIRPATH') and then set DIRPATH in the workflow variables configuration sheet. Link to comment
politicus Posted January 7, 2018 Share Posted January 7, 2018 @deanishe Ok. Thank you. I think your answer, while very helpful didn't answer my question 100% because I asked it in a to wrong way. So, here the more developed version: In my question, I was looking for a way to make the workflow work like a list filter. I would like to: 1°) trigger the workflow with a keyword 2°) be offered to choose a Folder path where the files I want to open are located in Alfred results 3°) hit enter to open all the files in their default application. The workflow that is the closest to what I am looking for is here. It lists all the potential folders but it still not working as, I guess, your code in the run script action doesn't know which folder to open. I am open to any solution, with or without environment variables Link to comment
deanishe Posted January 7, 2018 Share Posted January 7, 2018 4 hours ago, politicus said: as, I guess, your code in the run script action doesn't know which folder to open. That's because the script doesn't accept command-line arguments, which is what you get from a Script Filter. Ensure that the "with input as argv" option is selected for your Run Script (which it is in your workflow), then add import sys to the top of the script with the other import statements, and change the DIRPATH line to DIRPATH = sys.argv[1]. Link to comment
politicus Posted January 8, 2018 Share Posted January 8, 2018 (edited) It is working very well. Thank you! One more question. Let's imagine there is a folder in the folder I select, will it open. And, if yes, would it recursively open subfolders of this folder? Edited January 8, 2018 by politicus Link to comment
deanishe Posted January 8, 2018 Share Posted January 8, 2018 15 hours ago, politicus said: there is a folder in the folder I select, will it open. Yes, unless its name matches one of the EXCLUDES patterns. 15 hours ago, politicus said: would it recursively open subfolders of this folder? No. Wouldn't it have been faster to test that for yourself? Link to comment
Danny Hope Posted January 8, 2018 Author Share Posted January 8, 2018 Cheers so much @deanishe, that works great! Link to comment
politicus Posted January 9, 2018 Share Posted January 9, 2018 @deanishe to get the workflow I wanted, I tried many options. Two of them ended up opening so many sub-folders, I had to restart my Mac. I then preferred to ask you FIRST, rather than messing one more time. Link to comment
deanishe Posted January 9, 2018 Share Posted January 9, 2018 3 hours ago, politicus said: to ask you FIRST, rather than messing one more time. Makes sense. A smarter way would be to create some new test data and try the workflow (or whatever else) on that. Apart from being quicker than asking, I may have misunderstood the question (for example, I’m still not sure whether I was supposed to write a script that only opened files, rather than files and directories), and I may have made a mistake. The proof of the pudding, and all that… (Certainly, when testing the script, I used a very small, throwaway directory tree.) politicus 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