# Execute as Ruby Code Service for OS X

**URL:** <https://www.ruby-forum.com/t/execute-as-ruby-code-service-for-os-x/90160>\
**Category:** Ruby\
**Created:** [March 26, 2007, 2:56pm UTC](https://www.ruby-forum.com/t/execute-as-ruby-code-service-for-os-x/90160 "2007-03-26T14:56:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Florian\_GroSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3ec8ea/32.png) [@Florian\_GroSSSS](https://www.ruby-forum.com/u/Florian_GroSSSS)\
**Post date:** [March 26, 2007, 2:56pm UTC](https://www.ruby-forum.com/t/execute-as-ruby-code-service-for-os-x/90160/1 "2007-03-26T14:56:56Z")

</div>

Moin.

I’ve written a system service for OS X that allows you to execute  
selected Ruby code in any OS X application by hitting Apple-\* –  
Adium, Safari, X-Chat, Mail, TextMate, more. Use it as a calculator or  
for quickly executing any kind of Ruby code.

It will turn  
The number is rand  
into  
The number is 0.70682595577091  
when you select rand and hit Apple-\*.

It will turn  
“Hello World”.reverse  
into  
“dlroW olleH”  
when you select the line and hit Apple-\*.

It also supports these things:  
2\*\*32#  
x = 0  
5.times do  
x += 5#  
end  
raise#

They result in this when you select everything and hit Apple-\*:  
2\*\*32 # =\> 4294967296  
x = 0  
5.times do  
x += 5 # =\> 5, 10, 15, 20, 25  
end  
raise # ~\> -:7: unhandled exception

It uses mfp’s xmpfilter.rb and ThisService and is written in Ruby.

To install:

1. Download [http://flgr.0x42.net/ExecuteasRubycode.service.zip](http://flgr.0x42.net/ExecuteasRubycode.service.zip)
2. Unextract the archive if your browser hasn’t done so already.
3. Move ExecuteasRubycode.service to ~/Library/Services or /Library/  
Services.
4. Relogin to OS X.

You can use Service Scrubber ([http://www.manytricks.com/](http://www.manytricks.com/)  
servicescrubber/) to assign a custom hot key.

To see or modify the source code right click the service, select ‘Show  
package contents’ and navigate to Contents/Resources.

Kind regards,  
Florian G.

---

<div class="post-metadata">

**Author:** ![Florian\_GroSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3ec8ea/32.png) [@Florian\_GroSSSS](https://www.ruby-forum.com/u/Florian_GroSSSS)\
**Post date:** [March 26, 2007, 3:34pm UTC](https://www.ruby-forum.com/t/execute-as-ruby-code-service-for-os-x/90160/2 "2007-03-26T15:34:03Z")

</div>

> The number is 0.70682595577091  
> when you select rand and hit Apple-\*.

🙂 It almost certainly wont, you know 😉

That sounds great though, I’ll be grabbing it tonight. Thanks!

---

<div class="post-metadata">

**Author:** ![Florian\_GroSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3ec8ea/32.png) [@Florian\_GroSSSS](https://www.ruby-forum.com/u/Florian_GroSSSS)\
**Post date:** [March 26, 2007, 4:16pm UTC](https://www.ruby-forum.com/t/execute-as-ruby-code-service-for-os-x/90160/3 "2007-03-26T16:16:13Z")

</div>

On Mar 26, 3:33 pm, removed\_email\_address@domain.invalid wrote:

> > It will turn  
> > The number is rand  
> > into  
> > The number is 0.70682595577091  
> > when you select rand and hit Apple-\*.
> 
> 🙂 It almost certainly wont, you know 😉
> 
> That sounds great though, I’ll be grabbing it tonight. Thanks!

I just need a _huge_ user base! 😉

---

<div class="post-metadata">

**Author:** ![Florian\_GroSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3ec8ea/32.png) [@Florian\_GroSSSS](https://www.ruby-forum.com/u/Florian_GroSSSS)\
**Post date:** [March 27, 2007, 10:34pm UTC](https://www.ruby-forum.com/t/execute-as-ruby-code-service-for-os-x/90160/4 "2007-03-27T22:34:03Z")

</div>

On 26 Mar 2007, at 13:55, Florian Groß wrote:

> Moin.
> 
> I’ve written a system service for OS X that allows you to execute  
> selected Ruby code in any OS X application by hitting Apple-\* –  
> Adium, Safari, X-Chat, Mail, TextMate, more. Use it as a calculator or  
> for quickly executing any kind of Ruby code.

I’m sure I’m doing something wrong, but this isn’t working for me.  
It’s installed (well, I can see it in the services menu anyway), but  
if I select some text and select it, nothing seems to happen. Would  
you like some error diagnosis? I don’t know where to start providing  
further information, sorry.

Cheers,  
Benj

---

<div class="post-metadata">

**Author:** ![Florian\_GroSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3ec8ea/32.png) [@Florian\_GroSSSS](https://www.ruby-forum.com/u/Florian_GroSSSS)\
**Post date:** [March 26, 2007, 6:43pm UTC](https://www.ruby-forum.com/t/execute-as-ruby-code-service-for-os-x/90160/5 "2007-03-26T18:43:49Z")

</div>

Hi,

On 3/26/07, Florian GroÃŸ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > That sounds great though, I’ll be grabbing it tonight. Thanks!
> 
> I just need a _huge_ user base! 😉

Add another user to that list. Very nice work Florian. This will  
definitely come in handy!

Thanks,  
Michael G.

---

<div class="post-metadata">

**Author:** ![Florian\_GroSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3ec8ea/32.png) [@Florian\_GroSSSS](https://www.ruby-forum.com/u/Florian_GroSSSS)\
**Post date:** [March 28, 2007, 9:28am UTC](https://www.ruby-forum.com/t/execute-as-ruby-code-service-for-os-x/90160/6 "2007-03-28T09:28:25Z")

</div>

I found a similar thing. It doesn’t work in Mail (just beeps), but  
works in other apps (TextEdit and TextMate are the ones I tried).

Dave.

---

<div class="post-metadata">

**Author:** ![Florian\_GroSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3ec8ea/32.png) [@Florian\_GroSSSS](https://www.ruby-forum.com/u/Florian_GroSSSS)\
**Post date:** [March 28, 2007, 5:16pm UTC](https://www.ruby-forum.com/t/execute-as-ruby-code-service-for-os-x/90160/7 "2007-03-28T17:16:18Z")

</div>

Florian Groß [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Moin.
> 
> I’ve written a system service for OS X that allows you to execute  
> selected Ruby code in any OS X application by hitting Apple-\* –  
> Adium, Safari, X-Chat, Mail, TextMate, more. Use it as a calculator or  
> for quickly executing any kind of Ruby code.

Just FYI, see also Bellhop, which allows you to use Ruby code _as_ a  
system service:

[http://www.xendai.com/bellhop/library/index.html](http://www.xendai.com/bellhop/library/index.html)

Executing selected text would be a subset. m.

---

<div class="post-metadata">

**Author:** ![Florian\_GroSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3ec8ea/32.png) [@Florian\_GroSSSS](https://www.ruby-forum.com/u/Florian_GroSSSS)\
**Post date:** [March 28, 2007, 5:26pm UTC](https://www.ruby-forum.com/t/execute-as-ruby-code-service-for-os-x/90160/8 "2007-03-28T17:26:20Z")

</div>

On 28 Mrz., 17:10, removed\_email\_address@domain.invalid (matt neuburg) wrote:

> Just FYI, see also Bellhop, which allows you to use Ruby code _as_ a  
> system service:
> 
> [http://www.xendai.com/bellhop/library/index.html](http://www.xendai.com/bellhop/library/index.html)
> 
> Executing selected text would be a subset. m.

Ah, that’s an alternative to ThisService with more features then?

ThisService allows you take anything the console executes and turn it  
into a service by feeding the selection to the service as STDIN and  
replacing it by STDOUT.

My service is itself written in Ruby. 🙂

---

<div class="post-metadata">

**Author:** ![Florian\_GroSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3ec8ea/32.png) [@Florian\_GroSSSS](https://www.ruby-forum.com/u/Florian_GroSSSS)\
**Post date:** [March 28, 2007, 3:30am UTC](https://www.ruby-forum.com/t/execute-as-ruby-code-service-for-os-x/90160/9 "2007-03-28T03:30:43Z")

</div>

On Mar 27, 10:33 pm, Benjohn B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m sure I’m doing something wrong, but this isn’t working for me.  
> It’s installed (well, I can see it in the services menu anyway), but  
> if I select some text and select it, nothing seems to happen.

I think the archive I uploaded was broken. I’ve reuploaded it and it  
works now.

Unfortunately, there seems to be a bug in Apple’s Mail.app: When you  
try to use the service in its compose mail text panel the clip board  
will be pasted instead. In other fields in Mail.app the service will  
work as expected, however. This has been reported to Apple months ago  
by ThisService’s author.
