From 9a730afb49122a5de0e2658ef7b80d657b81dffd Mon Sep 17 00:00:00 2001 From: Olof Hellman Date: Wed, 10 Jul 2019 00:02:20 -0700 Subject: [PATCH 1/2] Add sample script for creating a new outgoing message in Mail, fix accessor for the 'name of author' --- .../Mail-CreateOutgoingMessage.applescript | 86 +++++++++++++++++++ Mac/Resources/NetNewsWire.sdef | 1 + Mac/Scriptability/Author+Scriptability.swift | 8 +- Mac/Scriptability/Feed+Scriptability.swift | 2 +- 4 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 AppleScript/Mail-CreateOutgoingMessage.applescript diff --git a/AppleScript/Mail-CreateOutgoingMessage.applescript b/AppleScript/Mail-CreateOutgoingMessage.applescript new file mode 100644 index 000000000..349ce1c3e --- /dev/null +++ b/AppleScript/Mail-CreateOutgoingMessage.applescript @@ -0,0 +1,86 @@ +-- This script grabs the current article in NetNewsWire and copies relevant information about it +-- to a new outgoing message in Mail +-- the intended use is that the user wants to send email about the current article, and +-- would fill in the recipient and then send the message + +-- sometimes, an article has contents, and sometimes it has html contents +-- this function getContentsOrHtml() gets the contents as text, despite the representation +-- first it checks to see if there are plain text contents +-- if not, it looks for html contents, and converts those to plain text using a shell script that invokes textutil +-- if it can't find either plain text or html, it returns "couldn't find article text" +to getContentsOrHtml() + tell application "NetNewsWire" + set textContents to the contents of the current article + if textContents is not "" then + return textContents + else + set htmlContents to html of the current article + if htmlContents is not "" then + set shellScript to " echo '" & htmlContents & "' | /usr/bin/textutil -stdin -stdout -format html -convert txt" + set pureText to do shell script shellScript + return pureText + end if + end if + end tell + return "couldn't find article text" +end getContentsOrHtml + + +-- given a list of author names, generate a happily formatted list like "Jean MacDonald and James Dempsey" +-- if the list is more than two names, use Oxford comma structure: "Brent Simmons, Jean MacDonald, and James Dempsey" +-- the returned string is padded by spaces on both sides + +to formatListOfNames(listOfNames) + set c to count listOfNames + if c is 1 then + set formattedList to item 1 of listOfNames + else if c is 2 then + set formattedList to item 1 of listOfNames & " and " & item 2 of listOfNames + else + set frontOfList to items 1 thru (c - 1) of listOfNames + set lastName to item c of listOfNames + set tid to AppleScript's text item delimiters + set AppleScript's text item delimiters to ", " + set t1 to frontOfList as text + set formattedList to t1 & ", and " & lastName + set AppleScript's text item delimiters to tid + end if + return " from " & formattedList & " " +end formatListOfNames + + +-- sometimes, an article has an author, sometimes it has more than one, sometimes there's no author +-- this function getAuthorStub() returns a string like " by Kaya Thomas " that can be used in crafting a message +-- about the current article. If there are no authors, it just returns a single space. +to getAuthorStub(authorNames) + try + if ((count authorNames) is greater than 0) then + return formatListOfNames(authorNames) + end if + end try + return " " +end getAuthorStub + + + +-- Here's where the script starts + +-- first, get some relevant info out for NetNewsWire +tell application "NetNewsWire" + set articleUrl to the url of the current article + set articleTitle to the title of the current article + set authorNames to name of authors of the current article +end tell + + +-- then, prepare the message subject and message contents +set messageSubject to "From NetNewsWire to you: " & articleTitle +set myIntro to "Here's something" & getAuthorStub(authorNames) & "that I was reading on NetNewsWire: " +set messageContents to myIntro & return & return & articleUrl & return & return & getContentsOrHtml() + + +-- lastly, make a new outgoing message in Mail with the given subject and contents +tell application "Mail" + set m1 to make new outgoing message with properties {subject:messageSubject} + set content of m1 to messageContents +end tell \ No newline at end of file diff --git a/Mac/Resources/NetNewsWire.sdef b/Mac/Resources/NetNewsWire.sdef index c025b6e1d..eecc40413 100644 --- a/Mac/Resources/NetNewsWire.sdef +++ b/Mac/Resources/NetNewsWire.sdef @@ -134,6 +134,7 @@ + diff --git a/Mac/Scriptability/Author+Scriptability.swift b/Mac/Scriptability/Author+Scriptability.swift index d4156144e..1b39cb0e9 100644 --- a/Mac/Scriptability/Author+Scriptability.swift +++ b/Mac/Scriptability/Author+Scriptability.swift @@ -27,6 +27,11 @@ class ScriptableAuthor: NSObject, UniqueIdScriptingObject { return (scriptObjectSpecifier) } + @objc(scriptingSpecifierDescriptor) + func scriptingSpecifierDescriptor() -> NSScriptObjectSpecifier { + return (self.objectSpecifier ?? NSScriptObjectSpecifier() ) + } + // MARK: --- ScriptingObject protocol --- var scriptingKey: String { @@ -35,9 +40,6 @@ class ScriptableAuthor: NSObject, UniqueIdScriptingObject { // MARK: --- UniqueIdScriptingObject protocol --- - // I am not sure if account should prefer to be specified by name or by ID - // but in either case it seems like the accountID would be used as the keydata, so I chose ID - @objc(uniqueId) var scriptingUniqueId:Any { return author.authorID diff --git a/Mac/Scriptability/Feed+Scriptability.swift b/Mac/Scriptability/Feed+Scriptability.swift index 42fefde87..40e6dfa0b 100644 --- a/Mac/Scriptability/Feed+Scriptability.swift +++ b/Mac/Scriptability/Feed+Scriptability.swift @@ -12,7 +12,7 @@ import Account import Articles @objc(ScriptableFeed) -class ScriptableFeed: NSObject, UniqueIdScriptingObject, ScriptingObjectContainer{ +class ScriptableFeed: NSObject, UniqueIdScriptingObject, ScriptingObjectContainer { let feed:Feed let container:ScriptingObjectContainer From 27e57aad6c720ec478c595d5ca6697aa9583fd21 Mon Sep 17 00:00:00 2001 From: Olof Hellman Date: Wed, 10 Jul 2019 00:08:30 -0700 Subject: [PATCH 2/2] fiddle with where the padding spaces are added --- AppleScript/Mail-CreateOutgoingMessage.applescript | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/AppleScript/Mail-CreateOutgoingMessage.applescript b/AppleScript/Mail-CreateOutgoingMessage.applescript index 349ce1c3e..0bd50c6e4 100644 --- a/AppleScript/Mail-CreateOutgoingMessage.applescript +++ b/AppleScript/Mail-CreateOutgoingMessage.applescript @@ -28,7 +28,6 @@ end getContentsOrHtml -- given a list of author names, generate a happily formatted list like "Jean MacDonald and James Dempsey" -- if the list is more than two names, use Oxford comma structure: "Brent Simmons, Jean MacDonald, and James Dempsey" --- the returned string is padded by spaces on both sides to formatListOfNames(listOfNames) set c to count listOfNames @@ -45,17 +44,17 @@ to formatListOfNames(listOfNames) set formattedList to t1 & ", and " & lastName set AppleScript's text item delimiters to tid end if - return " from " & formattedList & " " + return formattedList end formatListOfNames -- sometimes, an article has an author, sometimes it has more than one, sometimes there's no author --- this function getAuthorStub() returns a string like " by Kaya Thomas " that can be used in crafting a message +-- this function getAuthorStub() returns a string like " from Jean MacDonald " that can be used in crafting a message -- about the current article. If there are no authors, it just returns a single space. to getAuthorStub(authorNames) try if ((count authorNames) is greater than 0) then - return formatListOfNames(authorNames) + return " from " & formatListOfNames(authorNames) & " " end if end try return " "