# Using ruby in applescript studio

**URL:** <https://www.ruby-forum.com/t/using-ruby-in-applescript-studio/79598>\
**Category:** Ruby\
**Created:** [December 16, 2006, 10:27pm UTC](https://www.ruby-forum.com/t/using-ruby-in-applescript-studio/79598 "2006-12-16T22:27:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 16, 2006, 10:27pm UTC](https://www.ruby-forum.com/t/using-ruby-in-applescript-studio/79598/1 "2006-12-16T22:27:03Z")

</div>

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](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/](http://po-ru.com/projects/html-entities/) and  
[http://chneukirchen.org/repos/rubypants/rubypants.rb](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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 16, 2006, 10:37pm UTC](https://www.ruby-forum.com/t/using-ruby-in-applescript-studio/79598/2 "2006-12-16T22:37:04Z")

</div>

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 16, 2006, 10:37pm UTC](https://www.ruby-forum.com/t/using-ruby-in-applescript-studio/79598/3 "2006-12-16T22:37:05Z")

</div>

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](http://benyates.info/getArticle.rb)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 16, 2006, 10:57pm UTC](https://www.ruby-forum.com/t/using-ruby-in-applescript-studio/79598/4 "2006-12-16T22:57:08Z")

</div>

removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 18, 2006, 1:55am UTC](https://www.ruby-forum.com/t/using-ruby-in-applescript-studio/79598/5 "2006-12-18T01:55:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 16, 2006, 11:01pm UTC](https://www.ruby-forum.com/t/using-ruby-in-applescript-studio/79598/6 "2006-12-16T23:01:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 18, 2006, 2:29am UTC](https://www.ruby-forum.com/t/using-ruby-in-applescript-studio/79598/7 "2006-12-18T02:29:50Z")

</div>

On Mon, Dec 18, 2006 at 09:55:16AM +0900, removed\_email\_address@domain.invalid 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.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 18, 2006, 3:15am UTC](https://www.ruby-forum.com/t/using-ruby-in-applescript-studio/79598/8 "2006-12-18T03:15:54Z")

</div>

Logan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](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. 🙂 m.
