Character Search/Sort

Hi Guys,

I don’t know if this is possible with AppleScript but I would really appreciate if someone could help me out with this, I need to be an applescript that takes in input through the textbox and checks if there are specific characters like @ in the text. If the input has those characters, they need to be replaced with ‘%40’. I’m making a script that uses URI and I needed help with this portion.

Thanks in advance


set textAfter to my remplace(textBefore, "@", "%40")

--=====
(*
replaces every occurences of d1 by d2 in the text t
*)
on remplace(t, d1, d2)
	local oTIDs, l
	set oTIDs to AppleScript's text item delimiters
	set AppleScript's text item delimiters to d1
	set l to text items of t
	set AppleScript's text item delimiters to d2
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end remplace

--=====

may do the job.

Yvan KOENIG (VALLAURIS, France) mercredi 16 janvier 2011 23:09:30

Thanks,

That works perfectly, can this work and look for other values in the same string of text and replace them with the respective values

like
@ → %40
$ → %24
& ->%26
% → %25

Sorry, I’m kind of new the apple scripting.

Thanks for your help, can’t say that enough

Hi,

try this


set theString to "@"
set escapedString to do shell script "perl -e 'use URI::Escape; print uri_escape(\"" & theString & "\")';"

Hello Stephan

I tested your answer which is really powerful.
I just wish to point two details :

(1) never apply it upon a piece of text which was encoded once.
(2) I found a surprising (for me) special case.
Run this script :


set theString to "replace possible existing %25 by an unused string @robas &léphant 125$US dollar US"

set escapedString to do shell script "perl -e 'use URI::Escape; print uri_escape(\"" & theString & "\")';"

The result is :

“replace%20possible%20existing%20%2525%20by%20an%20unused%20string%20%20%26l%C3%A9phant%20125%20dollar%20US”

The re-escaped % character (was %25 in the source) is logical
but I don’t understand why the $US string (after 125) is dropped.

Yvan KOENIG (VALLAURIS, France) jeudi 17 janvier 2011 11:05:46

That’s not all that was dropped, by the look of it. What happened to “robas”?

Hi mac_life,

When using shell commands you need to excape characters. The dollar sign has a special meaning in the shell, it thinks it is a variable. So for every dollar sign you need to put an escape character in front of it (when using do shell script). A little example: $HOME is a variable in the shell to your home folder. When you try to echo in PHP for example the home folder as described below it will return the contents of the variable.

do shell script "php -r \"echo '$HOME';\""

The result is the path to your home folder. What happened was that while the command (the string of do shell script command) was interpreted by the shell’s interpreter, $HOME has been replaced by it’s value before it has been send to PHP.

so for PHP the script would look like this (PHP doesn’t have problems with @ signs)

set theString to "replace possible existing %25 by an unused string @robas &léphant 125\\$US dollar US"
set escapedString to do shell script "php -r \"echo rawurlencode('" & theString & "');\""

The reason why @ doesn’t work is that it has a special meaning in PERL. So if you want to use perl you have to escape @ signs as well.

Thanks Guys

This is exactly what I was looking for, I really appreciate the help… :slight_smile:

Here is a code doing the complete job :



set theString to "replace possible existing %25 by an unused string @robas &léphant 125$ dollar US"

set theString to my remplace(theString, "@", "\\@")
set theString to my remplace(theString, "$", "\\$")

set escapedString to do shell script "perl -e 'use URI::Escape; print uri_escape(\"" & theString & "\")';"

--=====
(*
replaces every occurences of d1 by d2 in the text t
*)
on remplace(t, d1, d2)
	local oTIDs, l
	set oTIDs to AppleScript's text item delimiters
	set AppleScript's text item delimiters to d1
	set l to text items of t
	set AppleScript's text item delimiters to d2
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end remplace

--=====

As you see, it use my original handler to escape the special characters $ and @
then it call the powerful Perl code.

I wish to say thank you to Shane because I missed the fact that “robas” was dropped too
and to DJ Bazzie Wazzie who gave the explanation (and the way to get rid) of what was for me an odd behavior.

Yvan KOENIG (VALLAURIS, France) jeudi 17 janvier 2011 14:24:02

Or escape it with quoted form of command and use it with PHP in a single line.

set theString to "replace possible existing %25 by an unused string @robas &léphant 125$ dollar US"
do shell script "printf \"<?php echo rawurlencode('%s');?>\" " & quoted form of theString & " | php"

Thanks again.

I’m perfectly ignorant about Perl and PHP languages so I was unable to build such efficient command.

Yvan KOENIG (VALLAURIS, France) jeudi 17 novembre 2011 19:10:23