Gilles Carpentier's Research Web Site:
Computer Image Analysis
Methods and Examples

Automatizated tasks around the ImageJ §1 software using the AppleScript technology §2 with Mac OS X
ImageJ and AppleScript Main Page
 Character Tools - Scripting Text Edit. "AppleScript Code Modifier"
(Previous Page)

-- AppleScript Code Modifier

-- Code author, Gilles Carpentier, Faculte des Sciences et Technologies, 

-- Universite Paris 12 Val-de-Marne, France.


-- Modify the droped file, protecting characters ("). 

-- Save the result in a new file under the name "name-protected.txt" with TextEdit

property compteur : 0

-- Routine - replace (x) by (\x) in a string, where (x) is a character.

on changesometexte(texttochange, aeliminer, remplacant)

set initialtext to texttochange

set compteur to 0

if texttochange contains aeliminer then

try

set longaeliminer to the number of characters of aeliminer

set newmacrotext to ""

set long to the number of characters of texttochange

repeat while texttochange contains aeliminer

set posaeliminer to the offset of aeliminer in the texttochange

if posaeliminer > 1 then

set decoupe to (characters 1 thru (posaeliminer - 1) of texttochange) as string

else

set decoupe to ""

end if

set newmacrotext to newmacrotext & decoupe & remplacant as string

set compteur to (compteur + 1)

if posaeliminer = long then

set texttochange to ""

exit repeat

end if

set texttochange to (characters (posaeliminer + longaeliminer) thru long of texttochange) as string

set long to (long - posaeliminer - (longaeliminer - 1))

end repeat

if texttochange ≠ "" then

set newmacrotext to newmacrotext & texttochange

end if

set texttochange to newmacrotext

end try

else

set texttochange to initialtext

end if

end changesometexte

-- End of the routine.

on open truc

try

tell application "TextEdit"

activate

end tell

on error

display dialog "The script doesn't find TextEdit" buttons {"Cancel"} default button 1

return

end try

tell application "Finder"

activate

set chemin to (truc as string)

get the name of the item chemin as string

set nomdefichier to result

if chemin contains nomdefichier then

set bug to the offset of nomdefichier in chemin

end if

set newfile to (characters 1 thru (bug - 1) of chemin) as string

tell application "TextEdit"

activate

open chemin

get the name of front window

set nomdefenetre to result as text

set contenudemacro to text of the front document

end tell

-- To obtain the name of the window.

set nomdelafenetre to ""

set tocut to "—"

if nomdefenetre contains tocut then

set cutend to the offset of tocut in the nomdefenetre

set nomdefenetre to (characters 1 thru (cutend - 3) of nomdefenetre) as string

end if

set tocut to ".txt"

if nomdefenetre contains tocut then

set cutend to the offset of tocut in the nomdefenetre

set nomdefenetre to (characters 1 thru (cutend - 1) of nomdefenetre) as string

end if

-- Text treatment.

try

if contenudemacro contains (ASCII character 34) then

set protected to my changesometexte(contenudemacro, (ASCII character 34), ((ASCII character 92) & (ASCII character 34)))

set compteur1 to compteur

end if

end try

end tell

-- Set a new name to the window containing the modified text.

tell application "TextEdit"

set text of the front document to protected

set newname to nomdefenetre & "-protected.txt"

set name of the front window to newname

set newfile to newfile & newname

save the front document in newfile as text

--close the front window

tell application "Finder"

activate

display dialog (compteur1 & " (" & (ASCII character 34) & ") protected in this text.") as string

end tell

end tell

end open


That's all!

To try the script, copy the code of this page with Safari as browser, an paste it in the AppleScript Editor

(ImageJ and AppleScript Main Page)

References:
ijlogo§1 ImageJ (http://rsb.info.nih.gov/ij/) is a public domain Java image processing program inspired by NIH Image for the Macintosh. It runs, either as an online applet or as a downloadable application, on any computer with a Java 1.1 or later virtual machine. Downloadable distributions are available for Windows, Mac OS, Mac OS X and Linux. The author, Wayne Rasband (wayne@codon.nih.gov), is at the Research Services Branch, National Institute of Mental Health, Bethesda, Maryland, USA.

aslogo§2 Apple Script is a technology developped by Apple since several years to automatize finder repetitive tasks and to pilote scriptable applications. It is available on MacOS X, works for Carbon and Cocoa applications and it is installed by default. An application, to be scriptable need to be customized by adding a dictionary of functions which allow Apple Script to send and receive Apple events. Apple developes also methods for developer to make scriptable java applications.


Page author: Gilles Carpentier. You can mail me if you are interested in more details:
Address: Gilles Carpentier, Faculté des Sciences et Technologies, Université Paris 12 Val-de-Marne, France.
Gilles Carpentier's Web Site: "Computer Data Acquisition for Biochemistry Practice Works: Methods and Examples"

Locations of visitors to this page