Công dụng dùng để up một hoặc nhiều bức ảnh lên trang chia sẻ ảnh imgur.com để lấy link ảnh đó, đoạn code sau sẽ tự up và tự lấy link ảnh cho các bạn.
Để up ảnh lên imgur thì có 2 loại là up ảnh nặc danh có nghĩa là không vào tài khoản của mình, và up ảnh có định danh nghĩa là vào tài khoản của mình.
Ở đây thì mình chỉ cách up định danh. Ảnh sẽ vào album tài khoản của các bạn cho dễ quản lý.
Đầu tiên thì cần phải có 1 tài khoản trên imgur.com, sau đó các bạn vào https://imgur.com/register/api_anon để đăng kí sửa dụng hàm api
Để up ảnh lên imgur thì có 2 loại là up ảnh nặc danh có nghĩa là không vào tài khoản của mình, và up ảnh có định danh nghĩa là vào tài khoản của mình.
Ở đây thì mình chỉ cách up định danh. Ảnh sẽ vào album tài khoản của các bạn cho dễ quản lý.
Đầu tiên thì cần phải có 1 tài khoản trên imgur.com, sau đó các bạn vào https://imgur.com/register/api_anon để đăng kí sửa dụng hàm api
Các bạn chọn như trong hình phần application name và description có thể điền tuỳ thích.
Sau khi đăng kí xong các bạn sẽ có 2 dãy biến là Client ID và Client secret.
Vào trình duyệt paste đoạn url sau:
Code:
https://api.imgur.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&response_type=token
Nhấn enter và lúc này trang imgur sẽ hỏi các bạn có cấp phép cho ứng dụng vừa rồi của các bạn kết nối tới tài khoản của bạn không. Nhấn Allow để cấp phép.
Khi nhấn Allow trang web sẽ trả về 1 đoạn link trên trình duyệt các bạn, vd như của mình sẽ là:
Code:
http://example.com/#access_token=d86109136f0f8419181f5f06d65c92f54f616d00&expires_in=3600&token_type=bearer&refresh_token=256306a996f9007e73a06cec346b31082c244d4f&account_username=nemtabi
Ok phần khó nhất đã xong, giờ vào cái chính.
Các bạn mở chương trình Applescript Editor
Paste đoạn code sau vào đó.
Code:
property Client_ID : "XXXXXXX" property Client_Secret : "XXXXXXX" property refresh_token : "XXXXXXX" -- Supported file types property fileTypes : {"jpeg", "jpg", "gif", "png", "apng", "tiff", "bmp", "pdf", "xcf"} -- If true, this will enclose the URL in a [img][/img] tag property img_tag : true property listOfLinks : {} on run -- Verify the selected file is a supported file type tell application "Finder" set userSelect to (selection as alias list) if (count of userSelect) < 1 then tell me to badChoice() else if name extension of first item of userSelect is not in fileTypes then tell me to badChoice() end if end tell --Request new token set a to do shell script "curl -X POST -F client_id=" & Client_ID & " -F client_secret=" & Client_Secret & " -F grant_type=refresh_token -F refresh_token=" & refresh_token & " https://api.imgur.com/oauth2/token" set {TID, text item delimiters} to {text item delimiters, {"\"access_token\":\"", "\",\"expires_in"}} set new_token to text item 2 of a set text item delimiters to TID -- Upload the file repeat with i from 1 to number of items of the userSelect set selectedItem to item i of the userSelect set theFile to quoted form of POSIX path of selectedItem set imgurUpload to do shell script "curl -X POST -H 'Authorization: Bearer " & new_token & "' -F image=@" & theFile & " https://api.imgur.com/3/upload " -- Copy the URL into your clipboard set {TID, text item delimiters} to {text item delimiters, {"http:\\/\\/i.imgur.com\\/", "\"},\"success\":true,\"status\":200}"}} if img_tag then copy "[img]http://i.imgur.com/" & text item 2 of imgurUpload & "[/img]" & "\n" to the end of listOfLinks else copy "http://i.imgur.com/" & text item 2 of imgurUpload & "\n" to the end of listOfLinks end if -- Reset delimiters set text item delimiters to TID end repeat set the clipboard to listOfLinks as string set listOfLinks to {} end run on badChoice() using terms from application "Finder" tell me activate display alert "Please select ONE supported file and try again." message "Allowed file types: JPEG, JPG, GIF, PNG, APNG, TIFF, BMP, PDF, XCF (GIMP). " & return & return & "Please note that the TIFF, BMP, PDF and XCF formats will be converted to PNG on upload." buttons "OK" cancel button "OK" end tell end using terms from end badChoice
Sau đó chọn export trong chương trình Applescript editor và chọn File Format là Application rồi save lại, cuối cùng kéo ứng dụng này lên thanh toolbar của finder, khi nào muốn up ảnh nào chỉ việc chọn ảnh đó và click vào biểu tượng của chương trình này, nó sẽ chạy khi nào xong tự tắt khi đó link ảnh sẽ được copy và clipboard.
Quảng cáo
À nếu muốn bỏ tag
Code:
[IMG] [/IMG]