gc icon Gilles Carpentier Research Web Site: Computer Image Analysis - Methods and Examples. ima
---
Automatizated tasks around the ImageJ §1 software using the AppleScript technology §2 with Mac OS X
ImageJ and AppleScript Main Page
IJStartUpd&d - MacOSX3 and Later  - commented code.
(Previous
Page)


The following comments concern only the steps particular to this method, using StartupMacros file. For more details about non commented parts, see the page describing the method using MacroSet file [1] at this link.

Check if the object you drop is a file or a folder. Quit if it is a folder. The script works only on single 8 bit greyscale images.

asij

Find location informations of the ImageJ application. Exit ImageJ if already started: macros of the StartupMacros.txt file are auto charged only when ImageJ starts up.

Check the presence of a StartupMacros.txt file into the macros folder: if a so file exists, its contents has to be removed, archived and replaced by the one created by the Apple Script application containing the macro(s) we want to automate.

If the file is not found, create a file containing the macro code to automate, with the StartupMacros.txt name.

If StartupMacros.txt file found, archive its code contents.

Write the macro code to automate into the StartupMacros.txt file. This file will be read by ImageJ application at starting.

asij

Activation of ImageJ by opening its item. Auto install of macros.

Open the dropped image file by ImageJ

uiij

Apply the macros to the image file using the menu shortcuts

as

Restore the initial StartupMacros.txt file content if existing

Quit the AppleScript application


AppleScript code:

-- IJStartUpd&d - MacOSX3 and Later:

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

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


-- put the macro content into the variable "macros"

-- see LookupTables.txt in the macro folder of IJ for the full original code

property macros : "macro 'Green LUT [Y]' {

    reds = newArray(256); 

    greens = newArray(256); 

    blues = newArray(256);

    for (i=0; i<256; i++)

greens[i] = i;

setLut(reds, greens, blues);

}

macro 'LUT Animation [y]' {

    getLut(r1, g1, b1);

    getLut(r2, g2, b2);

    offset = 0;

    while (true) {

        for (i=0; i<256; i++) {

            k = (i + offset)%256;

            r2[i] = r1[ k];

            g2[i] = g1[k];

            b2[i] = b1[k];

        }

        setLut(r2, g2, b2);

        offset += 2;

        getCursorLoc(x, y, z, flags);

        if (flags!=0) {

            setLut(r1, g1, b1);

            exit();

         }

    }

}

"

-- variable returned by the routine "findAppFolder"

property pathfolderApp : ""


-- routine "findAppFolder" to find the path of the folder containing an application (whose name is unaccessible) from its full path:

-- remove the extention (.app) and supress the file separator ":"  in a string, containing the full path of the application


on findAppFolder(fullPath, pathsignal, extensionToRemove)

set pathfolderApp to ""

set long to the number of characters of the fullPath

if fullPath contains extensionToRemove then

set cutend to the offset of extensionToRemove in the fullPath

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

end if

if fullPath contains pathsignal then

try

set long to the number of characters of the fullPath

repeat while the fullPath contains pathsignal

set pospathsignal to the offset of pathsignal in the fullPath

set decoupe to (characters 1 thru pospathsignal of fullPath) as string

set pathfolderApp to pathfolderApp & decoupe

set fullPath to (characters (pospathsignal + 1) thru long of fullPath) as string

set long to (long - pospathsignal)

end repeat

end try

end if

return pathfolderApp

end findAppFolder

-- end of the routine


-- drag and drop event

--"truc" : must be Tiff, 8 bit grayscale for this basic example.

on open truc

tell application "Finder"

-- test of version MacOSX (10.3.9 ou >). Apple Code

set the hexData to system attribute "sysv"

set hexString to {}

repeat 4 times

set hexString to ((hexData mod 16) as string) & hexString

set hexData to hexData div 16

end repeat

set the OSversion to the hexString as string

if the OSversion is less than "1039" then

tell application "Finder"

activate

display dialog "This script requires the installation of " & ¬

"Mac OS X 10.3.9 or higher." buttons {"Cancel"} ¬

default button 1 with icon 2

end tell

end if

-- test for the UI activation (Apple code)

tell application "System Events"

if UI elements enabled is false then

tell application "System Preferences"

activate

set current pane to pane "com.apple.preference.universalaccess"

tell application "Finder"

activate

display dialog "UI element scripting is not enabled. Check \"Enable access for assistive devices\". Start the Script again once UI is activated." buttons {"OK"} default button 1 with icon 2

return

end tell

end tell

end if

end tell

-- get the path of the dropped file image

set chemin to (truc as string) -- put the path of image file into the variable "chemin"

-- test for file or folder properties of the dropped item

if last item of chemin is ":" then

