do a perl command from applescript

I’d like to use Perl from AppleScript for text replacements in an html string. The snippet below works, however when the var haystackString starts to include apostrophes, this fails. I tried using AppleScript’s “quoted form of” on haystackString however it fails I think because the shell command I’m using needs single quotes to enclose the commands being executed by Perl. Any tips? Thanks, Ted

set haystackString to "this this"
  set findString to "this"
  set replaceString to "that"
 
set shellscript to ("perl -e '$s=\"" & haystackString & "\";$toFind=\"" & findString & "\";$replaceWith =\"" & replaceString & "\";$s =~ s/$toFind/$replaceWith/g;print $s;'") as string
 
  set testString to do shell script shellscript

Hi Ted,

You have single quotes around the whole command, so when you enter another single quote inside the single quotes, then quoting is turned off. In your search string, you need to literally backslash the single quotes. AppleScript uses backslash also, so you need to backslash the backslash.

set haystackString to "this isn'\\''t this"
set findString to "isn'\\''t"
set replaceString to "wasn'\\''t"

set shellscript to ("perl -e '$s=\"" & haystackString & "\";$toFind=\"" & findString & "\";$replaceWith =\"" & replaceString & "\";$s =~ s/$toFind/$replaceWith/g;print $s;'") as string

set testString to do shell script shellscript

gl,
kel

One other thing that might work is using smart quotes within the strings instead of straight quotes. You can check that out.

Edited: it does work with smart quotes:

set haystackString to "this isn˜t this"
set findString to "isn˜t"
set replaceString to "wasn˜t"

set shellscript to ("perl -e '$s=\"" & haystackString & "\";$toFind=\"" & findString & "\";$replaceWith =\"" & replaceString & "\";$s =~ s/$toFind/$replaceWith/g;print $s;'") as string

set testString to do shell script shellscript

gl,
kel

Never try to quote yourself, let quoted form handle it for you, especially with arbitrary data.

set haystackString to "this it's that"
set findString to "it's"
set replaceString to "not"

set perlScript to "$s=\"" & haystackString & "\";$toFind=\"" & findString & "\";$replaceWith =\"" & replaceString & "\";$s =~ s/$toFind/$replaceWith/g;print $s;"

set shellscript to "perl -e " & quoted form of perlScript

set testString to do shell script shellscript

edit: example with quotes

Hi DJ,

This is one of the few times I disagree. Sometimes, I like to use AppleScript Editor to learn shell scripting. Quoting is one of the major part of shell scripting that is hard to understand and want to to it by yourself instead of looking at examples.

Other than that, if you just want the script to work, then quoted form is the way to go.

Edited: btw, on the side. Quoted form gives the same thing:

set t to "this isn't that"
set qf to quoted form of t

Have a nice day,
kel

Hi kel and DJ,

A nice guy named Jacques Rioux was kind enough to provide a cool way of using AppleScript and Perl together, and his technique is very similar to the techniques that you both provided.

AppleScript has a special function “quoted form of” that is used to prepare/escape arbitrary string values to pass as command line arguments. Perl has a special variable named $ARGV that holds an array of any arguments passed via the command line. Using these in conjunction is a nice solution:

set haystackString to "can't  <p role=\"heading\">can't</p>"
set findString to "can't"
set replaceString to "that's all"

set args to (quoted form of haystackString) & " " & (quoted form of findString) & " " & quoted form of replaceString
set testString to do shell script "perl -e '$s=\"$ARGV[0]\";$s =~ s/$ARGV[1]/$ARGV[2]/g; print \"$s\"' " & args

Thanks,

Ted

It’s a free world!

I can write a long story about how important programmer’s discipline is. But the fact that the code doesn’t work and quoted form of just works in any case only proofs my point. Your first example needs an half baked quotation which is bad practice in the first place, and why mimic an existing function that is provided and matches with the system? Your second example will have substitution mode back on, something your definitely don’t want using arbitrary data making the code work with single quotes but fails when the text contains special characters like dollar signs.

Anyway, like I said both of your examples showed that using quoted form of the best way to do it, so I think I need no further explanation.

For the record: Learning shell with AppleScript is just bad practice in general, it’s like learning AppleScript using VBA.

Anyway you still don’t have to agree and I don’t expect you to, but at least you know my point of view.

