Postr supports Lua plugins that can be installed at ~/Library/Application Support/Postr/Plugins/. The plugins are plain-text files that have a “.lua” file extension.
Postr enables these objects in the Lua environment:
ServiceArrayControllerPostArrayControllerSelectedPostIn addition, Postr enables these classes:
PostServiceThe *ArrayController objects correspond to NSArrayControllers and posts and services are NSManagedObjects.
Post objects have the following keys:
desc - The description or notes of a post.hasBeenPosted - An NSNumber representing whether or not the post has been sent to a service.service - The service that the post is associated with.tagstitleurlService objects have the following keys:
api_url - The URL to a services posting API.nameprotocol - The HTTP protocol to use, either “GET” or “POST”.success_response - Some text that should be in the response that Postr receives after sending a post.url - URL to the service’s front or home page.posts - An NSMutableSet of posts associated with the service.scheme - An NSMutableSet of Scheme objects.Scheme objects are simple and only have three components. They define the keys and values for Service APIs.
key - The key for APIs. For the GET protocol, this is the segment between “&” and “=” such as “&key=value”.value - The value to be sent to APIs. This corresponds to a specific key from Post objects (usually desc, tags, title, or url).service - The service that the scheme is associated with.Here are a few small samples of using the above objects within Lua. More complex interactions are possible and the information passed to the Lua environment may be changed in the future.
-- Reverse the title of the selected post
x = SelectedPost:valueForKey("title")
rev = ""
j = string.len(x)
-- Lua v5.0.4 does not have string.reverse(). Will update Lua when framework is updated.
for i=1,j do rev = rev..string.sub(x, j-i+1, j-i+1) end
SelectedPost:setValue_forKey_(rev, "title")
-- Iterate over every post and print the title to stdout
postArray = PostArrayController:content()
e = postArray:objectEnumerator()
post = e:nextObject()
while post do
print(post:valueForKey("title"))
post = e:nextObject()
end
-- Create new post
post = Post:post()
post:setValue_forKey_("This post created in Lua", "title")
post:setValue_forKey_("http://www.lua.org", "url")
post:setValue_forKey_("Created on "..os.date()..".", "desc")
post:setValue_forKey_("lua plugin", "tags")
serviceArray = ServiceArrayController:content()
if serviceArray:count() > 0 then
firstService = serviceArray:objectAtIndex(0)
post:setValue_forKey_(firstService, "service")
end
-- Write service information to Desktop
-- Demonstrates working with services, schemes, and simple io
local f = assert(io.open(os.getenv("HOME").."/Desktop/Postr services.txt", "w+"))
f:write("File created on "..os.date().."\n------------------------------------------\n\n")
serviceArray = ServiceArrayController:content()
e = serviceArray:objectEnumerator()
service = e:nextObject()
while service do
name = service:valueForKey("name")
url = service:valueForKey("url")
apiUrl = service:valueForKey("api_url")
protocol = service:valueForKey("protocol")
if (name) then f:write(string.format("%s ", name)) end
if (url) then f:write(string.format("<%s> ", url)) end
if (apiUrl) then f:write(string.format("\n\t%s", apiUrl)) end
if (protocol) then f:write(string.format(" (%s)", protocol)) end
schemeSet = service:mutableSetValueForKey("scheme")
if (schemeSet) then f:write("\n") end
sse = schemeSet:objectEnumerator()
scheme = sse:nextObject()
while scheme do
f:write(string.format("\t%s = %s\n", scheme:valueForKey("key"), scheme:valueForKey("value")))
scheme = sse:nextObject()
end
f:write("\n\n")
service = e:nextObject()
end
f:close()