Show Open Recent Files List

Macro: “[ALL] Show Open Recent Files List”

This was requested in another thread:

[ALL] Show Open Recent Files List.kmmacros (1.7 KB)

Hey JM,

If I correctly understood the OP, she wanted a more generalized recent items list.

[Edit: macro redacted – I already posted it in the original thread.]

Trickster is a focused utility that watches file-open-events, stores them, and allows access to them via its UI.

We've tried to get the System's Recent Items via AppleScript, ASObjC and the shell – but Apple changed how they are stored – and as far as I know no one has figured out how to do it yet on post-10.9 (or so) systems.

Even if we manage to figure this out it won't allow for as much sophistication as Trickster.

Peter could probably create an event-tap to watch all file-open events, but creating a useful UI to manage them in Keyboard Maestro would be a pretty good chore.

-Chris

It’s possible to add a ‘secret’ icon to the Dock which does this. I believe you need something like Onyx to do it. I imagine that, once it’s working, there’s a way to access it via KM.

You are right that there is a way to get Recent Items in the dock.

1 Like

Great snag, Jimmy. :+1: Thanks for sharing.

Yep, you're right. But looks like Jimmy has posted a solution for the Dock.

IAC, I still find my macro useful on a per app basis. For example, many times when I open Script Editor, it's to work on a recent script.

Agreed. I've been using similar macros for years. :smile:

-Chris

1 Like

Indeed, thank you for sharing!

By any chance, have you been able to incorporate something along these lines into a keyboard maestro created window or palette, which can be called to show this in a window of choice from which to select?

I have not used this in a macro. I use Default Folder X for all recent files and folders.

Got it.

I think a combination of mdfind (spotlight command line interface) to find files modified in last 24 hours, and sort and limiting it to 10 or so files, and then transferring that information to applescript and have applescript show the dialog is perhaps one way to do it. But I am just not adept enough in these to be able to put it together.

I use launchbar for general navigation on the Mac. And being able to refer recently edited files (not necessarily for opening the files, could be for moving it to a different folder) through keyboard shortcuts is what I was trying to achieve.

Thanks again.

Hey @sims,

That’s not a bad idea. Here's a very quick and dirty proof-of-concept.

Remember to activate the macro after importing it.

Run it, and you'll get a pick-list that looks empty.

Type something like “.” to give it something to search for.

Then narrow down the search and pick 1 item, and hit <return> to open that item in its default application.

This macro is unsophisticated and not well tested, but it should supply the building blocks for something better.

-Chris


Pick-List of Items Modified in the Last 24 Hours.kmmacros (6.8 KB)

Wow Chris! Thank you!

This is so cool. I have been wanting to do something like this ever since High Sierra was released. Because for some reason, the "Recents" in Finder, omits many files.

Towards improving the macro:

  1. I think there is some error in mdfind the way it is coded above. I could get it to work with something simple like "mdfind -onlyin ~/Documents date:today". I need to get to get mdfind to work such that it picks most recent 10-15 files that have either been added or modified anywhere in ~/Documents, ~/Dropbox, ~/Desktop or ~/Downloads folder and to exclude folders and files beginning with .
  2. Also, wondering if it is possible to modify "Prompt with list" action to actually show the files as opposed to just offer the search box.
  3. And finally, is it possible to change the selection action such that if cmd- then open the file in Finder, if only then open with default application.
  4. And... finally - need to understand the regex that you have used. These regex are so cryptic!

Thank you again!

Also, re regex, I tried these at regex101.com,

  1. I noticed that the first one (?m)^.+/ only works if I choose python flavor, and not if I choose the default pcre. With pcre message is that / should be escaped with . Does KM use Python flavor regex?
  2. And then I tried to decipher what is happening in the last three actions before Open.
    • So prompt with List is obvious. Or may be not - because it does not seem to show the list. I read the Wiki for the prompt - seems to say that if less than 100 items it shows them all but does not seem to show here.
    • The next regex seems to replace . in file names with $1. Did not understand what this does. And it does seem that what ever this regex step does, is happening in real time with in the prompt with list action or is this regex action working on the output of "prompt with list" action?
    • and it appears that the picked item is searched out from within the FullList. So I think I get the last regex action, but cannot replicate it because I don't know how the previous two steps happen.

Sorry about the verbosity. Trying to understand and to explain my current understanding or lack of it.

I made some progress on mdfind.

mdfind -onlyin ~ '(kMDItemFSContentChangeDate > $time.now(-24h))' | grep -vwE "Library|Settings" | head

Linux commands are so powerful. Need to become better with them.

I don't think so. It should work as is out-of-the-box – remember though – you have to type a search string.

