Jump to content
patgilmour

Easy Selection of Folders with Same Name Based on Path

Recommended Posts

I do a lot of WordPress development and so have a lot of folders called 'wp-content' in my Sites folder.

 

I have file filter that searches only within Sites, then I type "wp-co" and all the wp-content folders are listed. I can see their paths under their names and scroll to the one I want.

 

Not bad, but I'd like something even better.

 

What I'm really after is a file filter that when I type 'wp-cxc' will show me all the wp-content folders with "xc" (such as "Sites/xchange/wp-content/ ) in the path name.
Or 'wp-cap' for all wp-content folders that have "ap" in the path.

 

The problem I'm having is that Alfred doesn't search in the path name, so if I activate my filter and type, for example 'wp-cxchange' Alfred won't return the "wp-content" folder in the parent directory that contains 'xchange'.

 

So is there a way to do this with the basic Workflow building blocks or will I need a script?

 

Thanks!

Share this post


Link to post

I think the only way you could use Alfred's built-in components would be if you used tags or Spotlight comments to add the project name to each 'wp-content' folder.

 

You could try this workflow, which filters across directory levels, so you'd set up a Fuzzy Folder for ~/Sites and then "sites xch wp" (assuming "sites" is the keyword you assigned to ~/Sites) will match ~/Sites/xchange/wp-content/.

 

The best way would probably be to write a Script Filter, however.

 

This is a very rudimentary prototype, but if you use this as a Script Filter (Language = zsh, Escaping = Double Quotes, Backquotes, Dollars, Backslashes), it will do more or less what you want (filter on the project name, open the wp-content folder):

#!/usr/bin/env zsh

export LC_CTYPE=UTF-8
query="{query}"

function projname() {
  echo "$( echo "$1" | awk -F/ '{print $(NF-1)}' )"
}

function contains() {
  local string="${1:l}"
  local substring="${2:l}"
  if test "${string#*$substring}" != "$string"
  then
    return 0    # $substring is in $string
  else
    return 1    # $substring is not in $string
  fi
}

wpdirs=(${(f)"$(mdfind -onlyin ~/Sites '(kMDItemContentType == public.folder) && (kMDItemFSName == wp-content)')"})

if [[ -z "$query" ]]; then
  projects=($wpdirs)
else
  projects=()
  for p in $wpdirs; do
    pname="$( projname "$p" )"
    if contains "$pname" "$query"; then
      projects+="$p"
    fi
  done
fi


echo '<?xml version="1.0" encoding="utf-8"?>'
echo '<items>'

for proj in $projects; do
  pname="$( projname "${proj}" )"
  cat <<EOF
<item valid="yes" type="file">
  <title>${pname}</title>
  <subtitle>${proj}</subtitle>
  <arg>${proj}</arg>
  <uid>${proj}</uid>
  <icon type="fileicon">${proj}</icon>
</item>
EOF
done

echo '</items>'
Edited by deanishe

Share this post


Link to post

deanishe - thanks for the comprehensive reply!

 

I've had too long a day at work to tackle trying to use the script now - I'm fried - but will have a go tomorrow. Just wanted to say thanks.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...