Academic Tutorials

English | French | Portugese | German | Italian
Home Advertise Payments Recommended Websites Interview Questions FAQs
News Source Codes E-Books Downloads Jobs Web Hosting

Apple Script
Introduction to Applescript
Applescript and UNIX commands
Reading, Writing and using Lists
Line endings and ChemDraw
Building a substructure searchable database
Rendering the chemical structures
Printing the clipboard
Embedding chemical information in image files

HTML Tutorials
HTML Tutorial
XHTML Tutorial
CSS Tutorial
TCP/IP Tutorial
CSS 1.0
CSS 2.0
XML Tutorials
XML Tutorial
XSL Tutorial
XSLT Tutorial
DTD Tutorial
Schema Tutorial
XForms Tutorial
XSL-FO Tutorial
XML DOM Tutorial
XLink Tutorial
XQuery Tutorial
XPath Tutorial
XPointer Tutorial
RDF Tutorial
SOAP Tutorial
WSDL Tutorial
RSS Tutorial
WAP Tutorial
Web Services Tutorial
Browser Scripting
JavaScript Tutorial
VBScript Tutorial
DHTML Tutorial
HTML DOM Tutorial
WMLScript Tutorial
E4X Tutorial
Server Scripting
ASP Tutorial
PERL Tutorial
SQL Tutorial
ADO Tutorial
Apple Script
PL/SQL Tutorial
SQL Server
.NET (dotnet)
.Net Mobile
C# : C Sharp
SVG Tutorial
Flash Tutorial
Media Tutorial
SMIL Tutorial
Photoshop Tutorial
Gimp Tutorial
Gnuplot Programming
GIF Animation Tutorial
Scientific Visualization Tutorial
Web Building
Web Browsers
Web Hosting
W3C Tutorial
Web Building
Web Quality
Web Semantic
Web Careers
Weblogic Tutorial
Web Site Hosting
Domain Name
Java Tutorials
Java Tutorial
JSP Tutorial
Servlets Tutorial
Struts Tutorial
EJB Tutorial
JMS Tutorial
JMX Tutorial
Programming Langauges
C Tutorial
C++ Tutorial
Visual Basic Tutorial
Data Structures Using C
Assembly Language
Forth Programming
Lisp Programming
Data Warehousing
CGI Programming
Emacs Tutorial
Soft Skills
Communication Skills
Time Management
Project Management
Team Work
Leadership Skills
Corporate Communication
Negotiation Skills
Database Tutorials
Operating System
Software Testing
SAP Module
Business Warehousing
SAP Basis
Material Management
Sales & Distribution
Human Resource
Customer Relationship Management
Production and Planning
Networking Programming
Corba Tutorial
Networking Tutorial
Microsoft Office
Microsoft Word
Microsoft Outlook
Microsoft PowerPoint
Microsoft Publisher
Microsoft Excel
Microsoft Front Page
Microsoft InfoPath
Microsoft Access
Financial Accounting
Managerial Accounting
Network Sites

Applescript and UNIX commands

Previoushome Next

Applescript and UNIX commands

Getting Started with AppleScript is a comprehensive overview of Applescript that is part of the Apple Developers reference library. I'm not sure if you need to be signed up as a developer to read it but since you can be an online member for free it might be worth signing up.

There is also the Applescript Users list which gives you access to an extensive network of scripting experts.

There are also a range of excellent books, a couple that I'd suggest looking at are AppleScript: The Definitive Guide (ISBN 0-596-00557-1) by Matt Neuburg, and AppleScript Handbook (ISBN 0-9744344-9-3) by Danny Goodman.

There is also the excellent built in help which of course we can access with AppleScript :-)

Open up Script Editor and type:-


tell application "Help Viewer"
		search looking for "AppleScript"
end tell

Now click "Run" This should open up the Help Viewer application and run a search for "AppleScript"
Applescript and UNIX

Beneath the beautiful Mac OS X interface lies an industrial-strength UNIX system, many of the key UNIX-based scientific applications are now available under Mac OS X. While many users feel very comfortable in the command-line environment AppleScript offers a means to access the UNIX foundations without the need to remember a series of UNIX commands. However as you might expect there are a few things that you you need to be aware of when linking AppleScript and UNIX.

Leading you up the garden path

