Alfred workflow to automate saving screenshots using ChatGPT

Use ChatGPT to write an Alfred workflow

Created: by Pradeep Gowda Updated: Nov 04, 2023 Tagged: aflred · chatgpt · automation


I often take screenshots that I plan to use in entries like this. I wrote an alfred workflow to make the repetitive parts less boring and more predictable.

To save the below screenshot which is then served via cloudflare, all I had to do was to use Cmd-3 to take a screenshot of the area I was interested in, press Cmd-space to bring up alfred, type fscreen fscreen-1 (the second argument being the filename), and hit enter. On successful save of this file screenshot into ~/, the workflow then showed a notification and copied the path to the clipboard.

fscreen alfred workflow screenshot
fscreen alfred workflow screenshot - a screenshot automated using this workflow

ChatGPT Prompt

write an alfred workflow that is triggered on the word “fscreen” and saves the image in the clipboard with the filename that is followed by the word “fscreen” command and ending with .png at a resolution of 800px width, while maintaining the aspect ratio. example: if the command is fscreen test-shot, the image in the clipboard should be saved to ~/ 2023 and 10 correspond to the year and month when the command was invoked.

ChatGPT Response

To make an Alfred Workflow to achieve this, follow these steps:

  1. Open Alfred Preferences and go to the Workflows tab.
  2. Create a new workflow: Give it a name and description.
  3. Add a Trigger: Use “Keyword” and set the keyword to “fscreen”.
  4. Add an Action: Use “Run Script” and select /bin/bash as the language.

Paste this Bash script in the “Run Script” box:

output_dir=~/$(date +%Y)/$(date +%m)

# Get clipboard image
osascript -e 'set the clipboarddata to (the clipboard as «class PNGf»)' -e 'set the clipboardfile to "/tmp/screenshot.png"' -e 'write the clipboarddata to (open for access (POSIX file clipboardfile) as «class PNGf»)'

# Create output directory if it doesn't exist
mkdir -p $output_dir

# Resize and save the image
sips -Z 800 $clipboard_file --out $output_file
  1. Connect the Trigger to the Action.

This workflow triggers on the word “fscreen”, takes an image from the clipboard, resizes it to 800px width while maintaining aspect ratio, and saves it with the specified name in the specified directory.

Tweaks to the prompt

modify the above script to save the data to notify the user with the full output file path, and copy the same to the clipboard or the error message from the script if there is an error output

Plus there was another error:

execution error: Can’t make file "Macintosh HD:private:tmp:screenshot.png" into type «class PNGf». (-1700)

Fixing both lead to the following solution that uses pngpaste:

output_dir=~/$(date +%Y)/$(date +%m)

# Create output directory if it doesn't exist
mkdir -p $output_dir

# Paste clipboard image to temporary file and handle errors
temp_file=$(mktemp /tmp/tempfile.XXXXXX.png)
if pngpaste $temp_file; then
  # Resize and save the image
  sips -Z 800 $temp_file --out $output_file

  # Notify the user and copy the path to clipboard
  osascript -e "display notification \"$output_file\" with title \"Screenshot Saved\""
  echo -n $output_file | pbcopy
  # Notify the user and copy the error message to clipboard
  errorMsg="No image data found on the clipboard."
  osascript -e "display notification \"$errorMsg\" with title \"Error\""
  echo -n $errorMsg | pbcopy

# Remove temporary file
rm -f $temp_file

TODO: Show URL in clipboard and alerts.