Merge pull request #798 from olofhellman/master
First sample script -- Create an outgoing message in Mail with info about the current article
This commit is contained in:
commit
8e2fef072e
|
@ -0,0 +1,85 @@
|
||||||
|
-- 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"
|
||||||
|
|
||||||
|
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 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 " 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 " from " & 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
|
|
@ -134,6 +134,7 @@
|
||||||
</class>
|
</class>
|
||||||
|
|
||||||
<class name="author" code="Athr" plural="authors" description="A feed author">
|
<class name="author" code="Athr" plural="authors" description="A feed author">
|
||||||
|
<cocoa class="ScriptableAuthor"/>
|
||||||
<property name="name" code="pnam" type="text" access="r" description="The name of the author">
|
<property name="name" code="pnam" type="text" access="r" description="The name of the author">
|
||||||
<cocoa key="name"/>
|
<cocoa key="name"/>
|
||||||
</property>
|
</property>
|
||||||
|
|
|
@ -27,6 +27,11 @@ class ScriptableAuthor: NSObject, UniqueIdScriptingObject {
|
||||||
return (scriptObjectSpecifier)
|
return (scriptObjectSpecifier)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@objc(scriptingSpecifierDescriptor)
|
||||||
|
func scriptingSpecifierDescriptor() -> NSScriptObjectSpecifier {
|
||||||
|
return (self.objectSpecifier ?? NSScriptObjectSpecifier() )
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: --- ScriptingObject protocol ---
|
// MARK: --- ScriptingObject protocol ---
|
||||||
|
|
||||||
var scriptingKey: String {
|
var scriptingKey: String {
|
||||||
|
@ -35,9 +40,6 @@ class ScriptableAuthor: NSObject, UniqueIdScriptingObject {
|
||||||
|
|
||||||
// MARK: --- UniqueIdScriptingObject protocol ---
|
// 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)
|
@objc(uniqueId)
|
||||||
var scriptingUniqueId:Any {
|
var scriptingUniqueId:Any {
|
||||||
return author.authorID
|
return author.authorID
|
||||||
|
|
|
@ -12,7 +12,7 @@ import Account
|
||||||
import Articles
|
import Articles
|
||||||
|
|
||||||
@objc(ScriptableFeed)
|
@objc(ScriptableFeed)
|
||||||
class ScriptableFeed: NSObject, UniqueIdScriptingObject, ScriptingObjectContainer{
|
class ScriptableFeed: NSObject, UniqueIdScriptingObject, ScriptingObjectContainer {
|
||||||
|
|
||||||
let feed:Feed
|
let feed:Feed
|
||||||
let container:ScriptingObjectContainer
|
let container:ScriptingObjectContainer
|
||||||
|
|
Loading…
Reference in New Issue