JorgeLuisBorges Posted May 14, 2015 Posted May 14, 2015 (edited) Hi! I'm an alfred noob, and not much of a programmer - so I was wondering if someone could help. I'm looking for a way to search top level folders in a specific directory only - and exclude its children - I'm guessing with a script filter. Context - We do a lot of development locally and I am often having to type in http://localhost/foldername to open the local instance of a website. Because there are so many its a bit of a pain in the proverbial. I've managed to cobble together a script that searches my localhost folder and opens the folder name (as $filename) at http://localhost/$filename I'm using the file filter, which returns all the sub folders in my localhost - I'm guessing I need to write a script filter that ignores sub folders and only looks for top-level directories in the localhost directory. thanks! Edited May 14, 2015 by JorgeLuisBorges
Vero Posted May 14, 2015 Posted May 14, 2015 Hi!! Welcome to the forum! I've just moved your thread to the "Workflow help & questions" section where you're more likely to get the right people's eyeballs on your topic Cheers, Vero JorgeLuisBorges 1
juliosecco Posted May 14, 2015 Posted May 14, 2015 (edited) Hi Jeorge, the solution I can give you is so a 'dirty' hack that I'm almost shameful telling you. I had a similar need, and didn't have time to look for an apposite script, what I have done is this: added to all the root site folders an unique tag, something like 'rootfolder' in Yosemite you can select multiple folders and set a tag once to all of them, don't know in previous versions in the advanced tab of the file filter, on the fields list, I have added the kMDItemUserTags field, and put in the value rootfolder ( the tag ) now the file filter searches using your search critera, but also check that the items found contains the tag rootfolder, which only the root folders have, so only them are returned don't know if is a solution for you... Giulio Edited May 14, 2015 by juliosecco
JorgeLuisBorges Posted May 14, 2015 Author Posted May 14, 2015 Filth! I think that might be a bit annoying, I have about 100 folders! I like the lateral thinking though.
Vero Posted May 14, 2015 Posted May 14, 2015 Hi Jeorge, the solution I can give you is so a 'dirty' hack that I'm almost shameful telling you. I had a similar need, and didn't have time to look for an apposite script, what I have done is this: added to all the root site folders an unique tag, something like 'rootfolder' in Yosemite you can select multiple folders and set a tag once to all of them, don't know in previous versions in the advanced tab of the file filter, on the fields list, I have added the kMDItemUserTags field, and put in the value rootfolder ( the tag ) now the file filter searches using your search critera, but also check that the items found contains the tag rootfolder, which only the root folders have, so only them are returned don't know if is a solution for you... Giulio If you're setting tags on files in Yosemite, you can simply use the "tags" keyword in Alfred to search them - unless you want to specify the scope, then a file filter is best
JorgeLuisBorges Posted May 14, 2015 Author Posted May 14, 2015 I didn't know you could set multiple tags at once - its a good workaround - thanks. Would still be interested in a more programatic solution though. thanks
juliosecco Posted May 14, 2015 Posted May 14, 2015 HI Jorge, I have played a little with this workflow idea, and I have wrapped out some code, I think I could publish this as a working workflow some day... here you have some code to play with: it's in applescript, so you should begin with a script filter, type osascript, with this code: set folderName to POSIX file "/Users/juliomacr/Sites" global elencocartelle set elencocartelle to "" process_folder(folderName) on process_folder(folderNameToProcess) tell application "Finder" set theItems to every folder of folder (folderNameToProcess) repeat with theItem in theItems if the name of theItem contains "{query}" then set elencocartelle to elencocartelle & "<item uid='" & the kind of theItem & "' arg='http://localhost/" & the name of theItem & "'><title>" & the name of theItem & "</title></item>" end if end repeat end tell end process_folder set elencocartelle to "cat << EOB <?xml version='1.0'?> <items> " & elencocartelle & "</items> EOB" do shell script elencocartelle the script accepts the input, searches for the folders matching, and prepares the xml as the alfred output obviously you must change the first row of the scrip to match your 'sites' folder then the selected row ( the alt tag of the xml ) is passed to an 'Open URL' action, that simply contains as url {query}, thats the string passed from the previous script I must work on generalize the sites folder handling, and the local url handling that could not be always http://localhost/folder anyway, that's a start... have a good evening Giulio
effe Posted May 14, 2015 Posted May 14, 2015 (edited) Here's another way to do it: ** edit ** I've uploaded a link to the workflow https://www.dropbox.com/s/33szngyros5nvhp/open%20%20localhost%20sites.alfredworkflow?dl=0 by default, it opens the folder, and cmd opens the url - Create a new workflow - Add a script filter - add your keyword, with space, argument optional - make sure language is /bin/bash - de-select all of the escaping options in the script window, add baseDir="/absolute/path/to/folder/" ./shallowDirectorySearch.sh "$baseDir" "{query}" - open the workflow folder and create shallowDirectorySearch.sh - make it executable ( chmod +x shallowDirectorySearch.sh ) - type or copy pasta the following into the file: shallowDirectorySearch.sh #!/bin/bash baseDir="$1" query="$2" out="$(ls -d "${baseDir}"*${query}*/)" echo '<?xml version="1.0" encoding="utf-8"?>' echo "<items>" echo "$out" | while IFS= read -r line do escapedline="$(echo $line | sed 's/ /\\\ /g' )" base="$(basename "$escapedline" )" echo '<item valid="yes">' echo '<title>'$base'</title>' echo '<subtitle>'$line'</subtitle>' echo '<arg>'$line'</arg>' echo '</item>' done echo "</items>" exit - add a run script output and connect the events query="{query}" escapedline="$(echo $query | sed 's/ /\\\ /g' )" base="$(basename "$escapedline" )" open "http://localhost/$base" have fun! Edited May 14, 2015 by effe
deanishe Posted May 22, 2015 Posted May 22, 2015 Hi effe, That doesn't work quite right for a couple of reasons. Because of the leading *, you won't get a match if your query matches the beginning of a folder name. That is to say, "test" will not match "Test", you have to enter "est" instead. $base and $line also need to be wrapped in <![CDATA[]]> tags in the XML output, otherwise the workflow will die in flames if a filename contains &, <, >. effe 1
effe Posted May 23, 2015 Posted May 23, 2015 (edited) Who capitalizes directory names? Updated the workflow. Fingers crossed. For anyone following along: you need to open the workflow and update the baseDir in the script filter to point to your desired directory. Same link as previous post: https://www.dropbox.com/s/33szngyros5nvhp/open%20%20localhost%20sites.alfredworkflow?dl=0 @deanishe, thank you for the pointers. Edited May 23, 2015 by effe
Jacob Graf Posted October 13, 2024 Posted October 13, 2024 Why is this still so hard in 2024? Does anybody else have a simpler way to search and return only top-level directories within ~/Sites? This really shouldn't be this hard. Why doesn't Alfred File Filter have an option to include/exclude subfolders?
Vero Posted October 13, 2024 Posted October 13, 2024 @Jacob Graf You can pop up Alfred and type the ~ tilde to open File Navigation and see your top-level directories. What specifically are you looking to achieve?
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