(By removing the prompt Title and not providing a search-string the prompt will show a list of less than 100 items. – I'll either change the first macro or upload a new one that incorporates this change.)

Something like this should do what you want:

mdfind -onlyin ~/Desktop -onlyin ~/Documents -onlyin ~/Downloads -onlyin ~/Dropbox 'kMDItemContentModificationDate >= "$time.now(-24h)"'

Invisible items with names prefixed with a dot should automatically be omitted by Spotlight.

Well, this action is a little quirky.

You can remove the title, and if the paragraphs in the search text are less than 100 they'll be shown automatically.

You can also give the acton an initial search string like “.” (dot). I don't particularly like this, because it's a purely literal search – and items without a dot in the name will be excluded.

Peter – Can the Prompt with List action be given a special variable to capture the last search? That would make it easy to bring it back where you last started which is often very useful.

@sims -- See the gear-menu in the Prompt with List action for the available options.

NOTE – If you install Dan Thomas' Spotlight Search Prompt macro, you have a great deal more control over the pop-up dialog than you do with KM's native Prompt with List action.

Spotlight Search Prompt Macro

Yes – something of that sort could be coded into the macro.

If key down then
...
else
...

Peter – why does the Prompt with List action BALK when you hit Return – but is fine with and any of , , .

Can this be changed? Or is there a good reason for the current behavior?

-Chris

Thank you again Chris.

I stumbled upon Dan Thomas's macro you have referred to. Looked great. But I will admit - I could not figure how I would put it to use :). I will look at it again to see how it can be leveraged here.

I am going to try the If key down then approach - but it sounds like you are suggesting that I try to use AppleScript. I will try to take a crack at it - I am certain I will bungle and not be able to solve it. Applescript appears to be such voodoo...

And, re the sorting of the mdfind output - I searched a good deal on the internet, and the best solution appears to be this Stackechange answer, but I am not sure how to incorporate it into the initial shell script command. I am going to try however.

No, you want PCRE – although it is not what Keyboard Maestro uses – it's acceptably close.

In the Keyboard Maestro Editor see:

Menu Bar > Help > ICU Regular Expression Reference.

See this: Remove Parent Directory from a Path String

You don't know your way around RegEx or regex101 yet, so you're hitting some bumps.

Note that I've change the delimiter regex101 uses for the find-string from “/” to “%”, because my pattern itself contains a “/”. This saves me from having to escape it in the pattern and isn't relevant in a Keyboard Maestro search/replace action.

I've also made sure the global switch is ON, but the multi-line switch is OFF, because (?m) turns multi-line ON in the pattern itself.

I've answered this in my previous post – let me know if I wasn't clear enough.

Nyet...

FInd:

([.?+()\{\}\[\]])

Replace

\$1

With this regular expression I'm escaping any RegEx special characters in the find-string that might cause problems when I go to find the full path from the file name returned from the Prompt with List action.

The outer parentheses are a capture. Capture 1 referred to as: $1

The inner brackets refer to a range of characters – in this case characters special to RegEx.

The replace pattern puts back any character in the range it finds with a prefixing backslash (an escape character).

The output of the Prompt with List action goes to a variable. I'm acting upon that variable.

Right – I’m converting back from file-name to file-path, so I can get to the actual item on-disk.

Questions are the forerunners of learning – don't apologize for asking.

I want people to spend at least a little time trying to answer their own questions before taking them public, but it's foolish to waste too much time and effort banging your head against the wall before asking for help.

I'll usually ask for help me'self if I'm still stumped after 30-60 minutes of research.

-Chris

No – I'm suggesting using a If Then Else action in Keyboard Maestro.

I was just using pseudocode to indicate the action.

That sort (pun intended) of thing gets tricky.  :sunglasses:

mdfind -onlyin ~/Desktop -onlyin ~/Documents -onlyin ~/Downloads -onlyin ~/Dropbox 'kMDItemContentModificationDate >= "$time.now(-24h)"' \
| awk -F'/' '{ print $NF"{ˆ}"$0 }' \
| sort --ignore-case \
| awk -F'{ˆ}' '{ print $NF }' \
| head -n 10

I cobbled together something along these lines by scraping using Google and some manual lookups. However, this expression of mine does not work because I think after the last line xargs is still evaluating...

mdfind -onlyin ~ '(kMDItemFSContentChangeDate > $time.now(-24h))' | grep -vwE "Library|Settings" | head -n 1 | xargs -0 stat -f '%m %N' | sort -n | cut -d ' ' -f2


The expression that you provided may be tricky but works :smile:
Update: Is your expression sorting in the order where most recent is listed at the top? I will need to dig around the syntax, but I think it is not doing that...

I will try the if then. For now, I changed the last action to Reveal file rather than Open.

I cobbled together this shell command which seems to sort the recent files in the reverse order of any change to the file.

mdfind -onlyin ~ '(kMDItemFSContentChangeDate > $time.now(-2h))' \
| tr '\n' '\0' \
| xargs -0 -n1 stat -f '%m %N' \
| sort -rn \
| cut -d ' ' -f 2-

However, the "prompt with list" action does not retain the order of the files in the variable that is used to "prompt with list".

Any way, to address this issue and retain the order?

Update: After some playing with the "prompt with list" action, it appears that the action alphabetically sorts the list it displays. And there does not seem to be a way to disable the inbuilt prompt.

Thank you.