Saturday, October 1, 2022

#1 2022-09-03 02:48:49 am

From:: Greece
Registered: 2019-03-03
Posts: 2524

Curl: get remote file's info as AppleScript record

Before downloading a remote file (for example, from an FTP server), a user often wants to know in advance information about the file size, its md5 checksum, last modified date (exactly there on the server, not on the local disk), etc.

The following handler will help them get all the information as usual AppleScript record:


on remoteFileInfo(theURL)
   set requestResponse to do shell script "curl -s --head " & theURL
   set remoteFileExists to word 3 of requestResponse
   if remoteFileExists = "200" then return ({«class usrf»:(do shell script "curl -s --head " & theURL & " | egrep -o '\\w[^:]+|:.+' | awk '{gsub(\" +$|: \",\"\"); print} ' ")'s paragraphs 2 thru -1})'s contents as any
   return "File Not Founded"
end remoteFileInfo

set theURL to ""
set remoteFileInfoRecord to remoteFileInfo(theURL)

Here is examples of gotten record keys values:


set fileSize to |content-length| of remoteFileInfoRecord
--> 112531050 (bytes)

set lastModifiedDate to |last-modified| of remoteFileInfoRecord
--> Thu, 11 Aug 2022 12:35:46 GMT

Last edited by KniazidisR (2022-09-03 02:57:36 am)

Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)