Using rb-appscript to write a bulleted/numbered list in pages or textedit
I need to use rb-appscrip开发者_Python百科t to create a new Pages document that contains bulleted and numbered lists. Looking in to this, I see that paragraphs have a property called list_style, but I'm not familiar enough with rb-appscript or applescript to figure out how to set that property. I have read the documentation generated by the ASDictionary, but my knowledge of AppleScript is apparently too little to understand it.
Any help with either understanding how to use the information presented in the documentation, or writing a list using rb-appscript in pages would be much appreciated.
Edit: I'm not stuck on pages, textedit is also a viable option.
rb-appscript:
require 'rubygems'
require 'appscript'; include Appscript
lst=["a", "b"]
doc = app('Pages').documents[0]
doc.selection.get.paragraph_style.set("Body Bullet")
doc.selection.set(lst.join("\n"))
AppleScript:
set lst to {"a", "b"}
set text item delimiters to linefeed
tell application "Pages" to tell document 1
set paragraph style of (get selection) to "Body Bullet"
set selection to (lst as text)
end tell
The current crop of Apple applications are weird to script. I don't use rb-appscript, but here is working code for Applescript that you should be able to alter to taste and port:
property dummyList : {"Tyler Durden", "Marla Singer", "Robert Paulson"}
tell application "Pages"
set theDocument to make new document
tell theDocument
set bulletListStyle to ""
set lastListStyle to (count list styles)
repeat with thisListStyle from 1 to lastListStyle
set theListStyle to item thisListStyle of list styles
if name of theListStyle is "Bullet" then
set bulletListStyle to theListStyle
end if
end repeat
repeat with thisItem from 1 to (count dummyList)
set body text to body text & item thisItem of dummyList & return
end repeat
set paraCount to count paragraphs of theDocument
repeat with thisPara from 1 to paraCount
select paragraph thisPara
set theSelection to selection
set paragraph style of theSelection to "Body Bullet"
end repeat
end tell
end tell
What this does, essentially, is place each list item in its own paragraph (that is what a list item is for all intents and purposes: an indented paragraph with a bullet), select each paragrah in turn, then apply the list paragraph style to the selection. The paragraph
object just returns the text of the given paragraph and does not hold any state in and of itself, for some reason. This isn't the best way to handle this scenario, but at least all the components are there to get you what you need.
精彩评论