Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > CLI AppleScript

CLI AppleScript
Thread Tools
Mac Elite
Join Date: May 1999
Location: San Jose, CA
Status: Offline
Reply With Quote
Jul 10, 2001, 11:16 AM
 
I can't be the only person to have thought of this, but I can't see any discussion in the archives, so here goes.

Why can't I run AppleScript scripts at the command line?

I don't mean 'saving an AppleScript as a compiled app and running the app', I mean write a shell script with AppleScript as the language, just like you can do with #! /usr/bin/perl, #! /bin/sh, #! /usr/bin/awk, etc., etc.

You should be able to write a script with:

#! /bin/applescript

as the first line and have the rest of the code in AppleScript.

There are two arguments I can think of as to why you can't do this, neither of which, I feel, are showstoppers.

First, you can argue portability. With scripting additions varying from machine to machine, this could be a problem, but not all that much more from differing versions of perl or missing perl libraries.

Secondly, someone will argue that AppleScript is a compiled (well, at least, tokenized) language and not interpreted. To which I say "So what?"
The vast majority of scripts I'm envisioning would be short scripts that could automatically be 'compiled' at runtime with little or no delay. The compiled version would be memory-based or, if you wanted to get really fancy, packaged in some way behind the script so that the compiled version was readily available next time the script was called (similar to how .apps are bundled now)

If this were to happen, you'd open scripting and the CLI to a new generation of AppleScript mavens.

I know there's already a myriad of available scripting options, but more choices can't hurt and, I still believe it's easier to read AppleScript than perl. It's certainly easier to get AppleScript to communicate and control other applications than it is in any other script language.

What technical reasons (other than Apple's ambivalence at using their own technology) are there for not doing this?

Could this be done without major reworking of the AppleScript code?

Could it be done outside of Apple?
Gods don't kill people - people with Gods kill people.
     
Senior User
Join Date: Feb 2001
Location: Rochester, uk
Status: Offline
Reply With Quote
Jul 10, 2001, 12:12 PM
 
As far as I can tell, the changes would need to be in the Unix part, not in AppleScript itself. The file /bin/applescript would simply be a wrapper program, that fed the text of the script into the compiler. It might be slow, but it should work perfectly well.

I suspect this could be written by anyone who knew enough about Unix. That ain't me, but such people exist all over the place.

On a separate note, i've never known why Unix didn't have a shortcut system for these: #!perl instead of #!/usr/bin/perl or #!/bin/perl or #!/System/Library/Unix ****/perl or whatever...

[ 07-10-2001: Message edited by: sadie ]
All words are lies. Including these ones.
     
Forum Regular
Join Date: Oct 2000
Location: Portland, OR USA
Status: Offline
Reply With Quote
Jul 10, 2001, 07:41 PM
 
You can run AppleScript from the command-line. Use /usr/bin/osascript. Although I can't get the #! style to work, but you can have it read from standard input. This is very cool.

Try:

<font face = "courier">
/usr/bin/osascript &lt;&lt;EndOfLine
say "Hello, world!"
EndOfLine
</font>

P.S. Please help with getting self running scripts. I've tried #! and --!, neither of them work. The kludge I've worked out is to put something like the above script into a file and run that.
<font face = "courier"> </font>

[ 07-10-2001: Message edited by: LordJavac ]
     
Grizzled Veteran
Join Date: Sep 2000
Location: Springfield, MA
Status: Offline
Reply With Quote
Jul 11, 2001, 12:40 AM
 
I tried way back in March to write this little script for a user so that a user logged in remotely could ask the user at the computer a question and get a reply back. Unfortunatly it never worked because of some Applescript weirdness on OS X.

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
#!/bin/sh

asfScript=/var/tmp/msgbox.asdf.$$

echo
echo Enter your question.
echo -n <font color = red>"=-&gt; "</font>
read inString

echo 'Tell application <font color = red>"Finder"</font>' &gt; $asfScript
echo activate &gt;&gt; $asfScript
echo display dialog \<font color = red>"$inString\"</font> <font color = green>default</font> answer \<font color = red>"reply would go here\"</font> buttons {\<font color = red>"ok\"</font>} <font color = green>default</font> button <font color = blue>1</font> &gt;&gt; $asfScript
echo end tell &gt;&gt; $asfScript

/usr/bin/osascript $asfScript

rm $asfScript
</font>[/code]

anyone have any ideas on how this could be made to work?
We hope your rules and wisdom choke you / Now we are one in everlasting peace
-- Radiohead, Exit Music (for a film)
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 11:12 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2