Jump to content

Search Top level folders only using script filter - workflow


Recommended Posts

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 by JorgeLuisBorges
Link to comment

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 by juliosecco
Link to comment

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 :)

Link to comment

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

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 by effe
Link to comment

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 &, <, >.

Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...