Alfred workflow to automate saving screenshots using ChatGPT
Use ChatGPT to write an Alfred workflow
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
, all I had to
do was to use
to take a screenshot of the area I was
interested in, press
to bring up
(the second argument being the filename),
and hit enter. On successful save of this file screenshot into
workflow then showed a notification and copied the path to the
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
10correspond to the year and month when the command was invoked.
To make an Alfred Workflow to achieve this, follow these steps:
- Open Alfred Preferences and go to the Workflows tab.
- Create a new workflow : Give it a name and description.
- Add a Trigger : Use “Keyword” and set the keyword to “fscreen”.
Add an Action
: Use “Run Script” and select
/bin/bashas the language.
Paste this Bash script in the “Run Script” box:
clipboard_file="/tmp/screenshot.png" output_dir=~/files.btbytes.com/screenshots/$(date +%Y)/$(date +%m) output_file="$output_dir/$1.png" # 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
- 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
output_dir=~/files.btbytes.com/screenshots/$(date +%Y)/$(date +%m) output_file="$output_dir/$1.png" # 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 else # 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 fi # Remove temporary file rm -f $temp_file
TODO: Show URL in clipboard and alerts.