【问题标题】:Using rb-appscript to write a bulleted/numbered list in pages or textedit使用 rbc-app 脚本在页面或文本编辑中编写项目符号/编号列表
【发布时间】:2011-09-19 00:01:46
【问题描述】:
我需要使用 rb-appscript 创建一个包含项目符号和编号列表的新 Pages 文档。对此,我看到段落有一个名为 list_style 的属性,但我对 rb-appscript 或 applescript 不够熟悉,无法弄清楚如何设置该属性。我已经阅读了由 ASDictionary 生成的文档,但是我对 AppleScript 的了解显然太少,无法理解。
任何有关如何使用文档中提供的信息或在页面中使用 rb-appscript 编写列表的帮助将不胜感激。
编辑:我没有卡在页面上,textedit 也是一个可行的选择。
【问题讨论】:
标签:
applescript
sourceforge-appscript
rb-appscript
【解决方案1】:
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
【解决方案2】:
当前的 Apple 应用程序编写起来很奇怪。我不使用 rb-appscript,但这里是 Applescript 的工作代码,您应该能够更改以适应和移植:
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
本质上,这样做是将每个列表项放在其自己的段落中(这就是列表项的所有意图和目的:带有项目符号的缩进段落),依次选择每个段落,然后应用列表选择的段落样式。 paragraph 对象只是返回给定段落的文本,并且由于某种原因,它本身不保持任何状态。这不是处理这种情况的最佳方式,但至少所有组件都可以满足您的需求。