Hello, Need some help in Applescript (Reverse String)

Hello,

I’m new here and didn’t tuch applescript about 3 years…
this why i need your help folks.

i want to write some script that reverse string that writen… i have to idea of the structure script :

  1. write some text inside TextEdit program and drag in on the AppleScript … Reverse the String In it… and save it reversed…
    or…
  2. when running the AppleScript… dialog is open and ask me to input a text…
    then on click the text will be reversed.

anyway i think the first one is better for more complicated text with line brake and so…

some one can please help me with this one., I’m sure Its a short writing and it will be a lot for me!

Thanks a lot!

What kind of text file? RTF? Plain text?
This is a quick routine to reverse a string if it is not so long:

set theText to "abcd"
set theText to reverse of theText's items as text --> "dcba"

And this one will grab user input (from the Standard Additions’ dictionary):

set enteredText to text returned of ¬
	(display dialog "Enter text or die" default answer "text")

RTF format would be great but Simple-text too
I just need a script that reverse text inside a file
whatever is writing inside it not matter

Its actually for hebrew in flash
in flash i cant write good so i need to right in reverse and its hard…
this why i want a script that reverse it and ill paste the text inside flash =)

Thanks!

Hmmm… Then you maybe better interested in writing a JSFL command (accessible from your “Commands” menu inside Flash 7, and much quicker!):

Ôªø
d=fl.getDocumentDOM();
if (! d) {
	alert('Please, open a document before running this command!');
} else {
	doIt();
}

function doIt(){
	s = d.selection;
	if (s.length == 0){
		if (d.getTextString() == undefined) {
			alert('Select some text before running this command!');
		} else {
			FC();
		}
		return;
	}
	for (i=0;i<s.length;i++){
		d.selectNone();
		d.selection = [s[i]];
		rev();
	}

	d.selection = s;
}

function rev(){
	x = d.getTextString().split('').reverse().join('');
	d.setTextString(x);
}

Make a new JSFL script window in Flash (“New…” menu), copy-paste this code, save in the dir “~/Application Support/Macromedia/Flash MX 2004/en/Configuration/Commands”. Write your text in the stage. Select the text field. Choose your new jsfl script in the “Commands” menu, and you are done.
If you own, though, Flash 6, perhaps you can write your text in TextEdit, then run something as:

tell application "TextEdit" to set x to text of document 1
set the clipboard to (reverse of (x's text items) as text)

And this will place your reversed text in the clipboard, ready to paste in Flash. :rolleyes:

  • NOT SURE, though, if these methods will support Unicode text…

but i wanted to ask you about the Applescript…
why the text that copied to my clipboard paste with wrong Encoding…
and i can’t see the text right?

how can i set the encoding of the text and then copy it to my clipboard?

i guess i need UTF-8 that Unicode and come for flash new Write engine…
and for TextEdit too =) in hebrew language.

thanks!

This was my “NOT SURE”. This is out of my scope, but one of these rough tricks could work if you use only two-byte Unicode:

tell application "TextEdit"
	set x to text of document 1
end tell

set rev to "" as Unicode text
repeat with i from 1 to count x
	set rev to rev & text item -i of x
end repeat

set the clipboard to rev
tell application "TextEdit"
	set x to text of document 1
end tell

set x to Unicode2Raw(x)

set raw to {}
repeat with i from 1 to count x by 4
	set raw's end to text -i thru -(i + 3) of x
end repeat

run script "set the clipboard to «data utxt" & raw & "»"

to Unicode2Raw(x)
	set x to x as Unicode text
	set ind to -((x's length) * 4) - 18
	{{kaka:x}}
	result as string as C string
	try
		result * 5
	on error msg
		return text ind thru -19 of msg
	end try
end Unicode2Raw

Hi man Thats Great,
I thought it will be easy for experience guy like you =)

anyway i want this script to run when I’ll drop TextEdit file on it…
and then open the file i dropped in TextEdit,
And the Script will convert the text to my Clipboard!

Oh and about the Flash JS its running great,
I just want you to make little modification…
when I select a block of that It reverse the order of the lines =)
Make the first line last and so on…

can you set it right? or Its to much complicated?!
oh and if i select all the text inside the text block… i get some error about something inside the FLJS line 1, So i need to select to block him self to prevent it

Thanks a lot man!

JSFL, very easy. Substitute the function “rev” with:

function rev(){ 
   x = d.getTextString().split('').reverse().join(''); 
   x = x.split('r').reverse().join('r'); // same mechanism ;-)
   d.setTextString(x); 
}

AppleScript code, simply add a “open” handler:

on open listOfFiles
	
	repeat with i in listOfFiles
		tell application "TextEdit"
			open i
			set x to text of document 1
			close document 1
		end tell
		
		set rev to "" as Unicode text
		repeat with i from 1 to count x
			set rev to rev & text item -i of x
		end repeat
		
		set the clipboard to rev
		
		
		beep 2
	end repeat
	
end open

Thanks A lot