XYMer's Home away from Home

When http://bbs.xlr8yourmac.com is down (i.e. always)
It is currently Mon Nov 20, 2017 1:37 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Thu Oct 27, 2011 1:56 am 
Offline
Master

Joined: Sun Apr 20, 2008 5:24 am
Posts: 9693
Location: North of the State of Jefferson
AppleScript is wonderful in that it makes many otherwise intractable automation problems no worse than simply slow and irritating, but it is nonetheless still pretty crude tool for certain tasks. Some of those tasks are simple to do with a simple shell command, but other times you'd like the shell script to do something complex before returning to the happy-go-lucky land of AppleScript.

Indeed, it's no problem to call a shell script from AppleScript. For example, use the ipconfig utility to get the IP address of your primary network interface:
set myIP to do shell script ("ipconfig getifaddr en0")

Doing something more complex is more challenging because your script has to know how to execute the shell tool. That is, the tool needs to reside in your normal search path, or you need to provide a path to the utility. If it's a custom shell script you need to run, and you need to distribute your AppleScript to other people, or if you just want something tidy, it may be a frustrating problem. Fortunately, it's not hard to solve. Since AppleScript lets you save your scripts as Application Bundles, you can put your utility right inside the bundle. Then to call the utility, you can reference the path to the AppleScript's Application Bundle. :)

For example, if I have a shell script client and an AppleScript named Connector that needs to execute client to do whatever it does, I can do the following:
  1. Save the Connector AppleScript as an Application Bundle.
  2. Open the bundle and place the shell script client inside it.
  3. Then I'll modify the Connector script as follows, to get the path to client and run it via the familiar do shell script(...).:
Code:
...
tell application "Finder"
set myFolder to path to me as alias
end tell
set clientPath to quoted form of (POSIX path of myFolder & "client")
set theResults to do shell script (clientPath)
...

Moral: In this manner you can bundle external scripts (shell, python, perl, whatever...) in to your AppleScripts so that you can use AppleScript for an interface, without having to jump around ripping out clumps of scalp trying to make AppleScript do anything non-trivial.

- Anonymous


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group