Applescript uses the colon ":" as a separator for directories however UNIX uses POSIX (Portable Operating System for UNIX) file paths in which the slash "/" is used as the directory separator. This could cause obvious problems, however one of the additions to AppleScript 1.8 was the ability to interconvert the two file reference systems.

Open up Script Editor and type/copy :-

set this_file to choose file

set this_file_text to (this_file as text)

display dialog this_file_text

set posix_this_file to POSIX path of this_file

display dialog posix_this_file

set this_file_back to (POSIX file posix_this_file) as string

display dialog this_file_back

The first line allows the user to select a file, the second line converts the file reference to text and then it is displayed. The fouth line converts the Apple file reference to the POSIX path and then displays it. Of course you need to be able to do the reverse translation and the "POSIX file" command converts the POSIX path to a Mac OS X path as shown below.


Here is a very simple example of how this can be used, this takes a Macintosh Path to a folder, converts it to the POSIX path. The script then runs the Shell command "ls" to list the contents of the directory and displays the result.

set a to "Macintosh HD:usr:local:bin:" --Mac file path

set p to POSIX path of a --Convert to POSIX path 

set the_text to (do shell script "ls " & p)

display dialog the_text

Whilst this works perfectly in most cases there are a couple of issues you should be aware of, a file or folder name may contain characters which need to be escaped to be passed to a shell command. Use quoted form to get the quoted form of a string. For example if a directory name contains a space.

set ap_sup to path to application support

set p_ap_sup to POSIX path of ap_sup

--  "/Library/Application Support/" -- contains a space 

do shell script "ls " & p -- does not work 

do shell script "ls " & quoted form of p -- works 

Come out from under that shell

The default shell for Mac OS X is BASH and I don't want to get into a debate about the relative merits of the different shells that are available, the important thing to remember when running a "Do shell script" command is that do shell script always uses /bin/sh to interpret your command, not your default shell. So commands that work fine in the Terminal may not work when called using the "Do shell script" command. In addition when you use just a command name instead of a complete path, the shell uses a list of directories (known as your PATH) to try and find the complete path to the command. For security and portability reasons, do shell script ignores the configuration files that an interactive shell would read, so you don't get the customizations you would have in Terminal. You need to use the full path to the command. Compare the results of these two applescripts:

do shell script "echo $PATH"

Should give you something like "/usr/bin:/bin:/usr/sbin:/sbin"

On the other hand this applescript

set the_shell_script to "echo $PATH"

tell application "Terminal"
	do script the_shell_script
end tell

Will give you the complete PATH as defined in your .profile.

Having a little Fun with UNIX

This script gets a list of all the applications in the Applications folder, then finds those applications that contain the capital letter "B", then sorts them into reverse alphabetical order and displays the result. The "|" is the pipe command, the pipe command is one of the most important commands in UNIX because it allows us to create powerful functions in a single statement. The pipe command is represented with the | character and it is used to gconnecth the output from one command and send it as input to another command. On a Mac keyboard it is on the right-hand side next to the return key.

set a to "Macintosh HD:Applications:"

set p to POSIX path of a 

set the_text to (do shell script "ls " & p & "| grep B | sort -r")

display dialog the_text

Here is a fun feature:-

set the_script to "cal"
set the_text to (do shell script the_script)

set the clipboard to the_text

Run this script and then paste into a text editor, (I used BBEdit) and you should see this months calendar. Replace "cal" with "cal 10 1492" and you can see the month in 1492 when Christopher Columbus landed on a Caribbean Island.

Putting it under the Spotlight

Tiger introduced Spotlight, a wonderful searching mechanism that indexes the contents of all the files on your Mac without any effort, it also provides a means to search that information. However you can also search the metadata using UNIX, mdfind provides a command line search interface, we can interact with this using Applescript.

As you might expect as a chemist I have a lot of documents containing chemical information the following script uses mdfind to identify all files that contain the word "ketone', it displays all the file names and then a count of how many files, too many.

set the_script to "mdfind   'ketone'"

set the_text to (do shell script the_script)

display dialog the_text

set num_records to count of paragraphs in the_text

display dialog num_records

I remember that I downloaded the file I want last week so I can limit the search.

set the_script to "mdfind   -onlyin  '/Downloads/'  'ketone'"