tell application "Finder"

activate

display dialog ¬

"This is a folder. This script works only with a single droped file" buttons {"Cancel"} default button 1 with icon 2

return

end tell

end if

-- find the path of the ImageJ front application and of the folder containing it

try

tell application "ImageJ"

activate

set originalIJ to ""

repeat while originalIJ = ""

set originalIJ to (path to the frontmost application as Unicode text) as string -- path of the ImageJ application

end repeat

-- quit ImageJ application if running

try

tell application "ImageJ"

quit

end tell

end try

end tell

on error

tell application "Finder"

activate

display dialog "The system can't activate the imageJ Application. Pb system" buttons {"cancel"} default button 1 with icon 2

return

end tell

end try

-- find the path of the folder containing the ImageJ application

try

set pathfolderijapp to my findAppFolder(originalIJ, ":", ".app")

on error

tell application "Finder"

activate

display dialog "The system can't find the ImageJ Application. Pb system" buttons {"cancel"} default button 1 with icon 2

return

end tell

end try

set cheminmacrofolder to pathfolderijapp & "macros:"

set cheminmacro to cheminmacrofolder & "StartupMacros.txt"

-- check for the presence of the macros folder.

try

get folder cheminmacrofolder

on error

tell application "Finder"

activate

display dialog "No macros folder found in the ImageJ application folder: " & pathfolderijapp buttons {"Cancel"} default button 1 with icon 2

end tell

return

end try

-- test for the presence of a StartupMacros.txt item into the macros folder of the ImageJ application's folder 

set test to ""

try

get item cheminmacro

set test to result as string

end try

-- StartupMacros file's code management:

-- if a StartupMacros doesn't exist, create a so file containing the code to use:

if test = "" then

try

set macrofile to open for access (cheminmacro) with write permission

write macros to the macrofile

close access macrofile

on error

tell application "Finder"

activate

display dialog "Can't write a macro file. pb System" buttons {"Cancel"} default button 1 with icon 2

end tell

return

end try

end if

-- if a StratupMacros exists, archive its content and write the code to use:

if test = cheminmacro then

try

get item cheminmacro

set macrofile to open for access (cheminmacro)

-- archive the StartupMacros content.

set ArchivedCode to read macrofile as string

close access macrofile

set macrofile to open for access (cheminmacro) with write permission

-- empty the StartupMacros file

set eof of macrofile to 0

-- write the macro code

write macros to the macrofile

close access macrofile

on error

tell application "Finder"

activate

display dialog "Can't write a macro file. pb System" buttons {"Cancel"} default button 1 with icon 2

end tell

return

end try

end if

-- delay needed to avoid ImageJ executes a macro commands before the macros are written

delay 2

-- activation of ImageJ by opening its application item path

try

open item "ImageJ" of folder pathfolderijapp

on error

tell application "Finder"

activate

display dialog "ImageJ doesn't start, has a modified app name or isn't installed" buttons {"cancel"} default button 1 with icon 2

return

end tell

end try

delay 2

-- open the dropped image file which path is in "chemin"

tell application "ImageJ"

activate

open file chemin

-- delay needed to avoid ij executes a macro command before opening the image file

delay 4

end tell

-- apply the macro to the image file

tell application "System Events"

tell process "ImageJ"

-- apply the macro 'Green LUT [Y] using the "Y" shortcut

keystroke "Y" using {shift down}

delay 2

-- apply the macro "LUT Animation [y]" unsig the 'y' shortcut

keystroke "y"

end tell

end tell

-- restore the initial StartupMacros content if necessary

if test "" then

try

get item cheminmacro

set macrofile to open for access (cheminmacro) with write permission

-- empty the StartupMacros file

set eof of macrofile to 0

-- write the initial macro code

write ArchivedCode to the macrofile

close access macrofile

on error

tell application "Finder"

activate

display dialog "Can't restore the StartupMacros file code. pb System" buttons {"Cancel"} default button 1 with icon 2

end tell

return

end try

end if

end tell

-- Quit the Apple Script application

quit

end open


That's all !

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

(ImageJ and AppleScript Main Page)


 Reference:

[1] ImageJ News v1.33e, 28 June 2004 , "Gilles Carpentier created an example AppleScript application that opens an image in ImageJ and processes it. Requires Mac OS X 10.3".


ij
§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.
ima Page author:
Gilles Carpentier
, Faculte des Sciences et Technologies,
Universite Paris 12 Val-de-Marne, France.
bioch
Microscopy Image Acquisition
and ImageJ contributions
mail Computer Data Acquisition
for Biochemistry Practice Works

Special thanks to Alessandra Albano for the English correction of this page.
--
Locations of visitors to this page

--

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

----

----

------