MACRO: Choose File From Folder List With Incremental-Search and AutoComplete

Use Case

  • Provide an easy way to search/filter the list of files in a folder
    • Allows user to use incremental search with autocomplete to search/filter a list of files in a selected folder
  • User will be Prompted to Select the Target Folder

Example Output

Revised to exclude invisible files, and to sort files alphabetically


Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MACRO:   Choose File From All Files in Target Folder using KM Prompt With List [Example]

UPDATED: 2021-02-18 18:33 GMT-6

  • Revised to exclude invisible files, and to sort files alphabetically

-~~~ VER: 2.2    2021-02-18 ~~~
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Choose File From All Files in Target Folder using KM Prompt With List [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Provide an easy way to search/filter the list of files in a folder
  • File List may come from current Finder or PathFinder Window,
    OR, from a preset Folder you have set in the Macro.

HOW TO USE

  1. First, make sure you have followed instructions in the Macro Setup below.
  2. Trigger this macro.
    • IF you have NOT provided the location of the Target Folder, then you will be prompted to select one.
    • A list of ALL files in the Target Folder will be displayed
    • You can easily filter this list just by typing a few characters in the file name.
    • To choose the selected file, press RETURN or Double-Click it.
    • You can change the Target Folder by choosing the command "[CF -- Change Folder ]".
    • The list will be refreshed with files from that folder.
    • Press ESC to cancal the Macro.

MACRO SETUP

  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. ??
      .
      Make These Changes to this Macro
  1. Assign a Trigger to this macro.
  2. Move this macro to a Macro Group that is only Active when you need this Macro.
  3. ENABLE this Macro, and the Macro Group it is in.
    .
  • REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:
    (all shown in the magenta color)
    • CHANGE to PATH of Target Folder;
      • Must be either full POSIX Path of the folder,
    • ADD Your Actions Here to Process Local__FilePath
      • Add your Actions at the END of the Macro.

REQUIRES:

  1. KM 9.0+ (may work in KM 8.2+ in some cases)
  2. macOS 10.11.6 (El Capitan)+

TAGS: @Files @Finder @PathFinder @PF @PromptList

USER SETTINGS:

  • Any Action in magenta color is designed to be changed by end-user

ACTION COLOR CODES

  • To facilitate the reading, customizing, and maintenance of this macro,
    key Actions are colored as follows:
  • GREEN -- Key Comments designed to highlight main sections of macro
  • MAGENTA -- Actions designed to be customized by user
  • YELLOW -- Primary Actions (usually the main purpose of the macro)
  • ORANGE -- Actions that permanently destroy Variables or Clipboards,
    OR IF/THEN and PAUSE Actions

USE AT YOUR OWN RISK

  • While I have given this a modest amount of testing, and to the best of my knowledge will do no harm, I cannot guarantee it.
  • If you have any doubts or questions:
    • Ask first
    • Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

3 Likes

Thanks very much !

  • I changed the Prompt action config (gear) to allow for multiple selections (which is what I want most of the time) but it does not work. One selection is fine. More than one and nothing happens when I press Enter. I know, based on some of your previous macros with prompts, that it should work.

  • to end up opening the selected file in default app, I added pause → keytroke Enter at the end. Is there a more elegant way of doing this ? I am NOT asking you to rewrite the macro yet again ! Just curious

thanks !

Multiple selection works just fine.
It returns a list of file paths, one per line.

But to use this list, you will need a For Each action with a Lines Collection to open each file separately.

Should not be needed. Just use this:

image

1 Like

Works fine now with you suggestion. SPECTACULAR !!
thank you so much !

I often use your macro in all kinds of situations, looking for files and calling up templates like the HoudahSpot Search Templates.
The only inconvenience is the 4.5 second delay (measured with a stopwatch) between the time the macro is triggered and the search box appears.
Is there anything that could be done to decrease the delay ?
thanks again !

Hey @ronald,

You must be doing something time consuming on the front end of your macro, because the Prompt With List action is almost instant – unless you choke it with many thousands of items.

Can you post an example of a macro that is so slow?

-Chris

1 Like

Thanks very much Chris for your interest.

I am sure that I am the problem, not @JMichaelTX

There are only 88 files in my Scrivener folder which is searched by this macro.

I retested a number of times: about 4.5 seconds

I have a very good mac : the latest i7 Mini, 32 GB RAM, 1TB SSD Drive

Here are a snapshot and the macro itself

Scrivener @FileSearch ver 2.0 COPY FOR FORUM POST.kmmacros (58.1 KB)

Hey @ronald,

I tested JM's macro, and it takes somewhere around 3 seconds to list 100 files on my old Mid-2010 17" 2.66 GHz Intel Core i7 MacBook Pro with only 8GB of memory.

Odd that it's taking longer on your newer hardware...

Even so – it's a pretty long pause to tolerate, unless you need to.

  • Load up this test macro.
  • Replace the path in the orange action with the 4 second folder on your system.
  • Run the macro and time it.
  • Let me know how fast it is.

From there we'll decide what to do.

-Chris


TEST ⇢ Prompt with List for a List of Files ⇢ for @ronald v1.00.kmmacros (7.7 KB)

1 Like

First and foremost, thanks very much for looking into this issue.
I downloaded your macro → changed the path as per your instructions → ran the macro → search window is immediately displayed. No perceptible delay which is fantastic.
Could you explain what you did ? Is it the shell script ?
thanks a million

please tell me that you also have a newer mac !

Hey @ronald,

JM is going beyond the default options for the Prompt With List action, and I think that's adding a little time to the macro.

He's also AppleScripting the Finder, and that's addling a little more time.

I haven't examined everything carefully for all possible bottlenecks...

My macro is using find in the shell and sed to maximize the speed of getting the file paths.

It's not quite as fancy and JM's, but the speed is worth it (to me).

I have the old 2010 MacBook Pro and a 2012 MacBook Air.

I desperately need some new hardware, but with the M1 Macs out now it's pointless to spend money – when I can get by for a while.

It does chafe though...

I'm thinking the 5K iMac M1 version will be where I want to go, but I'm not sure yet. It depends upon what Apple surprises us with and what their release schedule is.

I'm tempted to buy a M1 Mac Mini, but I'd have to have extra RAM, HD, and an external monitor – so the expense it too high right now.

-Chris

I recently bought my mini. At the time, I looked at the M1 and was very disappointed. All kinds of limitations in RAM, external video support etc etc etc
I am very happy with the 2018 Mini.
Although you know much better, I like the fact that you can add RAM, external video card, external monitor etc which I can do gradually as opportunities arise.
You have to get a desktop because laptops are bad for your posture and consequently your back.
So sorry you have to live with 2010 and 2012 models.

I have the laptops up on stands.

For the monster 17" MacBook Pro I have a monitor platform and a stand:

3M Extra Wide Adjustable Monitor Stand

Rain Design mStand

I haven't bought a stand for the MacBook Air yet (a relatively new acquisition), but I'm probably going to get this one:

Laptop Stand Adjustable, Lamicall Notebook Holder

-Chris

My original macro and script was more focused on flexibility than on speed.
The updated ver 2.1 now uses AppleScript System Events to build the list files, resulting on a list of, for example, 211 files in only 0.23 sec:

Sorry for the slow version. It is now much faster. See above.
It now takes only 0.23 sec AFTER you select the target folder.

Chris, the script is much simplified now, uses System Events, and should be fast even on your old MBP. It runs in 0.23 sec on my 2019 iMac. Please let me know if it is still slow for you.

@ronald and @ccstone, there is a disabled Action that will display the elapsed time if you wish to time it:

image

1 Like

Sorry, I had not tested the macro correctly. The search window and results are fine, but if I click on a file, nothing happens. I tried in a few different directories

with the new script, the delay is down to 0.3 sec on my mini, which is perfect. thanks very much.
In addition to searching for files, I use it to search houdahspot search templates which is great.
trying to figure out if I can search DevonThink smartgroups which require a lot of clicking.

1 Like

Hey JM,

Quite acceptably fast with 500 files.

But – you haven't eliminated invisible files, so .DS_Store files are showing up (and potentially others).

Simpler:

--------------------------------------------------------
# Whose-Clause filtering is slow as file numbers increase.
--------------------------------------------------------

# set targetFolderPath to "~/test_directory/Many_Files_100/"
set targetFolderPath to "~/test_directory/Many_Files_500/"

tell application "System Events"
   set filePathList to POSIX path of files of disk item targetFolderPath whose visible is true
end tell

# 100 files -->  0.44 seconds
# 500 files --> 10.75 seconds

Much Faster:

--------------------------------------------------------
# Brute-force text list filtering is much faster.
--------------------------------------------------------

# set targetFolderPath to "~/test_directory/Many_Files_100/"
set targetFolderPath to "~/test_directory/Many_Files_500/"

tell application "System Events"
   set filePathList to POSIX path of files of disk item targetFolderPath
end tell

set AppleScript's text item delimiters to "/"

repeat with i in filePathList
   if i contains "/." then
      set contents of i to 0
   else
      set iContents to contents of i
      set contents of i to iContents & "__" & (last text item of iContents)
   end if
end repeat

set AppleScript's text item delimiters to linefeed
set filePathList to text of filePathList as text

# 100 files --> 0.036
# 500 files --> 0.075

--------------------------------------------------------

Hey @ronald,

Of course not – quite obviously if you look at my macro.

I had no intention of reworking JM's entire macro for you – I just gave you a very fast tool to do this yourself.

-Chris

Really Chris?

I would have thought that on an old Bash hand like you would have known the easy solution to this, unlikely, issue:

So if you're looking for a file with some text in its name, and you want to skip hidden files and directories, run:

find . -not -path '*/\.*' -type f -name '*some text*'

REF: https://askubuntu.com/a/318211

@ronald et al:

To avoid, exclude invisible file, add this to the Bash find command:
-not -path '*/\.*'

IAC, since the user is presented with a list of files to select, I don't see this as a significant concern.

It's not an unlikely issue, and you chose to use AppleScript and System Events in your last macro – so I provided an AppleScript solution to you.

If you'd looked at my macro for Ronald you'd have seen that in fact I did use the shell's find command and did exclude invisibles.

Not only that I created the double-underscore display values for the Prompt-With-List action at the same time:

targetDir=$(cat);
cd "$targetDir";

find -E "$targetDir" -type f -maxdepth 1 \( ! -name ".*" \) \
| sed -E 's!^(.+/)([^/]+$)!\1\2__\2!'

-Chris