Applescript(通过appscript)停止循环处理

时间:2012-12-10 21:14:41

标签: applescript appscript rb-appscript

以下ruby代码会自动在VoodooPad中创建页面,并为每个页面添加元标记。

vp = app("VoodooPad.app")
doc = vp.open vpdoc

page_names = [@name]

if self.class.to_s.match('Stake::Stack')
    # Create the release notes page. Only run on parent stack
    notes = "#{@name} Release Notes"
    page_names.push notes
    doc.remove :with_name => notes
    doc.create :new => :page, :with_name => notes, :with_content => self.release_notes
end

# Create the settings page
settings = "#{@name} Settings"
page_names.push settings
doc.remove :with_name => settings
doc.create :new => :page, :with_name => settings, :with_content => self.to_md

page_names.each do |page_name|
    # Add the meta tags to the product page
    page = doc.pages[page_name]
    puts page_name
    page.remove_meta_record :with_key => "description"
    page.remove_meta_record :with_key => "url"
    page.remove_meta_record :with_key => "name"
    page.remove_meta_record :with_key => "image"
    page.remove_meta_record :with_key => "version"
    page.remove_meta_record :with_key => "stacks_version"
    page.add_meta_record :with_value => {'version'          => @version_str}
    page.add_meta_record :with_value => {'stacks_version'   => @stacks_version}
    page.add_meta_record :with_value => {'subtitle'         => @subtitle}
    page.add_meta_record :with_value => {'url'              => @info_url}
    page.add_meta_record :with_value => {'image'            => @basename + '@128.png'}
    page.add_meta_record :with_value => {'name'             => @name}
end

在每个块中,相同的元标记被添加到3页。但是,标签只会添加到3页中的2页。如果我们以“徽章”的名称为例,这里是我的页面名称。

Badges
Badges Release Notes
Badges Settings

无论数组的顺序如何,“徽章”页面始终会被正确标记。但是,对于其他2页,有史以来处理的第一个作品。另一个没有。

我想也许是页面名称中的空格。但是,这没关系。我得到了同样的行为。如果您注意到代码也可以正确地动态创建这两个页面。

我也尝试过加入睡眠,认为这可能是一个时间问题。不。

我被困住了。救命。

1 个答案:

答案 0 :(得分:0)

我明白了。我添加标签的页面可能不是当前活动的页面。

vp = app("VoodooPad.app")
doc = vp.open vpdoc

page_names = Array.new

if self.class.to_s.match('Stake::Stack')
    # Only the parent stack has a main page
    page_names.push @name

    # Create the release notes page. Only run on parent stack
    notes = "#{@name} Release Notes"
    page_names.push notes
    doc.remove :with_name => notes
    doc.create :new => :page, :with_name => notes, :with_content => self.release_notes
end

# Create the settings page
settings = "#{@name} Settings"
page_names.push settings
doc.remove :with_name => settings
doc.create :new => :page, :with_name => settings, :with_content => self.to_md

# Have to open to different page to ensure its not open when I add meta tags.
doc.open_page :with_name => 'index'

page_names.each do |page_name|
    # Add the meta tags to the product page
    page = doc.pages[page_name]
    puts page_name
    page.remove_meta_record :with_key => "description"
    page.remove_meta_record :with_key => "url"
    page.remove_meta_record :with_key => "name"
    page.remove_meta_record :with_key => "image"
    page.remove_meta_record :with_key => "version"
    page.remove_meta_record :with_key => "stacks_version"
    page.add_meta_record :with_value => {'version'          => @version_str}
    page.add_meta_record :with_value => {'stacks_version'   => @stacks_version}
    page.add_meta_record :with_value => {'subtitle'         => @subtitle}
    page.add_meta_record :with_value => {'url'              => @info_url}
    page.add_meta_record :with_value => {'image'            => "#{@basename}@128.png".downcase}
    page.add_meta_record :with_value => {'name'             => @name}
    # Open current page to ensure next page is not open or else cannot add tags
    doc.open_page :with_name => page_name
end