Using ruby in applescript studio

B

bluephonic

Hi -- I'm not a programmer by trade or inclination, so this might be a
stupid question, but is there any way to add a ruby file to an
applescript studio project?

More detail: I'm writing a program to grab an article from Wikipedia,
format it into a cool-looking poster in Illustrator, and save it.
Right now the program uses BBEdit for regex-ing the text from raw
wikisyntax into something nice, but I've coded a script in ruby that
will do the same thing. Now I just need to figure out how to combine
the ruby file with the applescript project and pass it the name of the
article.

You can download the existing program at
http://benyates.info/WikipediaPrint.dmg

The ruby file follows (I'm sure its terrible and hackish in many ways,
due to my not being a programmer). You'll have to have html entity
support and RubyPants installed (
http://po-ru.com/projects/html-entities/ and
http://chneukirchen.org/repos/rubypants/rubypants.rb ). I'm not sure
whether the script will work on all systems because it includes unicode
characters (Apple Roman?).


require "net/http"
require "uri"
require "htmlentities"
require "rubypants"

#####################################################

# This is where you specify what article you want!

# The full program takes care of the URL formatting, but for now,
# make sure the capitalization is the same as that of the Wikipedia
# article, and replace all spaces with underscores

theArticleName = "Acoustic_Kitty"

#####################################################



############################################################
# this is just for testing when I'm not connected to the net
def offlineWikipediaArticle
homeFolder = File::expand_path("~")

File::eek:pen("#{homeFolder}/Desktop/train.xml").readlines.to_s
end
############################################################



def onlineWikipediaArticle(theArticleName)
wikiLanguage = 'en'
wikiProject = 'wikipedia'

wikiExportPath =
URI.parse("http://#{wikiLanguage}.#{wikiProject}.org/wiki/Special:Export/#{theArticleName}")


theArticleText = Net::HTTP.start(wikiExportPath.host,
wikiExportPath.port) do |http|
http.get(wikiExportPath.path, {'User-Agent' => 'WikipediaPrint'})
end
return theArticleText.body

end


def cleanUp(theArticleText)
# remove xml, templates, wikitables, and leading carriage returns
theArticleText = theArticleText\
.gsub(/<mediawiki xmlns.+<text
xml:space="preserve">/m, '')\
.gsub(/\{\{[^\{]+?\}\}/, '')\
.gsub(/\{\| class=&quot;wikitable&quot;.+\|\}/m, '')\
.gsub(/\A\n+/, '')


# replace pipelinks, remove images, replace regular wikilinks
# remove noincludes, remove categories
# remove leading semicolons
theArticleText = theArticleText\
.gsub(/\[\[[^\]\[\|]+\|([^\]\[\|]+)\]\]/, '\1')\
.gsub(/\[\[image:[^\|\[\]]+\|[^\|\[\]]+\|.+\]\]/i,
'')\
.gsub(/\[\[image:.+\]\]/i, '')\
.gsub(/\[\[([^\|]+?)\]\]/, '\1')\
.gsub(/<noinclude>.+?<\/noinclude>/i, '')\
.gsub(/\[\[category:.+\]\]/i, '')\
.gsub('\n\n;', '\n\n')

# format urls
theArticleText = theArticleText.gsub(/\[(http:\/\/\S+\\*\w)(.+)\]/i,
'\2 (\1)')

# remove references, remove comments
# remove sections: External links, see also, notes and references,
etc.
theArticleText = theArticleText\
.gsub(/<ref.+<\/ref>/mi, '')\
.gsub(/<!--.+-->/m, '')\
.gsub(/==\s*?External links\s*?==.+/mi, '')\
.gsub(/==\s*?See also\s*?==.+/mi, '')\
.gsub(/==\s*?Notes and References\s*?==.+/mi, '')\
.gsub(/==\s*?References\s*?==.+/mi, '')

# Format lists -- 2nd-order first, then top-level
theArticleText = theArticleText\
.gsub(/\.*\n\s*(\*\*)\s*/, '. · ')\
.gsub(/\.*?\n\s*(\*|#)\s*/, '. • ')\
.gsub('==. •', '== •')


# Replace line breaks
theArticleText = theArticleText\
.gsub(/\n\n\:/, '\n\n')\
.gsub(/\n/, '¶')\
.gsub(/(¶\s*)+/, ' ¶ ')\
.gsub(/==\s*¶/, '==')\
.gsub(/¶\s*==/, ' ==')\
.gsub(/\A\s*¶\s*/, '')

# Convert wikisyntax delimitors ('' and ==) to less-common alternates
theArticleText = theArticleText\
.gsub("'''", "â—Šâ—Šâ—Š").gsub("''", "â—Šâ—Š")\
.gsub("===", "√√√").gsub("==", "√√")


# Convert quotes into smart-quote html entities
# decode all html entities into unicode (twice, becase ampersands are
encoded)
# fix rubypants' mistakes (Hackish. Sorry.)
theArticleText = RubyPants::new(theArticleText)\
.to_html.decode_entities.decode_entities\
.gsub(' â€', ' “').gsub('“’', '“‘')


# Minor cleanup
theArticleText = theArticleText\
.gsub(' • ', ' • ')\
.gsub(' · ', ' · ')\
.gsub('<br>', '')\
.gsub(/¶\s*\Z/, '')

return theArticleText
end



theArticleText = cleanUp(onlineWikipediaArticle(theArticleName))

puts theArticleText
 
M

matt neuburg

Hi -- I'm not a programmer by trade or inclination, so this might be a
stupid question, but is there any way to add a ruby file to an
applescript studio project?

Sure - do it just the same way as you'd add a Perl file to an
AppleScript Studio project, as demonstrated in an extensive example in
my AppleScript book. AppleScript can find the Ruby file within the
application's bundle using "path to resource", and can then call it
using "do shell script". m.
 
B

Bernd Haug

Hi -- I'm not a programmer by trade or inclination, so this might be a
stupid question, but is there any way to add a ruby file to an
applescript studio project?

I don't know that, but did you consider going it the other way around
and calling the AppleScript from ruby? The command-line tool osascript
might interest you.

lg, Bernd
 
B

bluephonic

I thought about that, but (if I'm not mistaken) it would mean anyone
who used the program would have to have osascript installed -- since
the thing already requires a mac and Illustrator CS2 (reducing the
potential audience to about 100 people) I want to get rid of other
dependancies. (Plus, I don't want to have to rewrite everything in
Ruby.)

Matt: Thanks! I'll see if I can figure that out (and if the program
makes me a why-esque microcelebrity, I'll buy your book).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top