Using ruby in applescript studio

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::open(“#{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.+/m, ‘’)
.gsub(/{{[^{]+?}}/, ‘’)
.gsub(/{| class="wikitable".+|}/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>/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(’
’, ‘’)
.gsub(/¶\s*\Z/, ‘’)

return theArticleText
end

theArticleText = cleanUp(onlineWikipediaArticle(theArticleName))

puts theArticleText

[email protected] wrote:

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.

Aw, crap: it looks like usenet added line breaks to my code. I’ve put
the .rb file online instead: http://benyates.info/getArticle.rb

[email protected] [email protected] wrote:

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

Okay, I’ve run into a problem. I’ve added the .rb file to the project
in XCode and referenced it in the applescript file with this line:

set theArticleText to (do shell script (quoted form of POSIX path of

(path to resource “getArticle.rb”)) & " “” & theArticleName & “” “”
& wikiLanguage & “” “” & wikiProject & “”" with administrator
privileges)

But whenever I run the program, it throws an error – “Permission
denied” – even after I enter my password.

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).

On Mon, Dec 18, 2006 at 09:55:16AM +0900, [email protected] wrote:

Just a guess here but either chmod +x getArticle.rb or change the
applescript to say
do shell script "ruby " & (quoted form of POSIX path of (path to
resource “getArticle.rb”)) & …

Geez AppleScript is verbose.

Logan C. [email protected] wrote:

denied" – even after I enter my password.

Just a guess here but either chmod +x getArticle.rb or change the
applescript to say
do shell script "ruby " & (quoted form of POSIX path of (path to
resource “getArticle.rb”)) & …

That’s right.

Geez AppleScript is verbose.

That’s right too. Don’t get me started. My book contains the full rant.
Fortunately, we now have alternate, Ruby-based ways of sending Apple
events to AppleScriptable apps, such as rb-appscript:

http://rb-appscript.rubyforge.org/

This means that instead of using a mix of AppleScript and Ruby, I just
stay in Ruby all the time. :slight_smile: m.