Create new Internet record and Set Fields
Posted: Sun Sep 29, 2024 1:25 pm
I am an admitted newbie with AppleScript, but I have cobbled together a script to retrieve from a website the basic information to create an Internet reference. How do I insert the data into the fields of a new blank record?
Code: Select all
tell application "Safari"
activate
open location "https://theconversation.com/future-evolution-from-looks-to-brains-and-personality-how-will-humans-change-in-the-next-10-000-years-176997"
delay 5 -- Wait for the page to load
tell front document
-- Retrieve the page title
set pageTitle to do JavaScript "document.title"
if pageTitle is missing value or pageTitle is "" then
set pageTitle to "Title not available"
end if
-- Retrieve the author's name
try
set authorName to do JavaScript "document.querySelector('meta[name=\"author\"]').content"
if authorName is missing value or authorName is "" then error
on error
set authorName to "Author not available"
end try
-- Retrieve the writer's name (if available)
try
set writerName to do JavaScript "document.querySelector('meta[name=\"writer\"]').content"
if writerName is missing value or writerName is "" then error
on error
set writerName to "Writer not available"
end try
-- Retrieve the publication year
try
set publicationYear to do JavaScript "document.querySelector('meta[property=\"article:published_time\"]').content.split('-')[0]"
if publicationYear is missing value or publicationYear is "" then error
on error
set publicationYear to "Year not available"
end try
-- Retrieve the full publication date
try
set publicationDate to do JavaScript "document.querySelector('meta[property=\"article:published_time\"]').content"
if publicationDate is missing value or publicationDate is "" then error
on error
set publicationDate to "Date not available"
end try
-- Retrieve the description
try
set pageDescription to do JavaScript "document.querySelector('meta[name=\"description\"]').content"
if pageDescription is missing value or pageDescription is "" then error
on error
set pageDescription to "Description not available"
end try
-- Retrieve the keywords
try
set pageKeywords to do JavaScript "document.querySelector('meta[name=\"keywords\"]').content"
if pageKeywords is missing value or pageKeywords is "" then error
on error
set pageKeywords to "Keywords not available"
end try
-- Retrieve the publisher's name
try
set publisherName to do JavaScript "document.querySelector('meta[property=\"og:site_name\"]').content"
if publisherName is missing value or publisherName is "" then error
on error
try
-- Fallback: Check for publisher in another meta tag
set publisherName to do JavaScript "document.querySelector('meta[name=\"publisher\"]').content"
if publisherName is missing value or publisherName is "" then error
on error
set publisherName to "Publisher not available"
end try
end try
end tell
end tell
-- Retrieve the access date (current date)
set accessDate to (current date) as string
-- Output the results
set result to "Title: " & pageTitle & return & "Author: " & authorName & return & "Writer: " & writerName & return & "Year: " & publicationYear & return & "Publication Date: " & publicationDate & return & "Description: " & pageDescription & return & "Keywords: " & pageKeywords & return & "Publisher: " & publisherName & return & "Access Date: " & accessDate
display dialog result