set the_text to (do shell script the_script)

display dialog the_text

set num_records to count of paragraphs in the_text

display dialog num_records

Now I only find two files. OK so I could do that with Spotlight, well download ChemSpotlight and install it. ChemSpotlight is a Spotlight metadata importer plugin for Mac OS X, which reads common chemical file formats (MDL .mol, .mdl, .sd, .sdf, Tripos .mol2, Protein Data Bank .pdb, Chemical Markup Language .cml, and XYZ) using the Open Babel chemistry library, it then adds molecular formulas (complete with subscripts in the Finder), molecular weight, and a variety of other chemical information.

Now if we modify the mdfind script as shown below, this script now searches your hard drive for all chemical structure files that contain a molecule with a molecular weight less that 250.


set the_script to "mdfind   'net_sourceforge_openbabel_Mass < 250'"
set the_text to (do shell script the_script)

display dialog the_text

ChemSpotlight adds a variety of metadata as shown in the table below, all of which can be searched using mdfind.

Metadata Field Notes
net_sourceforge_openbabel_Chirality True/False (1/0)
net_sourceforge_openbabel_Dimension 0D/2D/3D depending on the coordinates found
net_sourceforge_openbabel_DisplayFormula Formula with subscripts for Finder gGet Infoh windows
net_sourceforge_openbabel_Formula Chemical formula in standard gHill Orderh
net_sourceforge_openbabel_Mass Standard molecular weight in a.m.u. (g/mol)
net_sourceforge_openbabel_ExactMass Molecular mass of most common isotopes for mass spectra
net_sourceforge_openbabel_NumAtoms Number of atoms in the molecule
net_sourceforge_openbabel_NumBonds Number of bonds in the molecule
net_sourceforge_openbabel_NumMols Number of molecules in the file
net_sourceforge_openbabel_NumResidues Number of biomolecule residues
net_sourceforge_openbabel_SMILES Daylight SMILES string for this molecule
net_sourceforge_openbabel_InChI IUPAC/NIST canonical identifier
net_sourceforge_openbabel_Sequence Biomolecule residue sequence

Give me some input

This is the mechanism by which iBabel searches chemical information, of course iBabel was built using Applescript Studio which allows the construction of rich user interfaces, and hopefully that will be the subject of a future tutorial. However we can use Applescript to provide a limited user interface, the following script asks the user to choose a file, it then uses the UNIX commandline tool OpenBabel to convert the file to the format chosen in the second dialog box and then saves it to the desktop. You will need to have installed OpenBabel or better/easier ChemSpotlight. You could have yet another dialog box asking to choose where to save the file but I think you can see why Applescript Studio would be a better option.


--Get path to desktop to save result
set out_path to path to desktop
set out_path_posix to (POSIX path of out_path & "output_file")

--Get file to convert
set TheFile to (choose file with prompt "Choose a file for Conversion")

set the_posix_file to POSIX path of TheFile

display dialog "Choose output file format" buttons {"smi", "mol2", "sdf"} default button 3

set out_format to the button returned of the result

--Generate shell script
-- /usr/local/bin/babel     '/Public/multimol.sdf' -osmi \  
-- '/Users/username/Desktop/outputfile.smi' 

set babel_script to "/usr/local/bin/babel '" & the_posix_file & "' -o" \ 
                    & out_format & " '" & out_path_posix & "." & out_format & "'"

do shell script babel_script

Be the first one to comment on this page.

  Apple Script eBooks

No eBooks on Apple Script could be found as of now.

 Apple Script FAQs
More Links » »
 Apple Script Interview Questions
More Links » »
 Apple Script Articles

No Apple Script Articles could be found as of now.

 Apple Script News

No News on Apple Script could be found as of now.

 Apple Script Jobs

No Apple Script Articles could be found as of now.

Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • connotea
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Netvouz
  • RawSugar
  • Reddit
  • scuttle
  • Shadows
  • Simpy
  • Smarking
  • Spurl
  • TailRank
  • Wists
  • YahooMyWeb

Previoushome Next

Keywords: applescript language,applescript documentation,applescript manual,applescript guide,applescript application

HTML Quizzes
CSS Quiz
CSS 1.0 Quiz
CSS 2.0 Quiz
XML Quizzes
XML Quiz
XSL Quiz
DTD Quiz
Schema Quiz
XForms Quiz
XLink Quiz
XQuery Quiz
XPath Quiz
XPointer Quiz
RDF Quiz
RSS Quiz
WAP Quiz
Web Services Quiz
Browser Scripting Quizzes
JavaScript Quiz
VBScript Quiz
WMLScript Quiz
E4X Quiz
Server Scripting Quizzes
ASP Quiz
SQL Quiz
ADO Quiz
CVS Quiz
Python Quiz
Apple Script Quiz
SQL Server Quiz
PHP Quiz
.NET (dotnet) Quizzes
Microsoft.Net Quiz
ASP.Net Quiz
.Net Mobile Quiz
C# : C Sharp Quiz
VC++ Quiz
Multimedia Quizzes
SVG Quiz
Flash Quiz
Media Quiz
Photoshop Quiz
Gimp Quiz
Matlab Quiz
Gnuplot Programming Quiz
GIF Animation Quiz
Scientific Visualization Quiz
Graphics Quiz
Web Building Quizzes
Web Browsers Quiz
Web Hosting Quiz
W3C Quiz
Web Building Quiz
Web Quality Quiz
Web Semantic Quiz
Web Careers Quiz
Weblogic Quiz
SEO Quiz
Web Site Hosting Quiz
Domain Name Quiz
Java Quizzes
Java Quiz
JSP Quiz
Servlets Quiz
Struts Quiz
EJB Quiz
JMS Quiz
JMX Quiz
Eclipse Quiz
J2ME Quiz
Programming Langauges Quizzes
C Quiz
C++ Quiz
Visual Basic Quiz
Data Structures Using C Quiz
Cobol Quiz
Assembly Language Quiz
Mainframe Quiz
Forth Programming Quiz
Lisp Programming Quiz
Pascal Quiz
Delphi Quiz
Fortran Quiz
OOPs Quiz
Data Warehousing Quiz
CGI Programming Quiz
Emacs Quiz
Gnome Quiz
ILU Quiz
Soft Skills Quizzes
Communication Skills Quiz
Time Management Quiz
Project Management Quiz
Team Work Quiz
Leadership Skills Quiz
Corporate Communication Quiz
Negotiation Skills Quiz
Database Quizzes
Oracle Quiz
MySQL Quiz
Operating System Quizzes
BSD Quiz
Symbian Quiz
Unix Quiz
Internet Quiz
IP-Masquerading Quiz
IPC Quiz
Software Testing Quizzes
Testing Quiz
Firewalls Quiz
SAP Module Quizzes
ERP Quiz
Business Warehousing Quiz
SAP Basis Quiz
Material Management Quiz
Sales & Distribution Quiz
Human Resource Quiz
Netweaver Quiz
Customer Relationship Management Quiz
Production and Planning Quiz
Networking Programming Quizzes
Corba Quiz
Networking Quiz
Microsoft Office Quizzes
Microsoft Word Quiz
Microsoft Outlook Quiz
Microsoft PowerPoint Quiz
Microsoft Publisher Quiz
Microsoft Excel Quiz
Microsoft Front Page Quiz
Microsoft InfoPath Quiz
Microsoft Access Quiz
Accounting Quizzes
Financial Accounting Quiz
Managerial Accounting Quiz
Testimonials | Contact Us | Link to Us | Site Map
Copyright ? 2008. Academic All rights reserved Privacy Policies | About Us
Our Portals : Academic Tutorials | Best eBooksworld | Beyond Stats | City Details | Interview Questions | Discussions World | Excellent Mobiles | Free Bangalore | Give Me The Code | Gog Logo | Indian Free Ads | Jobs Assist | New Interview Questions | One Stop FAQs | One Stop GATE | One Stop GRE | One Stop IAS | One Stop MBA | One Stop SAP | One Stop Testing | Webhosting in India | Dedicated Server in India | Sirf Dosti | Source Codes World | Tasty Food | Tech Archive | Testing Interview Questions | Tests World | The Galz | Top Masala | Vyom | Vyom eBooks | Vyom International | Vyom Links | Vyoms | Vyom World | Important Websites
Copyright ? 2003-2024 Vyom Technosoft Pvt. Ltd., All Rights Reserved.