perl script in AS another question

Hi all.
Can you help me to understand how to run perl script from applescript?
I have something like this:
applescript variable inputText “Width 10 mm”
I want to check using perl via AS do shell script if inputText contains mm at the end of the line (s/mm$/)

I need statement like this:
if ( check via perl if inputText contains mm) then
display dialog “You have millimeters units”
display dialog “You have other units”
end if

I think it should be something like (do shell script $inputText =~ s/mm$/)
How can I do it? I don’t understand perl statement in AS


Hi Grzessnik,

That’s what I’ve been trying to say is that you don’t need Perl for simple things like this.

set inputText to "Width 10 mm"
if inputText ends with "mm" then
	display dialog "You have millimeters units"
	display dialog "You have other units"
end if


Hi Kel.
Thanks for help.
I understand it, but this is only a simple example.
I want to verify diffrent things in diffrent texts.
I want to know it also because I’m working wit text every day and thinking about simple regex script to InDesign for my needs.

Mayby I should write this to avoid misunderstanding :slight_smile:

I found do shell script with perl examples on forum, but don’t understand syntax.

set s to "33 mm"
(do shell script "perl -e 'use utf8; $_=@ARGV[0]; print m/mm$/;' " & quoted form of s) ≠ ""
--> true

Do you have to use Perl + ‘do shell script’ though? e.g. TextCommands’ ‘check’ and ‘search’ commands support PCREs and are quicker and easier to use.

set s to "33 mm"
tell application "TextCommands" to check s contains "mm$"
--> true


Hi grzessnik,

Ok, I thought you didn’t know what AppleScript can do and a lot of the times, doing a ‘do shell script’ takes longer because you have to call a shell especially for doing simple things.


Thanks guys :slight_smile:
Kel, my knowledge of AS is not big, but I found units there :slight_smile:
I need to work with InDesign, and it has different units (like pica points, points, cicero) and this is beyond AS. I’m trying to do simple script find & replace to remove white space etc. and my idea was to use Perl regex :slight_smile:

Next question. When I use TextCommand in my script, when compiling it to app, will this script addition bundle into application, or must I remember to copy it?

Hhas, can you explain this perl statement for me, please? I want to understand it :). How will it look, when I want to replace?

It think I will love AS and all thiss stuff :smiley:

TextCommands is a faceless background application, not an osax. Probably best to install it into /Library/ScriptingAdditions each time to avoid any confusion.

perl -e ‘…’ arg1 arg2 …

‘perl’ is the Perl interpreter. The -e option provides the Perl script to execute (see below). The arguments at the end will be passed to the Perl script via Perl’s special @ARGV variable. In this case there’s a single argument: the string to match.

Here’s a breakdown of the Perl script itself:

use utf8; – Tell Perl interpreter that strings are UTF8 encoded (what ‘do shell script’ uses).

$_=@ARGV[0]; – Set Perl’s special ‘default input’ variable to the first argument passed to the script, in this case the string to search. (The ‘do shell script’ command stupidly doesn’t provide a way to feed data directly to STDIN, so you have to muck about with arguments or temp files to pass data to shell scripts.)

print m/mm$/; – Check if the default input matches the regex ‘mm$’ and print the result, which is “” (false) if no match is found.

If you’re going to use Perl, I recommend getting yourself a good book or tutorial ASAP. I’d suggest O’Reilly’s ‘Learning Perl’ (the Camel book’s a bit heavy for beginners). Or consider learning Python or Ruby instead for an easier learning curve.



Thanks a lot. I’ve thought about Ruby. IIs it really easier to learn? I suppose, this statement can be used in Ruby via do shell script as well?

About this script.
Do I think right?
$_=@ARGV[0]; gets value from “quoted form of s”
then comes print m/mm$/; and checks if “mm” is at the end of s. (when I want to replace mm I should give s/mm$/“something”?)
≠“” gives false, when it’s no “mm” in s

Perl’s core language is huge; Ruby’s is smaller. Unless you’ve a talent for soaking up large amounts of information you’ll find Ruby quicker to get to grips with. (Ruby also has better application scripting support, btw.)

(do shell script "ruby -e 'print /mm$/ === ARGV[0]' " & quoted form of s) = "true"

Ruby 1.8’s Unicode support isn’t particularly good (out of the three, Python’s is best), though you can enable what there is by adding the following to the start of the Ruby script:

require "jcode"; $KCODE = "UTF8";


There are also various modifiers such as ‘g’ (global) and ‘i’ (case insensitive) that you may want to use; see the perlop and perlre manpages for more info.



I have next questions.
Can I use Ruby with Apple Script Studio?

Is a possibility to use Ruby scripts instead AS in Adobe Applications?

Not at the moment: you need a full OSA language component (plus a little judicious hacking of Studio) to write Studio-based apps. There is a basic RubyOSA component available, but it isn’t sufficiently powerful for the job. I’m planning on writing a full featured RbOSA component, but it won’t be done before the summer. (I’m currently working on PyOSA, the first release of which is due next week, and once it’s a bit more mature I’ll port it over.)

However, you don’t actually need Studio to write Cocoa apps in Ruby if you’re happy learning Cocoa proper. See RubyCocoa, which provides much more powerful Ruby-Cocoa bindings, and RubyScript2Exe for creating standalone executables.

You can use Ruby scripts to control Adobe applications, yes. e.g.:

#!/usr/bin/env ruby

require 'appscript'

PS ='Adobe Photoshop CS2')

doc = PS.make(:new => :document, :with_properties => {
        :width => 500,
        :height => 400})
layer = doc.make(:new => :art_layer, :with_properties => {
        :name => 'hello layer',
        :kind => :text_layer})
text = layer.text_object
text.contents.set("Hello World!")

To run scripts from inside the applications they control (e.g. triggered from script menus) you usually need a full OSA language. You could try using rb-appscript with RubyOSA which may or may not work; if it doesn’t, you’ll need to run your scripts from Terminal/OS X’s global Script Menu/another application until RbOSA is done.



p.s. When writing OSA scripts in Tiger’s Script Editor, be aware that there’s a bug in SE which prevents it opening .scpt files unless you set the default language in SE’s preferences to the same language as the .scpt file first. (No problems in Script Debugger and Panther’s Script Editor though, and the bug is presumably fixed in Leopard’s Script Editor.) Or you can avoid SE completely and just use ‘osacompile’ to create .scpt files from .rb files as needed.

I’ve visited RubyCocoa site.
If I understood it because my English is terrible, using RubyCocoa framework I can connect Ruby with Interface Builder dialog. If it’s true, it is a good message for me, cause I need Interface Builder for customizing dialog for my simple app.
Do I understand it rightly? Can I connect it?

Yes. It’s essentially the same process as for writing ObjC-based Cocoa apps; the only difference is you’re writing code in Ruby instead of ObjC.



p.s. Best place to ask specific RubyCocoa questions would be the rubycocoa-talk mailing list. For general Ruby questions, the comp.lang.ruby newsgroup provides a convenient gateway to the ruby-talk mailing list.

HTH “ I’ve installed appscript-rb on my mac, but script that you put there doesn’t work.

It doesn’t recognize require ‘appscript’

What can be wrong?