📜 ⬆️ ⬇️

Automator script loading images on Habrastorage

Once I got tired of poking the Habrastorage tab in the browser. And I did this ...

image


That's why I love MacOS - for Automator and AppleScript.
Launch Automator, select the type - Service.
image
In the drop-down menus, we select that the service only works on graphic files in the finder:
image
On the left in the search, we find the action “Run Apple Script” and drag it to the workspace:
image
We write a small script:
image
')
on run tell application "Finder" return POSIX path of (selection as alias) end tell end run 

It consists, in fact, of two lines:
tell application "Finder" - call the application Finder
return POSIX path of (selection as alias) - the script returns the address of the selected file in POSIX style (if you just ask for the path, it will give the address with a colon separator).
Now we find “Run shell script” on the side and also drag it into the workspace. Select in the menu "Submit input: as arguments" In this case, the fact that the top script returns will not be transferred to the standard input, but as if we ran this script with the parameter (this is the path to the file)
image
We write this:
 curl -F "Filedata=@$1" "http://habrastorage.org/uploadController/?username=vvzvlad&userkey=7a25d94cde460365b6f7ce137675c623ec" | json_pp | grep url | awk -F '"' '{print $4}' 

I found this line in this comment. The first part is the POST loading in the Filedata parameter of our file. $ 1 is the argument that passes the top script — the file address. Ie, it looks like this for me:
  curl -F "Filedata=@/Users/vvzvlad/Documents/REVIEWS/alpha\ mio/foto/003.jpg" "http://habrastorage.org/uploadController/?username=vvzvlad&userkey=7a25d94cde460365b6f7ce137675c623ec" 

How to get the magic userkey string is described here . It is necessary to go to the address habrahabr.ru/whoami but not just like that, but with the referrer habrastorage. The easiest way to do this is: go to habrastorage.org, look at the source code, find the line (I have it 13), and poke it.
image
We get the following line, which we copy to our script:
image
The rest of the line:
 | json_pp | grep url | awk -F '"' '{print $4}' 

This is the analysis of the JSON response, in order to tear out from it what interests us - the URL of the loaded picture.
Now we have received the address, but we still need to give it to the user. For example, copy to clipboard. Add another Apple script block in the editor:
image
We have this script in it:
 on run {input} set the clipboard to "<img src=\"" & input & "\" alt=\"image\"/>" display notification input with title "Habrastorage" subtitle " " end run 

on run {input} - we get a line with the address from the previous script
set the clipboard to "<img src = \" "& input &" \ "alt = \" image \ "/>" - copy it to the clipboard, at the same time wrapping it in an img tag
display notification input with title “Habrastorage” subtitle “Picture uploaded” and showing a notification so that the user understands that the picture has already been uploaded and can be inserted into the text.
Everything, the script works, you can download files directly from the Finder, without touching the buggy Flash once again and without opening the browser tab.

Materials:
AppleScript for beginners

The script itself lives here .
It must be unpacked and put in the / Users / User / Library / Services / folder, not forgetting to insert its own line inside.

PS I understand that it is not very optimal, and you can do everything in one Apple Script. But I did not understand how to properly pass the argument inside the do shell script, as well as how to escape characters.

PPS In the article all images are uploaded using this script.

Source: https://habr.com/ru/post/207282/


All Articles