Created
June 29, 2021 12:06
-
-
Save mixio/1725bc5e179b82da1ea1bb35bcbaf824 to your computer and use it in GitHub Desktop.
Run this script to have the contents of a link copied to the clipboard, downloaded into a new BBEdit document.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# README | |
# | |
# 1. Copy some remote link starting with http://, https://, ftp://, sftp:// to the clipboard. | |
# | |
# 2. Run this script to have the contents of the link downloaded into a new BBEdit document. | |
# | |
# INSTALL | |
# | |
## | |
-- | |
use AppleScript version "2.7" | |
use scripting additions | |
use framework "Foundation" | |
-- | |
property NSError : class "NSError" | |
property NSString : class "NSString" | |
property NSURL : class "NSURL" | |
property NSURLConnection : class "NSURLConnection" | |
property NSURLRequest : class "NSURLRequest" | |
property NSURLResponse : class "NSURLResponse" | |
property NSURLRequestReloadIgnoringLocalCacheData : a reference to current application's NSURLRequestReloadIgnoringLocalCacheData | |
property NSUTF8StringEncoding : a reference to current application's NSUTF8StringEncoding | |
-- | |
# Inspired from: <https://forum.latenightsw.com/t/downloading-text-from-a-website-using-asobj-c/949> | |
on getRemoteContents(aURL) | |
set vURL to aURL as string | |
set vNSURL to (NSURL's URLWithString:vURL) | |
set vRequest to NSURLRequest's requestWithURL:vNSURL cachePolicy:(NSURLRequestReloadIgnoringLocalCacheData) timeoutInterval:10 | |
set {vDownloadedData, vHTTPURLResponse} to NSURLConnection's sendSynchronousRequest:vRequest returningResponse:(reference) |error|:(missing value) | |
set vDownloadedNSString to NSString's alloc()'s initWithData:(vDownloadedData) encoding:(NSUTF8StringEncoding) | |
set vMIMEType to MIMEType of vHTTPURLResponse | |
try | |
set vTextEncoding to textEncodingName of vHTTPURLResponse | |
on error | |
set vTextEncoding to missing value | |
end try | |
set vURLValue to |URL| of vHTTPURLResponse | |
set vURL to (vURLValue's absoluteString()) as string | |
return {pURL:vURL, pContents:(vDownloadedNSString as string), pMIMEType:vMIMEType, pTextEncoding:vTextEncoding} | |
end getRemoteContents | |
-- | |
try | |
tell AppleScript | |
set vURL to the clipboard as string | |
if not ((vURL starts with "http://") or (vURL starts with "https://") or (vURL starts with "ftp://") or (vURL starts with "sftp://")) then | |
error "The contents of the clipboard is not a valid URL." & linefeed & linefeed & "The clipboard's contents:" & linefeed & the quoted form of vURL | |
end if | |
end tell | |
tell application "BBEdit" | |
set {pURL:vURL, pContents:vContents, pMIMEType:vMIMEType, pTextEncoding:vTextEncoding} to my getRemoteContents(vURL) | |
make new document at beginning with properties {name:vURL, contents:vContents} | |
end tell | |
on error aMessage | |
display alert aMessage as critical | |
end try | |
-- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment