Applescript for Scientists Tutorials
This is the first of what will hopefully be a series of tutorials on Apple's own scripting language Applescript that I will be writing for MacResearch.org. I should perhaps begin with a confession, "I'm not a programmer", by training I'm a chemist and I spent the majority of my career as a Medicinal Chemist. This might actually make me the ideal person to write this since I've always thought of Applescript as the programming language for the rest of us. Applescript is a scripting language that allows users to automate reptitive or complex tasks, or customise applications as such it is really useful for lttle tools or widgets that make life easier for you. In this tutorial I cover getting started, using the script editor, Applescript dictionary, folder actions and interacting with applications.
Click here to see a formatted view of this tutorial in a new window.
Applescript Tutorial 2
This is the second in the Applescript tutorials written for MacResearch, in this tutorial I cover the use of Applescript interactions with the UNIX system, in particular the issues of Mac and UNIX paths and the use of shell scripts.
Click here to see a formatted view of this tutorial in a new window.
Applescript Tutorial 3
This is the third Applescript tutorial written for MacResearch, reading and writing files, creation and manipulation of lists, and using ChemDaw to calculate properties.
Click here to see a formatted view of this tutorial in a new window.
Applescript Tutorial 4
This is the fourth Applescript tutorial written for MacResearch, reading and writing UNIX files, dealing with missing properties, and using ChemDaw to calculate properties.
Click here to see a formatted view of this tutorial in a new window.
Applescript Tutorial 5
This is the fifth Applescript tutorial written for MacResearch, creating a sub-structure searchable chemical database.
Click here to see a formatted view of this tutorial in a new window.
Applescript Tutorial 6
This is the sixth Applescript tutorial written for MacResearch, rendering structures in a chemical database
Click here to see a formatted view of this tutorial in a new window.
Applescript Tutorial 7
This is the seventh Applescript tutorial written for MacResearch, actually it uses a web viewer within the FileMaker database to display a web page that uses the chemdraw plugin to render the structure.
Click here to see a formatted view of this tutorial in a new window.
Applescript Tutorial 8
This is the eighth Applescript tutorial written for MacResearch. Rich Apodaca has been discussing embedding molecular information in images of molecules, such as a PNG file depicting a 2D structure. As we move to a more web-centric view of the world it is apparent that much of research information will be only available via the web, whilst images of chemical structures are usually adequate for a human viewer the chemical structure cannot be indexed and subsequently searched. In a subsequent article Rich showed a method of extracting the information as text. In this tutorial I'm going to show how to use applescript to extract the information from the PNG file and then display the structure in a couple of chemical display packages in an editable form.
Click here to see a formatted view of this tutorial in a new window.
Applescript Tutorial 9
This is the ninth Applescript tutorial written for MacResearch. In this tutorial we create an applescript for ChemBioDraw (aka ChemDraw) that embeds chemical information into the meta data of an image file.
Click here to see a formatted view of this tutorial in a new window.
Applescript Tutorial 10
At my presentation on Applescript and UNIX I was asked how to create a droplet that accessed a UNIX application so I thought it might be useful to give a very simple example
Click here to see a formatted view of this tutorial in a new window.
Send to MOE
ConvertChemDraw_to_TIFF
Convert Applescript to HTML
DupCheck
It is based on a hint from macosxhints, see http://www.macosxhints.com/article.php?story=20061003163429425
ChemDraw Properties
Multi ChemDraw Properties
Combining PDFs a Automator Workflow
Today I was sent a license agreement as a 6 page
pdf file, I was asked to sign the document and send
it back. All very well but I was in a hurry and
wanted to get to work a little faster than the
postal service might allow. I have a scanner so I
figured I could sign a copy of the last page then
scan it and convert to pdf. I could have then sent
the original license pdf plus the last page by
email but that did seem a little unprofessional.
So then how to combine the signed page with the
rest of the pages of the license agreement?
Well opening the original pdf in Preview I could
select pages 1 to 5 to print and then use the "Save
as pdf" option from the "PDF" button so create a
new pdf with just the first 5 pages.
Now create an Automator workflow to join the files.
Open Automator and you should see a three pane
window. The first pane contains the library of
applications that have automator actions, the next
pane shows the actions available for the selected
application. Double click on an action to add it to
the workflow in the third pane. No add these
actions
From the Finder library select:-
Get Specified Finder Items
Sort Finder Items (and
from the pulldown menus select "name" and
"ascending"
From the PDF library select:-
Combine PDF Pages
Then finally from the Preview Library selct:-
Open
Images in Preview.
Now save the workflow as Combine_PDF in
"username/Library/Workflows/Applications/Finder/"
Now when you select several pdf files in the
Finder, control-click (or right click) will bring
up a menu that includes automator actions, choose
combine_PDF and Preview will open showing a single
document containing all files.
Note I added a sort option, this was to give some control over how the files were combined. If you now name them alphabetically then they will be combined as desired.
Print_clipboard
--Need
to first remove application specific formatting
from clipboard
set
the clipboard to «class
ktxt»
of ((the
clipboard as
text)
as record)
set
the_clip
to the
clipboard
--Comment
out if not needed
display
dialog the_clip
tell
application
"Finder"
to
set the_folder
to (path
to temporary
items folder)
as text
--If
you want to keep the file
use
--tell
application "Finder" to set the_desktop to (path to
desktop folder) as text
set
target_file
to the_folder
&
"JUNK"
--display
dialog target_file
my
write_to_file(the_clip,
target_file,
false)
set
posix_path
to POSIX
path
of target_file
--display
dialog posix_pathsw
set
to_print
to "lp " &
posix_path
do shell
script to_print
--For
testing
--tell
application "Terminal"
--activate
--do
script to_print
--end
tell
on
write_to_file(this_data,
target_file,
append_data)
try
set
the target_file
to
the target_file
as text
set
the open_target_file
to ¬
open for
access file
target_file
with write
permission
if append_data
is false
then ¬
set
eof
of
the open_target_file
to
0
write
this_data
to
the open_target_file
starting
at eof
close
access
the open_target_file
--display
dialog "file_done"
return true
on
error
try
close
access file
target_file
end
try
return false
end
try
end
write_to_file
Comparing version numbers
You can get the version number of the OpenBabel installation with the simple command
/usr/local/bin/babel -V
Open Babel 2.1.0 -- Apr 13 2007 --
21:38:32
We can wrap this
in an Applescript and use the "do shell script"
command to get the returned text, simple parsing of
the returned text then gives the version number.
set
the_script
to "/usr/local/bin/babel
-V"
set
the_version
to (do shell
script the_script)
display
dialog the_version
--Open
Babel 2.1.1 -- Apr 13 2007 --
21:38:32
set
the_offset_minus
to
the offset
of
"--"
in
the_version
set
the_offset_el
to
the offset
of
"el"
in
the_version
set
babel_version
to (characters
(the_offset_el
+ 3)
thru (the_offset_minus
- 2)
of the_version)
as text
display
dialog babel_version
Comparing version
numbers however is not so straightforward since the
version number could be 2.10.3 or 2.3.10,
fortunately Apple have kindly provided a simple
means to compare this type of numeric strings. So
using "considering numeric strings".
considering
numeric
strings
if babel_version
>
"2.1"
is true
then -->
true, if you consider each numeric string to be a
single "character".
display
dialog "True"
end
if
end
considering
The actual
implementation in iBabel is this:-
considering numeric
strings
--Different tools available depending on
version of iBabel
if babel_version
>
"2.1"
is true
then -->
true, if you consider each numeric string to be a
single "character".
--display dialog "2.2"
repeat
with i
in
my tools_options_new
make new
menu item at the end of menu items of menu of popup
button "tools_option" of tab view item "Tools" of
tab view "nstab" with properties {title:i,
enabled:true}
end
repeat
else
if babel_version
<
"2.1"
is true
then
--display dialog "2.0"
repeat
with i
in
my tools_options_old
make new
menu item at the end of menu items of menu of popup
button "tools_option" of tab view item "Tools" of
tab view "nstab" with properties {title:i,
enabled:true}
end
repeat
end
if
end
considering
So depending on
the version of OpenBabel I load a different list of
tools options.
Print Folder Contents
Full details are here.
Lecture on Applescript and Unix
The presentation is available here.
If anyone would like a copy of the slides let me know.