Web Links and Archives for Mac File Managers

My previous post on some useful file renaming in Finder, Forklift, and Path Finder gave me a mental nudge to write up and share with you all some automations for getting some content into files from Safari. More specifically ways to generate links and snapshots of web pages and store them away in the current folder. This time however, rather than using Shortcuts, the tool of choice is Keyboard Maestro.

The Need

While I do use Hookmark and add hyperlinks to documentation directly, I do also need to capture links into my file system. I sometimes want to flag where I downloaded a particular file from, and sometimes I want to actually grab a snapshot of a page to keep stored away (typically, but not always in DEVONthink). These both can become more important when I’m sharing folder structures and files with people.

Therefore when working with files in a particular location in the file system it can be useful to automatically generate some reference files without shifting focus from the folder.

The Output Options

For snapshots of files, you can always output as a PDF, but for Safari, there is also the .webarchive file format that does a pretty good job of keeping everything intact and generally works better than PDF on pages which have beautiful and intricate design (over engineered iframes, etc.)

For links, there is a simple text-based file format that’s used across operating systems. The .url format. It is a very simple initialisation (.ini) file structure with a section and key value pairs. Importantly, a section heading for InternetShortcut, and a URL.

e.g.

[InternetShortcut]
URL=https://thoughtasylum.com

Saved as a .url file, when opened from your file manager, this would cause the default browser to navigate to this website.

Together these two formats give us options for the live link and the archived snapshot of the page content.

Keyboard Maestro vs Shortcuts

In the last post I used Apple Shortcuts to manipulate my files rather than Keyboard Maestro. So why switch?

Well, Keyboard Maestro just had a few more convenience options for me to use than Shortcuts. Primarily Keyboard Maestro made it easier to interact with Safari, but also I can track macro progress with Keyboard Maestro, and apply a more sophisticated trigger mechanism. While I could probably have built this in Shortcuts, the features in Keyboard Maestro presented it as the better candidate this time around.

Triggering Macros

For triggering Keyboard Maestro I wanted to be able to do it from any of my file managers, but there is a need to be able to distinguish between them as getting ‘the current folder’ will vary based on the file manager being used. I also wanted to be able to choose easily between creating a URL/link file and creating a web archive file.

To support this I decided to do two things. First I looked to parameterise the file creation steps using a subroutine macro so I could use that across each of the file managers and from each file manager’s macros I could then pass in the current folder path for that application. Second I set up the macros specific to each file manager application, but with the same shortcut key combination. In fact to make things even easier I used the same shortcut for both types of file, but rather than invoking a conflict palette, I set a link to be generated on a single press and a web archive to be created on a double press.

Naming Output Files Safely

For naming the output files, the most sensible ways of doing this seemed to either prompt for a name, or use the page title from the web page. The latter was my preferred option, but it came with an extra challenge. not all characters that appear in a web page title are valid characters in a file name. To that end I created a short shell script to tidy up a page title and strip out any problematic characters.

#!/bin/zsh
echo $(echo $(cat) | sed 's/[\/\\:*?"<>|]/_/g' | sed 's/^[.]*//; s/[.]*$//; s/[[:space:]]\+/ /g')

Let’s break this down a little to work through what is going on.

The outer echo is outputting the result of the pipeline in the parentheses, which consists of an echo piping into a sed instruction, which is piped into another sed instruction. The echo is just passing the script input into the pipeline.

The first sed instruction replaces occurrences of “/”, “", “:”, “*”, “?”, “"”, “<”, “>”, and “ ” with an underscore character.

The second instruction consists of three parts. The first removes any leading periods from the output of the previous instruction. The second part does similarly, but at the end of the title. The third part collapses every sequence of whitespace characters into a single space.

The result should be a base name suitable for use in a filename, derived from the input to the script.

Generating a URL File

The subroutine macro for creating a URL file accepts the file path from a file manager macro and then using the filename sanitising script above and a Keyboard Maestro variable to grab the Safari page title can write out the .url file relatively easily.

Generating a Web Archive File

Creating a web archive file is, as you might expect, a little more involved. Keyboard Maestro does not have an easy option for saving a web archive file, Safari doesn’t actually make it all that accessible either. While there are some command line utilities out there, I did a little poking around in Swift and discovered that someone (Alex Chan) had written a compilable command line application in Swift to generate web archive files. Keyboard Maestro has the capability to run Swift scripts as an action step, which makes it fractionally slower to run, but easier to maintain and distribute with a macro.

Details are included in the macro, but if you want to view the script standalone, you can check out Alex’s GitHub repo for the Safari web archiver. Note that the script in the macro has been modified to pick up some Keyboard Maestro variables to use as input.

Overall, the macro is actually very similar to the URL file generating one, swapping out the web archive file generation for the writing direct to file. The other notable difference is I added a little progress info bar because generating a web archive file can take a little while, particularly on more complex pages, and this gives you a visual indication it is running.

Passing in Current Folder Paths

With the subroutines in place, all that was required was to create two macros for each of the three file managers that would call each of the two subroutines. As noted earlier, in each case the pair of macros was added to a macro group that is only enabled for the particular app, and the trigger shortcut is consistent across apps.

Finder

Finder is the easiest one to set up as Keyboard Maestro includes a variable, much like the Safari page title one, and we can just call the subroutines using this variable to specify the current folder path.

Path Finder

Path Finder is a little more difficult in that Keyboard Maestro isn’t natively supporting getting data from it. However, we can use a relatively simple piece of AppleScript to fetch the current path and then we can pass that into each of the subroutines. The AppleScript is simple enough that it doesn’t warrant having a subroutine for it to maintain between these two macros.

Forklift

Forklift is not as automation friendly as the others, but I did come up with a way that seems to have been reliable. This utilises Keyboard Maestro’s menu interaction features to have Forklift place the file path for the current folder on the clipboard. The macro ensures that any files and folders in the current folder are deselected (and that macro action is set to not abort the macro on failure) before triggering a path copy. The subroutine is then called using the system clipboard as input, and upon completion of the subroutine, the entry in the clipboard is removed - I always try to tidy up afterwards if I’m having to stage data via the clipboard.

Conclusion

Of course, you don’t have to rebuild these macros by hand and guess what modifications I made to the Swift script. You can get installable copies of each of the macros you would like to use by using the download links below. Just download whichever macros interest you, and as always, give them a read through before you enable them in Keyboard Maestro to ensure you are happy they are not doing anything untoward.

I don’t use these macros every day, but I do end up using them for specific projects and typically using them frequently for short periods on those projects. For example, recently I have been using them just to ensure I keep track of download links for 3D printing model files I’m downloading before I print them and start creating notes about them. It just gives me a quick and easy option when I’m working with downloads and web browsing side-by-side. Hopefully these will give you a quick and easy option too.

Author: Stephen Millard
Tags: | keyboard maestro |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read