As Markdown markup language gets more and more popular, most people have a need to quickly generate a public url for screenshot/image in clipboard. Today I created an Alfred workflow that helps you upload images/screenshot in clipboard.

Demo

Overview

Use pngpaste to get image data from clipboard

Write to a image file in /tmp folder

Use boto3 to upload image file to AWS S3

Implementation

Now we have the idea to achieve the goal, let’s do it in Alfred workflow and python script.

Create Alfred workflow

Create Blank Workflow and give it a name

Right click in background and select Inputs -> Keyword

Give it a keyword which you will be typing to trigger the workflow

Right click in background and select Actions -> Run Script

Let’s make it use Python to run a script

Click on the icon on the left of Cancel button to open workflow folder

Create a script file (Make sure the file name is the same as the Run Script action uses) and edit like below

#!/usr/bin/python# -*- coding: utf-8 -*-from__future__importprint_functionimportdatetimeimportosimportsysimportatexitimportimghdrfromsubprocessimportcallfromos.pathimportexpanduser,exists,basename,getsizefromworkflowimportWorkflowdefcapture():file_name=datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S.png')file_path=os.path.join('/tmp',file_name)atexit.register(lambdax:os.remove(x)ifos.path.exists(x)elseNone,file_path)save=call(['./pngpaste',file_path])ifsave==1:# Quit job if no image found in clipboardprint"No image found in clipboard"sys.exit()returnfile_path,file_namedefmain(wf):importboto3file_path,file_name=capture()bucket_name=os.getenv('bucket_name')s3=boto3.client('s3',aws_access_key_id=os.getenv('access_key'),aws_secret_access_key=os.getenv('secret_key'))s3.upload_file(file_path,bucket_name,file_name,ExtraArgs={'ContentType':"image/png"})output="%s/%s"%(os.getenv('bucket_uri'),file_name)print(output,end='')if__name__=='__main__':wf=Workflow(libraries=['./lib'])sys.exit(wf.run(main))

This script will return the image public url and let’s copy it to clipboard and push a notification.

Right click on the background and select Outputs –> Copy to Clipboard

Right click on the background and select Outputs –> Post Notification

Now let’s connect those boxes like below:

Include libraries

In the scripts, we used below libraries and need to be included in the workflow to make sure they can be run without issue