OSX: Automatically upload screenshot to imageBin a

2019-09-02 04:06发布

问题:

I would like to send portions of my OSX screen over IRC with the least fiddling.

Many apps do this already: CloudApp, Droplr, DropBox, Jing, etc

Problem is none of them return a link to the actual image. They return a link to a page containing the image, mainly so they can get advertising. This is no good for IRC; most IRC clients are smart enough to display an image when they see the URL for an image, but they are not smart enough to pull out the image from such a framing page.

I would like to write something that does this.

Shift + Cmd + F3 lets me select a rectangle with the mouse, and deposits the image on my desktop. This can be changed to another folder: http://osxdaily.com/2011/01/26/change-the-screenshot-save-file-location-in-mac-os-x/

I would like to have this image automatically upload onto the Internet and place a URL link in my clipboard.

I figured out half of the problem: the following bash script will upload an image and put the link into the clipboard:

#! /bin/bash

# Imagebin Loader 
# π 27.1.2014
#
# Upload image to imagebin.org, put url to image in clipboard, make noise

filename="$1"

# http://stackoverflow.com/questions/4651437/how-to-set-a-bash-variable-equal-to-the-output-from-a-command
url_out=$(
    curl  -s \
        --form "nickname=$(hostname)" \
        --form "image=@$filename;type=$(file --brief -L --mime-type "$filename")" \
        --form "disclaimer_agree=Y" \
        --form "mode=add" \
        --form "Submit=Submit" \
    http://imagebin.org/index.php \
    -w '%{redirect_url}\n'\
    )

# e.g. http://imagebin.org/288917
echo "$url_out"

# http://mywiki.wooledge.org/BashFAQ/073
number=${url_out##*/} 

printf -v new_url "http://imagebin.org/index.php?mode=image&id=%s" $number

# e.g. http://imagebin.org/index.php?mode=image&id=288917
echo "$new_url"

#http://osxdaily.com/2007/03/05/manipulating-the-clipboard-from-the-command-line/
printf "$new_url" | pbcopy

say -v Trinoids "Fooo"

Now how can I accomplish the other part of the puzzle: whenever a new screenshot arrives on the desktop, to automatically execute this script with it?

LINK: https://apple.stackexchange.com/questions/118698/free-screen-grabber-for-os-x

回答1:

Use an Automator Folder Action to launch the script when new items are added to the folder. You might want to redirect screenshots to somewhere other than the Desktop otherwise all items added to your desktop will get uploaded.



回答2:

For example save this plist as ~/Library/LaunchAgents/example.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>example</string>
  <key>Program</key>
  <string>/path/to/script</string>
  <key>WatchPaths</key>
  <array>
    <string>~/Desktop/</string>
  </array>
</dict>
</plist>

Then run launchctl load ~/Library/LaunchAgents/example.plist or log out and back in to load the plist. To apply changes to the plist later, unload and load it.

There is no way to use anything like wildcards with WatchPaths, but you can edit the script to skip files that are not screenshots:

for f; do
  [[ $f = Screenshot\ *.jpg ]] || continue
  echo "$f"
done

(You'll also need to figure out how to avoid running the script multiple times for the same images.)