Applescript (via appscript) stops processing in loop

128 Views Asked by At

The following ruby code automatically creates pages inside VoodooPad and adds meta tags to each page.

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

In the each block, the same meta tags are getting added to 3 pages. However, the tags are only getting added to 2 of the 3 pages. If we take the name of "Badges" as an example, here are my page names.

Badges
Badges Release Notes
Badges Settings

No matter the order of the array, the "Badges" page always gets the properly tagged. However, for the other 2 pages, which ever is processed first works. The other does not.

I thought maybe it was spaces in the page names. However, that does not matter. I get the same behavior. If you notice the code also dynamically creates both of these pages properly.

I have also tried adding a sleep, thinking it may be a timing thing. Nope.

I am stuck. Help.

1

There are 1 best solutions below

0
On BEST ANSWER

I figured it out. The page that I was adding tags to could not be the currently active page.

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