[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Using ruby in applescript studio

bluephonic

12/16/2006 9:24:00 PM

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/Wikipedi...

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... and
http://chneukirchen.org/repos/rubypants/ru... ). 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.+<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("===", "vvv").gsub("==", "vv")


# 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

7 Answers

matt

12/16/2006 9:31:00 PM

0

<bluephonic@gmail.com> 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.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Tiger - http://www.takecontrolbooks.com/tiger-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

bluephonic

12/16/2006 9:32:00 PM

0

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

Bnerd[TM]

12/16/2006 9:48:00 PM

0

bluephonic@gmail.com <bluephonic@gmail.com> 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

bluephonic

12/16/2006 9:56:00 PM

0

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 Dec 16, 4:48 pm, Bernd Haug <h...@berndhaug.net> wrote:
> bluepho...@gmail.com <bluepho...@gmail.com> 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

bluephonic

12/18/2006 12:51:00 AM

0

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.


On Dec 16, 4:56 pm, bluepho...@gmail.com wrote:
> 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 Dec 16, 4:48 pm, Bernd Haug <h...@berndhaug.net> wrote:
>
> > bluepho...@gmail.com <bluepho...@gmail.com> 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

Logan Capaldo

12/18/2006 1:29:00 AM

0

On Mon, Dec 18, 2006 at 09:55:16AM +0900, bluephonic@gmail.com wrote:
> 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.
>
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.

matt

12/18/2006 2:12:00 AM

0

Logan Capaldo <logancapaldo@gmail.com> wrote:

> On Mon, Dec 18, 2006 at 09:55:16AM +0900, bluephonic@gmail.com wrote:
> > 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.
> >
> 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.rubyforg...

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

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Tiger - http://www.takecontrolbooks.com/tiger-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...