help with apple script - text edit editing and more

hey, i’m a scripter and new to this website (i’m not that good) and well,

i wanted a script that would
a - open a text edit file (dosent matter what type)
b - change every letter to something different e.g. a to (1) or b to (12)
c - and save
(edit) also, i dont want to encode it, just replace it. like A for a. i’ll work out a code by myself.

if u could help, than thanks.

thganks again

does this help?


tell application "TextEdit"
	-- open file
	set theAlias to choose file
	set theDoc to open theAlias
	
	-- get text contents
	set theText to text of theDoc
	
	-- your conversion code here
	-- e.g. begin
	set newText to "" as Unicode text
	try
		considering case
			repeat with i from 1 to number of characters in theText
				set thisChar to character i of theText
				
				-- convert by character
				set newChar to thisChar & "A" --> your code <--
				
				set newText to newText & newChar as Unicode text
			end repeat
		end considering
	on error eMsg number eNum
		display dialog "Error converting text (" & eNum & ")" buttons {"OK"} default button 1
	end try
	-- e.g. end
	
	-- replace text
	try
		set text of theDoc to newText
	on error eMsg number eNum
		display dialog "Error setting text (" & eNum & ")" buttons {"OK"} default button 1
	end try
	
	-- close and save file
	try
		--save theDoc in theAlias
		close theDoc saving yes saving in theAlias
	on error eMsg number eNum
		display dialog "Error saving (" & eNum & ")" buttons {"OK"} default button 1
	end try
	
	-- reopen
	open theAlias
end tell

lol, a bit complicated…
i dont want to have a code in there, i want it to replace. so, for geample, a:1 b:2
like that, only i can change the numbers 1 and 2 to what ever i like, e.g. binary
even if you could give me a code that would do it, that would be great ( for the script)
thanks for your help,

I thought you said you were a scripter? No script without code.

you’re welcome.
You can show your gratitude by posting your progress and results :slight_smile:

well, i am a scripter, but not that good. i havent done the whole code thing. i can do simpel stuff, but i dont no how to do the conversion code…
i had a code that was using textedit’s find and replace, but it dident work. it was mainly keystokes.
anyway, i g2g. my sis is yelling at me to come :frowning:
but i might check in later
later.

This forum is a good place to learn.

So how far do you understand the script we have so far?

Here’s a hint for your next step:
you need 2 lists. One containing the characters you want to replace. The second with the values you want to replace them with.

i get what this dose
“tell application “TextEdit”
– open file
set theAlias to choose file
set theDoc to open theAlias”

and the bottom line of this
– get text contents
set theText to text of theDoc

i have some idea what this dose,
" – your conversion code here
– e.g. begin
set newText to “” as Unicode text
try
considering case
repeat with i from 1 to number of characters in theText
set thisChar to character i of theText"
is this where i put my things to replace? if so, how?
" – convert by character
set newChar to thisChar & “A” → your code <–"

this bit i get (mostly)

           set newText to newText & newChar as Unicode text
       end repeat
   end considering

on error eMsg number eNum
display dialog “Error converting text (” & eNum & “)” buttons {“OK”} default button 1
end try

i get this
try
set text of theDoc to newText
on error eMsg number eNum
display dialog “Error setting text (” & eNum & “)” buttons {“OK”} default button 1
end try

and i get most of this
try
–save theDoc in theAlias
close theDoc saving yes saving in theAlias
on error eMsg number eNum
display dialog “Error saving (” & eNum & “)” buttons {“OK”} default button 1
end try

– reopen
open theAlias
end tell

so, i have a list of what i want to do, but or this ill just go from lower case to capitals

also, in an unrelated event, i had a script that typed a sentince constantly over and over. i had to shut the computer down
lol.

try command+option+esc. You should be able to force quit such an app.

The “considering case” block ensures that applescript sees “a” as something different to “A”

So your lists should look something like this:


set theSearchList to {"a", "b", "A", "B"}
set theReplaceList to {"(1)",  "(2)", "(27)", "(28)"}

according to your first post:

you are figuring out this step yourself and posting it here, let us see it :slight_smile:

Here’s how I went about it:
To find and replace, I searched on this forum for something like “Find replace text” to find: http://bbs.applescript.net/viewtopic.php?id=18551
In that discussion, Bruce Philips shared his ‘clone’ of the PHP str_replace function.
Also further down the discussion explains the use of the “considering case” block.


set theSearchList to {"a", "b", "A", "B"}
set theReplaceList to {"(1)", "(2)", "(27)", "(28)"}

