Jump to content

File filter results in grid view


Recommended Posts

Hi, i have several workflows to search through pdfs and images, and would like to use grid view for displaying results.

 

But i couldn't find a way to make it work. I saw "Thumbnail Navigation" workflow in alfred gallery, but it requires first selecting folder.

 

Is there any way to make it work "reactive" without unnecessary enter press?

Link to comment
21 hours ago, sinled said:

Is there any way to make it work "reactive" without unnecessary enter press?

 

There is not, by design¹. Think about that interface: you’d be typing something in Alfred and then the whole interface would change from under you to something different, and then you wouldn’t be able to continue typing to something else or go back² or else you would be able to do that but not filter in the view and have the interface going back and forth. The new views are separate paradigms, and to allow their full potential there can’t be ambiguity.


That said, while showing all files would have major performance implications, listing a specific file type is more doable. All you need is a Run Script Action before the Grid View set to /usr/bin/swift as the Language with this code (quick adaptation from another workflow):

 

import Foundation

// Change this to what you want to search
let query = "kMDItemContentType == com.adobe.pdf"

// LEAVE UNTOUCHED FROM HERE
// Prepare query
let searchQuery = MDQueryCreate(kCFAllocatorDefault, query as CFString, nil, nil)
MDQuerySetSearchScope(searchQuery, [("~" as NSString).expandingTildeInPath] as CFArray, 0)

// Run query
MDQueryExecute(searchQuery, CFOptionFlags(kMDQuerySynchronous.rawValue))
let resultCount = MDQueryGetResultCount(searchQuery)

// No results
guard resultCount > 0 else {
  print(
    """
    {\"items\":[{\"title\":\"No Results\",
    \"subtitle\":\"No paths found matching query\",
    \"valid\":false}]}
    """
  )

  exit(EXIT_SUCCESS)
}

// Prepare items
struct ScriptFilterItem: Codable {
  let uid: String
  let title: String
  let subtitle: String
  let type: String
  let icon: FileIcon
  let arg: String

  struct FileIcon: Codable {
    let path: String
  }
}

let sfItems: [ScriptFilterItem] = (0..<resultCount).compactMap { resultIndex in
  let rawPointer = MDQueryGetResultAtIndex(searchQuery, resultIndex)
  let resultItem = Unmanaged<MDItem>.fromOpaque(rawPointer!).takeUnretainedValue()

  guard let resultPath = MDItemCopyAttribute(resultItem, kMDItemPath) as? String else { return nil }

  return ScriptFilterItem(
    uid: resultPath,
    title: URL(fileURLWithPath: resultPath).lastPathComponent,
    subtitle: (resultPath as NSString).abbreviatingWithTildeInPath,
    type: "file",
    icon: ScriptFilterItem.FileIcon(path: resultPath),
    arg: resultPath
  )
}

// Output JSON
let jsonData = try JSONEncoder().encode(["items": sfItems])
print(String(data: jsonData, encoding: .utf8)!)

 

All you need to do is change the text between quotes at the query line near the top. Currently it’s set for PDFs; PNGs would be public.png. Other file types are left as an exercise to the reader, but here’s a list.




¹ Technically you can, by using something like a Hotkey, but I don’t think that’s what you mean. You could also fudge it with an External Trigger, which I do not recommend at all.


² Because how would Alfred know if you were refining the input in the new view or changing to another one.

Link to comment
22 hours ago, vitor said:

All you need is a Run Script Action before the Grid View set to /usr/bin/swift as the Language with this code (quick adaptation from another workflow):

Hi, vitor. Would it be difficult to sort the results by modified date? Also, would it be possible to produce the same results with a Keyword with space (without the return key) instead of hotkey?

Link to comment
On 4/17/2024 at 3:28 PM, vitor said:

you’d be typing something in Alfred and then the whole interface would change from under you to something different,

 

I'm not sure I completely follow. Isn't the Alfred results bar doing the same thing? The results change right, same can happen with grid view? As a user, all I'm looking for is ability to search files (same as Alfred's default file search) with their thumbnail visible.

Link to comment

@vitor I like your example. If I may suggest a compact mdfind alternative:

#!/usr/bin/env zsh

# Perform mdfind search and populate jsonObjects array
while IFS= read -r resultPath; do
    jsonObject='{
    "uid":"'$resultPath'",
    "icon":{"path":"'$resultPath'"},
    "arg":"'$resultPath'",
    "subtitle":"~'${resultPath/#$HOME/}'",
    "title":"'$(basename "$resultPath")'",
    "type":"file"
    }'
    jsonObjects+=("$jsonObject")
done < <(mdfind "kMDItemContentType == 'com.adobe.pdf'" -onlyin ~) # Search PDF files only in home
#done < <(mdfind "*" -onlyin ~ 2>/dev/null) # search all in home

# Convert jsonObjects array to JSON
JSON='{"items":['$(IFS=, ; echo "${jsonObjects[*]}")']}'

# Output JSON
echo "$JSON"

 

@andy4222 & @vitor To achieve this, would one need to first populate the 'Grid View' with all files and then filter the results using a 'Search Field' of the 'Grid View'? 

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