Hello.

I have written a script that should quote most do shell command lines correctly for use with Apple Script once they have been copied to the clipboard.

The script can be found in post 8 in this thread.

1.) You have the perl script quoted, like it should be for starters.
2.) You copy the text you want to have escped correctly to the clipboard, then you run the text, choose copy to clipboard, if you want to use the result as or in a do shell script command in an Applescript.

Hi DJ,

One thing I find hard about using Terminal is that you can’t move the cursor. With Script Editor you can just click to where you want the insertion point to be. That’s why I use ‘do shell script’ to test shell scripts. I could never find an easy way to move to insertion point in Terminal. It takes to long with the arrow keys.

Maybe I just don’t know how to move the cursor. I remember some advice about how to move the cursor, but can never remember them or maybe it is just limited.

Anyway, you’re right. To each his own.

Edited: p.s. I agree with your arbitrary data argument. What I was thinking was that the op might have wanted to learn Perl through the AppleScript Editor as I have often done with Python and Sed.

Good day,
kel

Maybe this refresh your memory :slight_smile:

Tab Auto-complete files and folder names
Ctrl + A Go to the beginning of the line you are currently typing on
Ctrl + E Go to the end of the line you are currently typing on
Ctrl + U Clear the line before the cursor
Ctrl + K Clear the line after the cursor
Ctrl + W Delete the word before the cursor
Ctrl + T Swap the last two characters before the cursor
Esc + T Swap the last two words before the cursor (first press escape and release then press t)
Ctrl + R Lets you search through previously used commands
CMD + K Clears the Screen
Ctrl + C Kill whatever you are running
Ctrl + D Exit the current shell

Also you can click in the terminal and move the cursor with your mouse but you have to hold down the option key.

update: forgot a very important one. What you remove with ctrl + k you can paste later with ctrl + y. This won’t influence Mac OS X’s clipboard. When possible I prefer this so I have two clipboards i can work with.

Hello kel.

The keyboard layout is highly configurable in bash, you’ll have to read a bit though, but I think basically, that the keyboard should behave as in Text Edit, or applescript editor for that matter. There are good instructions in the bash manual (man bash), should you need to configure it a little.

ctl-A moves to beginning of line, ctl-E to end, ctl-F move forward, ctl-B move backward.
ctl-k kills a line from the curser position and so on.

However, I always use the mouse for selecting text. :wink:

It pays off to experiment a bit,

Yeah, I know. You often want to move to around the middle and it takes forever for the cursor to move. Apple should make Terminal insertion point friendly. :slight_smile:

Edited: but that’s not the nature of the beast.

I’m not sure if it is standard, but Esc-f and Esc-b moves the cursor a word to the left and right respectively.
I haven’t done anything in order to achieve this, explicitly, but I have configured the cocoa text system, to behave conformant, so maybe it is those settings that kick in. You can however specify settings in .inputrc, which you can read about in the bash manual.

Os X understands many emacs short cut keys, I think it was fruitful for me to figure out which of those worked, and which I could activate. :slight_smile:

They do: option-click is your friend.

:slight_smile:

I DID NOT KNOW ABOUT THE OPTION KEY!!! :smiley: Why didn’t you guys tell me that earlier. :smiley:

Darn, I’ve been waiting forever for the arrow key to move the cursor. My fault. Should have asked earlier. :slight_smile:

Hi McUsr,
I don’t know why, but I just can’t remember the keystrokes. Getting old I guess.

Now I’m happier

Thanks a lot!!!
kel

One thing strange happens though. When I press the up arrow key and it goes through all the lines that you’ve entered. On this line:

and wherever I Option + click the alert sounds (duck starts quacking) and I think the program runs. Need to test it out more.

Edited: Option + click seems to work on lines without double quotes.

Edited: think I know why the alert sounded. The command had errors maybe.

Edited: think I’ve got it. If the cursor turns into crosshairs, then the command had error(s). Otherwise, It remains an i beam. Need to check that out.

Edited: no it wasn’t the i beam thing. It must be that the whole command erorred.

Edited: interesting. I think you can find what line errored using Option + click.

Edited: think I know why it beeped. It has to start with a unix command. The one I was using was totally AppleScript Scripting Addition. Still Testing.

Interesting stuff!