Introduction
In my previous article, I explained the first steps for using Fabric to work more efficiently with LLMs. This was primarily done through the command line interface. As I promised in that article, I now want to show how Fabric can be integrated into an Apple Shortcuts workflow.
As an example, I will demonstrate how to summarize a webpage using the workflow described in the article. As a reminder, here’s the CLI command:
wget -qO - https://example.com/my-blog-article | pandoc -f html -t plain | fabric --stream --pattern summarize
My requirements for the shortcut are:
- It should work with every browser.
- It will be called from the Shortcut Menu.
- It will save the result in my Obsidian Vault as a note in a specific folder.
- The title of the webpage will be used as the title of the note.
- The URL of the webpage and the tag “fabric” should be saved in the frontmatter section of the note.
The Apple Shortcuts
Originally, I intended to implement the Apple Shortcut using the script more or less as it was. The plan was to fetch the URL of the active website with the “What’s On Screen” action, pass it as an argument to the “Run Shell Script” action, and then execute the script. However, it didn’t work as intended. While the URL is correctly identified within the Shortcuts environment, it turns into a link to a local copy of the webpage when the “Run Shell Script” action is called. This can be tested with a simple Shortcut.
Just run this shortcut when a browser is active, and you’ll see the problem. 😉
In the end, I took advantage of this strange behavior by simply piping the content of the file located at the path provided as an argument with the cat command to the fabric command.
Before starting to define the shortcut, there is another issue to solve. Since the “Run Shell Script” action doesn’t run in the normal environment of the Terminal program, it can’t access, for example, the search path definitions that are set in the .zshrc configuration file. This means it won’t find the fabric command or any of the alias definitions for easily calling fabric, as I explained in the previous article.
Shortcut definitions
Before starting to define the shortcut, there is another issue to solve. Since the “Run Shell Script” action doesn’t run in the normal environment of the Terminal program, it can’t access, for example, the search path definitions that are set in the .zshrc configuration file. This means it won’t find the fabric command or any of the alias definitions for easily calling fabric, as I explained in the previous article.
Here is the shell configuration that will be used in the script:
# Add the Go binary directory and the path to homebrew commands to the existing PATH environment variable export
export PATH="$PATH:$HOME/go/bin:/opt/homebrew/bin/"
# Check if the fabric-bootstrap.inc file exists in the specified directory
# If the file exists, source it to load its configurations and variables
if [ -f "$HOME/.config/fabric/fabric-bootstrap.inc" ];
then . "$HOME/.config/fabric/fabric-bootstrap.inc";
fi
This configuration adds the path information for the Fabric as well as for Homebrew-installed commands (in this case, pandoc). The second part loads the alias definitions, allowing you to use just summarize
instead of fabric -sp summarize
.
Now, let’s take a look at the script:
source $HOME/.fabenv
cat $1 | pandoc -f html -t plain | -sp summarize
First, it sources the shell configuration. Then, it calls the command cat $1. Since the variable $1 contains the local path to the webpage, it pipes the entire HTML, which includes a lot of extra rendering information, through the pandoc command to clean it up before piping it through the fabric/summarize call.
With the script defined, let’s look at the entire Shortcut:
And here’s how the process works:
- The URL and title of the webpage are passed to the shortcut and stored in the corresponding variables.
- The title will be cleared of any special characters that might cause issues in Obsidian or Finder.
- The “Run Shell Script” action receives the path to the local HTML file through the URL variable.
- The script will be executed as described above.
- The output of the shell script is stored in the
Text
variable. The note is then assembled in a text block, including the frontmatter section and footer. - This text is then saved in the Obsidian Vault. To save the note in the Obsidian Vault, I use the shortcut extension Actions for Obsidian with the “Create Note” action. Alternatively, you can save the note with the “Save File” action, but when working with Obsidian, it’s worth investing in Actions for Obsidian.
Some webpages are resistant, and the script can’t fetch them. In such cases, at least when using OpenAI, the shortcut will save a note with some placeholder text.
Summary and Outlook on Future Applications
I hope that with this article, I not only introduced a useful tool but also provided a starting point for creating your own solutions on how to integrate Fabric into Apple Shortcuts.
The shortcut is certainly not yet fully refined. For example, error handling is missing. I can also imagine that a selection dialog could be used to choose different patterns. Additionally, the workflows currently run locally on my MacBook Pro, so I haven’t tested whether the “Remote Shell Script” action can execute the Fabric command on a server. As long as no local LLMs are involved, a Raspberry Pi should suffice. This way, a shortcut could also work on an iPad or iPhone. I’m currently working on that, so stay tuned.
Feel free to leave criticism, corrections, and praise in the comments.
Leave a Reply