tell application "TextEdit"
	-- open file
	set theAlias to choose file
	set theDoc to open theAlias
	
	-- get text contents
	set theText to text of theDoc
	
	-- your conversion code here
	-- use the case sensitive version of Bruce Philip's str_replace() which accepts lists as arguments
	tell me to set newText to str_ireplace(theSearchList, theReplaceList, theText)
	-- or: set newText to my str_ireplace(theSearchList, theReplaceList, theText)
	
	-- replace text
	try
		set text of theDoc to newText
	on error eMsg number eNum
		display dialog "Error setting text (" & eNum & ")" buttons {"OK"} default button 1
	end try
	
	-- close and save file
	try
		--save theDoc in theAlias
		close theDoc saving yes saving in theAlias
	on error eMsg number eNum
		display dialog "Error saving (" & eNum & ")" buttons {"OK"} default button 1
	end try
	
	-- reopen
	open theAlias
end tell

(*
clone of PHP's str_ireplace function
original posted by Bruce Philips
http://bbs.applescript.net/profile.php?id=5342
on bbs.applescript.net
http://bbs.applescript.net/viewtopic.php?id=18551
note: the above mentioned version doesn't consider case, that's the only
modification by SwissalpS. The original would be str_replace.scpt
*)
on str_ireplace(search, replace, subject)
	considering case
		local search, replace, subject, ASTID, returnList, searchCount, thisSubject, i, n
		
		set ASTID to AppleScript's text item delimiters
		set returnList to true
		
		-- This wouldn't make sense
		if search's class is not list and replace's class is list then return subject
		
		-- Make one-item lists if needed	
		if search's class is not list then set search to {search}
		if subject's class is not list then set {subject, returnList} to {{subject}, false}
		
		set searchCount to count search
		get count subject
		
		try
			repeat with i from 1 to result
				set thisSubject to subject's item i
				
				repeat with n from 1 to searchCount
					set AppleScript's text item delimiters to search's item n
					set thisSubject to thisSubject's text items
					
					if replace's class is list then
						try
							get replace's item n
						on error
							get "" -- `replace` ran out of items
						end try
					else
						get replace
					end if
					
					set AppleScript's text item delimiters to {result}
					set thisSubject to "" & thisSubject
				end repeat
				
				set subject's item i to thisSubject
			end repeat
		end try
		
		set AppleScript's text item delimiters to ASTID
		if not returnList then set subject to subject's first item
		return subject
	end considering
end str_ireplace

If it’s just the case you want to change, then I’d do something like this:


property sLower : "abcdefghijklmnopqrstuvwxyz"
property sUpper : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

tell application "TextEdit"
	-- open file
	set theAlias to choose file
	set theDoc to open theAlias
	
	
	-- get text contents
	set theText to text of theDoc
	
	-- your conversion code here
	-- e.g. begin
	set newText to "" as Unicode text
	try
		considering case
			repeat with i from 1 to number of characters in theText
				set thisChar to character i of theText
				
				-- convert character by character to lower case
				if thisChar is in sUpper then
					set newChar to sLower's character (offset of thisChar in sUpper)
				else
					-- if the character isn't in our upper case string we don't touch it
					set newChar to thisChar
				end if
				
				(*
				-- convert character by character to upper case
				if thisChar is in sLower then
					set newChar to sUpper's character (offset of thisChar in sLower)
				else
					-- if the character isn't in our lower case string we don't touch it
					set newChar to thisChar
				end if
				*)
				
				set newText to newText & newChar as Unicode text
			end repeat
		end considering
	on error eMsg number eNum
		display dialog "Error converting text: " & eMsg & " (" & eNum & ")" buttons {"OK"} default button 1
	end try
	-- e.g. end
	
	-- replace text
	try
		set text of theDoc to newText
	on error eMsg number eNum
		display dialog "Error setting text: " & eMsg & " (" & eNum & ")" buttons {"OK"} default button 1
	end try
	
	-- close and save file
	try
		--save theDoc in theAlias
		close theDoc saving yes saving in theAlias
	on error eMsg number eNum
		display dialog "Error saving: " & eMsg & " (" & eNum & ")" buttons {"OK"} default button 1
	end try
	
	-- reopen
	open theAlias
end tell

I guess you know that AppleScript isn’t the best choice for string conversions.
If you have long text, you’ll be happier using a faster language or shell script.

sorry i have not been on, my internet was down :frowning:
but i did write this script


repeat 50 times
	
	set numtotype to (random number from 1 to 2)
	tell application "System Events"
		set binary1 to "0"
		set binary2 to "1"
		activate application "TextEdit"
		if numtotype is 1 then
			keystroke binary1
			
		else if numtotype is 2 then
			keystroke binary2
			
		end if
	end tell
	
end repeat

basicly, it types 50 charicters of random binary. if you want to crash a computer (sort of) then set the 50 to something high. you canot do anything while it types.