Automatically open a file with periodically changing filename

Hello.
I'm a teacher and I'm trying to open a file automatically when I enter the school's WIFI. It works great with normal files but I have one specific file that changes it's filename according to the actual school week.
I'm sitting here since 2 hours now but I can't figure out how to set variables and stuff to open the file with the name weeklyplan SW 07.numbers; It just doesn't work.

Google and the search isn't a great help because all I can find is how to change filenames. Which I already know about. Help me please.

This prior thread has a solution whereby you create an alias to the file and point Keyboard Maestro at the alias.

1 Like

You could do it without an alias if the naming is consistent and school weeks are linear—I fear they are not, given breaks for Thanksgiving, Christmas, etc. But if they were, and assuming this is week seven as shown in your example, this macro should work.

Download Macro(s): Open date-coded file.kmmacros (5.4 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 14.7
  • Keyboard Maestro v11.0.3

You'd need to modify the two green boxes. The first one is the Monday of the first week of school, and is the starting point for counting weeks.

The second green box is the full path to your weekly planner file. You only need to change the path part; the filename bit is calculated based on the values of the dates.

This works for me in testing, but as noted above, probably fails in real life due to gaps in the calendar. Nonetheless, I like the all-in-one approach without relying on an alias file. It can probably be modified to handle calendar gaps, but I don't have that knowledge of your school's calendar.

-rob.

1 Like

The "Open a file" action needs an actual file path -- as you've found, you can't do "open a file that's named roughly like this".

A few possible approaches:

  1. Work out what the school week is, build your file path with that included
  2. List the containing folder, get the name of the file that starts "weeklyplan", build your file path with that -- does assume there is only one "weekly plan" in the folder
  3. List the containing folder by modification date, get the name of the most recently modified file that starts "weeklyplan", build your file path with that -- does assume that the one you want will be the most recently modified
  4. List the containing folder in reverse alphabetical order, and open the first "weeklyplan" file -- does assume they don't put in the next year's 01 while last year's files are still there!

I'm sure there are other ways. Find how you can determine the correct file, and go from there.

For the "list" methods, take a look at the "For Each" action, using the "Folder Contents Collection" option. And remember that you can "Break" from the "For Each" once you've found the match.

1 Like

I tried the Applescript way but it somehow doesn't work. Either I get no error message at all, or it just says there is no such file.

set folderPath to "/Users/XXXXX/Library/Mobile Documents/com~apple~CloudDocs/DaZ/"
set targetFile to do shell script "ls " & quoted form of folderPath & " | grep -i '^Wochenplan SW' | head -n 1"
if targetFile is not "" then

    set filePath to folderPath & targetFile
  
    tell application "Finder"
        open POSIX file filePath
    end tell
else
    display dialog "No file found." buttons {"OK"} default button "OK"
end if

I also tried it with "For Each" but somewhere it goes wrong and I can't find the mistake.

What you're showing is very different than what is shown in the other thread for using Keyboard Maestro and AppleScript to open an alias.

image

Yes. I tested this script in Scripteditor it opens the file, but when the name changes from "filename SW 1" to "filename SW 2" it doesn't work anymore.Then I asked ChatGPT for help because it usually knows its way around Applescript, and it told me that I should use this. Both don't work.
( Oh and Keyboard Maestro seems to not execute any Applescripts, not even if I feed it script files.)
Edit: I figured out that tell application Finder won't work. I changed it to Numbers and it opens the file.
I don't know why ScriptEditor will open it, but KM not.
I still have the problem that it won't open the file next week with another week number.

Question to ChatGTP

Please write a bash script to find the most recent modified file in a directory passed as an argument and open it in macOs.

#!/bin/bash

# Check if a directory argument is provided
if [ -z "$1" ]; then
  echo "Please provide a directory path."
  exit 1
fi

# Check if the provided argument is a valid directory
if [ ! -d "$1" ]; then
  echo "The provided argument is not a valid directory."
  exit 1
fi

# Find the most recently modified file in the directory
most_recent_file=$(find "$1" -type f -print0 | xargs -0 ls -t | head -n 1)

# Check if a file was found
if [ -z "$most_recent_file" ]; then
  echo "No files found in the directory."
  exit 1
fi

# Open the most recently modified file
open "$most_recent_file"

I tested it here and it works. Save it as a script - say most-recent.sh.
Give it execute permissions chmod 775 most-recent.sh.
Example call on my device

./most-recent.sh "/var/go/src/csmu.balance-infosystems.com/go-nullable/nullable/"

Use the Execute Shell Script action. You'll need the absolute path to the script if ~ (your home directory).

You may have to give permissions to interact with the shell. It's likely it will work for you.

Thank you kindly for your answer and the script. It doesn't help me that much because the file isn't always the recent file in the folder. It's a folder with my list of kids, daily plan, weekly plan,... it's a work folder. In it I have all the stuff I use daily.
Every Sunday Hazel will automatically archive the stuff I don't need next week into their Johnny Decimal folders.
But you gave me the idea that a bash script might work. I will look into that. Thanks!

Looking at what you need to do, I think a Keyboard Maestro For Each Action should be able to do the job -

Will the filename always contain "Tagesplan" with some additional text?

1 Like

Did you try my macro? It should do exactly what you need it to do, without AppleScript or an alias. And it could probably be modified to handle interruptions in the calendar count, if such things exist.

-rob.

2 Likes

There's absolutely no need for a bash script (though it could be fun to use one :wink: ).

KM's "For Each" action using "Folder Contents Collection" gives you a list of everything in the directory that you can work through. Use the options to sort the list by date, if necessary. Then work down the list to get (for example) the first occurrence of /weeklyplan SW \d+.numbers and "Open" that path.

All you need to do is define, unambiguously, which file in that folder you want to open.

Since writing almost any macro starts with "how do I do this manually?" -- if you open that file using the Finder, how do you decide which item in the window to double-click? Once you define that we can translate that to KM terms.

2 Likes

It looks always like this:
Tagesplan SW 1
Tagesplan SW 2
Tagesplan SW 3

I mean I could name it as I want but I want it to reflect the actual school week which is not like calendar week or something.
I messed around with "For Each" but got nowhere with it. I also asked ChatGPT and with another post about this topic I got to setting a Varible as "Filepath".

Then I added For Each, but I'm lost at this point because ChatGPT tells me stuff I can't find in the dropdown. It says I should choose "for each file" but I cant find this.

Not yet because I've set up Hazel to do the naming and sorting of the files for years now. I don't want to mess around with file naming because then I have to go through all the Hazel rules and then I'd rather give the file a name without the week.
I made the experience that adding the school week is not ideal because as you said, there are gaps, it changes every year, so all the rules about it have to be changes as well.
If I can't get the script to work or the For Each, I would try to look if I change Hazel and use your script instead. Thank you nontheless!

Everyday in the morning, when I open up the computer at school, I open Finder, choose my work folder, look for the "Dailyplan" file and doubleclick it. Automating all of this would be easy if this file wouldn't change the name automatically every week with Hazel. :smiley:

I'm a bit confused: It shouldn't matter how the file is named, whether that's via Hazel or something else. The macro just finds the filename that you define, with the week number changing as needed.

Are there multiple "daily plan" files or just one? If there's just one, you can do this really simply with a shell command in a macro. But I won't post an answer until you answer that question—it can be handled if there's more than one such file, but it'll be a bit different.

-rob.

1 Like

This can't be complete, otherwise all you'd do is tell KM to open "Dailyplan" every morning! At the risk of repeating myself:

So is there only every one "Tagesplan SW nn" file, so you just look for "the file whose name starts with 'Tagesplan'"? Are there multiple, but you "know" the current school week so you can pick the right one -- if so, how do you "know" the current school week? Are there multiple, but the file with the biggest "nn" is always the one for the current week? Or is it the most recently modified file whose name starts with "Tagesplan...".

You know how you do this "in real life". KM doesn't, we certainly don't, which is why you need to explain it as completely and precisely as possible.

Since it sounds like the file is being renamed by a Hazel rule -- showing us the Hazel rule would show us how your doing that renaming and would give us a clue how to use the same to let KM find the file. Unless, of course, you're renaming it based on the file's contents...

1 Like

@Johnkree Do you see how you have Keyboard Maestro experts in this thread competing to solve your problem for you?

All you need to do to enable that is to answer this question:
How can we programmatically identify the file you want to open?

Is it:

  • The most recently added file? [No, you've said it isn't]
  • The file with Tagesplan in the filename? [No, you've said there are several such files]
  • The most recently added file with Tagesplan in the filename?
  • The file with a filename that looks like Tagesplan SW [a number] with the highest value for [a number]?

If you post some rule like that, we can create a macro that identifies the file according to that rule. But only you can determine the rule, because only you know how your file system works.

2 Likes

Yes, list the rules and someone here will give you a solution.

You have a folder you want to monitor - always the same folder y/n?
There will be a single file in that folder that has "Tagesplan" in its name that you want to open?
Might there be other files with "Tagesplan" in their name in the folder?

Just list the criteria in simple steps or pseudo code. Don't worry about detailing the unsuccessful attempts that you have tried already.

1 Like