-- DoShellCommand Maker - The ij.jar Version-v2
-- Code author, Gilles Carpentier, Faculte des Sciences et Technologies,
-- Universite Paris 12 Val-de-Marne, France.
-- Code of the macro ReturnTheVersion.
property macrocode : "theversion = getVersion();
print ('The Version is: ' + theversion);"
-- Routine - replace (x) by (\x) in a string, where (x) is a character.
on changesometexte(texttochange, aeliminer, remplacant)
set initialtext to texttochange
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
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
-- get the attributes of the droped item
on open truc
-- Test for the MacOSX version (10.3 or higher, Apple code).
tell application "Finder"
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 "1030" then
display dialog "This script requires the installation of " & ¬
"Mac OS X 10.3 or higher." buttons {"Cancel"} ¬
default button 1 with icon 2
end if
end tell
-- Get the location of the ij.jar file to be tested.
tell application "Finder"
activate
set chemin to (truc as string) -- put the path of the ij.jar file in the variable "chemin"
-- Test for file or folder properties of the droped item.
if last item of chemin is ":" then
display dialog ¬
"This is not a file. This script works only with a single ij.jar file." buttons {"Cancel"} default button 1 with icon 2
return
end if
get the name of the item chemin
set nomdejar to result as string
if nomdejar ≠ "ij.jar" then
display dialog "Droped file must be an ij.jar file." buttons {"Cancel"} default button 1 with icon 2
return
end if
end tell
-- Find the path of the ImageJ application, default ImageJ, or activated ImageJ.
tell application "Finder"
try
tell application "ImageJ"
activate
set originalIJ to ""
repeat while originalIJ = ""
set originalIJ to (path to frontmost application as Unicode text) as string
end repeat
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
end tell
----> originalIJ contains the path of the ImageJ application in the form toto:application:ImageJ:ImageJ.app, where toto is the volume.
---- To obtain the path of the folder containing the ImageJ.app:
tell application "Finder"
try
set forpathij to originalIJ
set tocut to ".app:"
if forpathij contains tocut then
set cutend to the offset of tocut in the forpathij
set forpathij to (characters 1 thru (cutend - 1) of forpathij) as string
end if
set pathsignal to ":"
set pathfolderijapp to ""
set decoupe to ""
if forpathij contains pathsignal then
try
set long to the number of characters of the forpathij
repeat while the forpathij contains pathsignal
set pospathsignal to the offset of pathsignal in the forpathij
set decoupe to (characters 1 thru pospathsignal of forpathij) as string
set pathfolderijapp to pathfolderijapp & decoupe
set forpathij to (characters (pospathsignal + 1) thru long of forpathij) as string
set long to (long - pospathsignal)
end repeat
end try
end if
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 0
return
end tell
end try
end tell
----> pathfolderijapp contains the path of the folder containing the ImageJ application in the form toto:application:ImageJ:
----> pathfolderijapp contains the path of the ImageJ application folder.
-- To get the ij.jar droped file version.
-- To tranform the ij.jar path into compatible "do shell path" by removal the first item (volume).
tell application "Finder"
activate
try
set forijjardoshell to chemin
set long to the number of characters of the forijjardoshell
set tocut to ":"
if forijjardoshell contains tocut then
set cutplace to the offset of tocut in the forijjardoshell
set forijjardoshell to (characters cutplace thru long of forijjardoshell) as string
end if
end try
-- Replace (:) by (/).
set forijjardoshell to my changesometexte(forijjardoshell, (ASCII character 58), (ASCII character 47))
end tell
-- To tranform the ImageJ application folder path into compatible "do shell path" by removal the first item (volume).
tell application "Finder"
activate
try
set fordoshellfolder to pathfolderijapp
set long to the number of characters of the fordoshellfolder
set tocut to ":"
if fordoshellfolder contains tocut then
set cutplace to the offset of tocut in the fordoshellfolder
set fordoshellfolder to (characters cutplace thru long of fordoshellfolder) as string
end if
end try
-- Replace (:) by (/) in the path of the ImageJ application folder.
set fordoshellfolder to my changesometexte(fordoshellfolder, (ASCII character 58), (ASCII character 47))
----> fordoshellfolder contains the ImageJ application folder in the form /application/ImageJ/
end tell
----> fordoshellfolder contains the ImageJ application folder in the form /application/ImageJ/
-- Make the do shell command with special characters protected.
set doshellforijjar to "java -jar " & quoted form of forijjardoshell & " -ijpath " & quoted form of fordoshellfolder & " -batch ReturnTheVersion"
-- Check for the presence of the macros folder.
tell application "Finder"
activate
-- path of the macros folder:
set macrofolderpath to pathfolderijapp & "macros:"
try
get item macrofolderpath
on error
display dialog "The macros folder can not be found at:
" & pathfolderijapp & "
The script can not continue." buttons {"Cancel"} default button 1 with icon 0
return
end try
end tell
-- Write the ReturnTheVersion.txt macro (macrocode) into the macros folder.
tell application "Finder"
activate
try
tell application "Finder"
set the ReturnTheVersion to open for access the file (macrofolderpath & "ReturnTheVersion.txt") with write permission
set eof of ReturnTheVersion to 0
write macrocode to the ReturnTheVersion
close access the ReturnTheVersion
end tell
end try
end tell
-- Check for the presence of the ReturnTheVersion macro.
tell application "Finder"
activate
set macroname to "ReturnTheVersion.txt"
set macropath to macrofolderpath & macroname
try
get item macropath
on error
display dialog "The macros " & macroname & " can not be found at:
" & macrofolderpath & "
The script can not continue." buttons {"Cancel"} default button 1 with icon 0
return
end try
end tell
-- Run the do shell command to get the ij.jar version.
tell application "Finder"
activate
try
do shell script doshellforijjar
set theijjarversion to result
on error
tell application "Finder"
activate
display dialog "ij.ImageJ process can't be start." buttons {"Cancel"} default button 1 with icon 0
end tell
return
end try
if theijjarversion = "" then
tell application "Finder"
activate
display dialog "The dropped ij.jar is obsolet. The version can't be obtained." buttons {"Cancel"} default button 1 with icon 0
end tell
end if
tell application "Finder"
activate
display dialog theijjarversion & "
for the ij.jar file at the location:
" & forijjardoshell
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
§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.
§3. 4 April 2006, ImageJ News: v1.37c, Gilles Carpentier has created a web page that explains how to send commands to ImageJ from AppleScript.