Domain: applescriptsourcebook.com
Stories and comments across the archive that link to applescriptsourcebook.com.
Comments · 8
-
Other languages for Mac OSA scripting
[With Windows Scripting host] you can choose what language to use (unlike AppleScript, which forces you to use Apple's horrible proprietary COBOL clone).
Do you still feel the same way about AppleScript after reading this page?
-
Re:Its about time
Too bad when I try to script something from scratch, my guess at the english word that is expected is never right.
Amen. I remember my first attempt, circa 1996. I wanted a task to repeat every few minutes, pausing between loops. But what on earth could the AppleScript command be? Pause? No. Wait? No. Hold? No. It's idle. Ah. So simple, and yet so obscure. -
AppleScript and Perl
AppleScript had the concept of "dialects" which were AppleScript terms written in different languages (they had French, Japanese, Japanese (romanji), German, and Italian working). It was intriguing, I remember actually submitting an AppleScript in French for an assignment in French class in high school circa 1995.
English:
the first character of every word whose style is bold
French:
le premier caractère de tous les mots dont style est gras
Sample of an AppleScript in English and Japanese
Some discussion on it circa 1994
Note, this should not be confused with OSA (Open Scripting Architecture) dialects, like JavascriptOSA, which are different.
Aside from this, the most linguistically extendable language would probably be Perl (especially Perl 6). Having been written by a linguist, I imagine the most awareness of the linguistic aspects of coding in a different lanugage would be.
I mean really, "coding in another language" doesn't mean replacing "for" loops with "pour" loops, it means taking advantage of concepts (like word genders and verb conjugation) that are specific to that language. Programming "in a French way" could lead to constructs, algorithms, and phrasing very different from "standard C".
-
Re:Even as a Linux weenie...
Are there any good open source applications out there that I can take a look at, just so I can get an idea of the language? It sounds pretty interesting, and would be easier than purchasing a $25 book. (Especially since I live in the UK!)
You might want to look at Applescript for absolute beginners -
Re:Gnome and KDE are more or less the same these d
Scriptability: You mention AppleScript, and claims it is like having shellscript for GUI. No it isn't: you are bound to use that specific language. They could easily have supplied a network protocol (like KDE's DCOP) or any other more generic interface. Since they didn't, everything has to go to this dreadful language. Any experienced programmer would instantly fear "an easy-to-use, approachable, English-like language".
Way to do your research, lil buddy.
The AppleScript system is open. In fact, AppleScript just happens to be the default language Apple gives you to use within their "Open Script Architecture" (OSA).
For example, you could use JavaScript to tie into all the hooks AppleScript can. There is an older list of other OSA languages available as well.
As an experienced programmer, I find AppleScript useful. When I'm scripting a bunch of Mac apps, the english-ness and gimpy-ness of AppleScript has never bothered me. Why? Because I'm not doing any "real" work. If I'd like to do a combination of "real" work and scripting apps, I could easily use a language from the above list, or call the script events from C or a C module access by a real language. -
MacOS has had this for yearsSeriously, through Apple's Open Scripting Architecture folks have been able to use any number of languages such a Tcl/Tk. Java, Perl, Python, Jpython, and JavaScript under MacOS & MacOS X.
The great thing is that virtually every Mac application has hooks for scripting through the standard Apple Events model which is automagically available to all other OSA languages.
-
AppleScript
AppleScript is incredibly easy. If you can learn Perl in a week, you can learn AppleScript in... well, five minutes.
:)These are your basic references, in order:
- Apple's site. Place to start for novices; I find myself going back there sometimes now, but I usually hit the more advanced stuff really quick.
- AppleScript Sourcebook.The site for AppleScript references; it has a lot of information itself, but its real strength is in the links: nobody has so many great links.
- OSAXen. OSAXen are compiled extensions to AppleScript, similar in some ways to Perl modules. This is the best source of them.
AppleScript isn't as powerful as Perl in some ways, but it's vastly easier to use & it integrates into the GUI much, much better.
You need a program called the Script Editor. It might not be on your HD yet, but it will be on your MacOS install disks.
I'll even give you some sample code. Here ya go, this is what AS looks like; you can even cut and paste this into Script Editor, it should work. I give you this one because it's one of the few I have that doesn't use any OSAXen; it's all done through the scriptable Finder, so all you need is OS 9.
to ProcessFile(fileRef)
tell application "Finder"
set currentName to the name of fileRef
set extension to the last text item of currentName
if extension is "html" or extension is "htm" then
set creator type of fileRef to "DmWr"
set file type of fileRef to "TEXT"
else if extension is "smil" then
set creator type of fileRef to "TVOD"
set file type of fileRef to ".SMI"
else if extension is "gif" then
set creator type of fileRef to "ogle"
set file type of fileRef to "GIFf"
else if extension is "jpg" then
set creator type of fileRef to "ogle"
set file type of fileRef to "JPEG"
else if extension is "c" or extension is "h" or extension is "cpp" then
set creator type of fileRef to "MPS "
set file type of fileRef to "TEXT"
end if
end tell
end ProcessFile
to ProcessFolder(folderRef)
set AppleScript's text item delimiters to "."
tell application "Finder"
if (count of items of folderRef) is greater than 0 then
set fileNo to count of files of folderRef
set speechString to "Processing " & (fileNo) & " file"
if fileNo is not equal to 1 then
set speechString to speechString & "s"
end if
set folderNo to count of folders of folderRef
if folderNo is not equal to 0 then
set speechString to speechString & " and " & (folderNo) & " folder"
if folderNo is not equal to 1 then
set speechString to speechString & "s"
end if
end if
set speechString to speechString & " in folder " & name of folderRef
say speechString
set x to 1
repeat while x is less than or equal to the (count of folders of folderRef)
my ProcessFolder(folder x of folderRef)
say "Returning to " & name of folderRef
set x to x + 1
end repeat
set x to 1
repeat while x is less than or equal to the (count of files of folderRef)
set currentFile to file x of folderRef
my ProcessFile(currentFile)
set x to x + 1
end repeat
else
say "Skipping empty folder " & name of folderRef
end if
end tell
end ProcessFolder
set x to 1
set currentSelection to selection of application "Finder"
if (count of items of currentSelection) is 0 then
say "I have nothing to clean up."
else
repeat while x is less than or equal to the (count of items of currentSelection)
set currentItem to item x of currentSelection
if folder of (info for currentItem) then
ProcessFolder(currentItem)
else
ProcessFile(currentItem)
end if
set x to x + 1
end repeat
say "Cleanup file types done."
end ifWhat the previous batch of code does is ask Finder for its currently selected items, and then, for each of them, if it's a folder, it recursively processes every item in the folder, and checks its unix-style file extension. If the file extension matches one item in a list, then it changes the file's creator and document type codes. Specifically, it hands over all html files to DreamWeaver, C source to Macintosh Programmer's Workshop, and SMIL, GIF, and JPEG files to QuickTime. It executes rather slowly, but I don't need to run it very often. I compiled it to an applet, and have it in my Speakable Items folder.
It's a little buggy. It doesn't seem to work right when the currently selected items are files (well, it doesn't do anything at all). I don't know why, I haven't cared enough to fix it.
:)Most of my AppleScript links either to MacAST or to the QuickTime Player. Unfortunately, AppleScript has to be implemented at the application end, unlike Perl which can interface through pipes to most anything text-based. However, this makes it more powerful in some ways: AppleScript works exclusively by sending events, so it can do things that normally would require the user to click buttons, hard to do in Perl (although I think there's an AppleEvents module in MacPerl, which would be more flexible than AppleScript; all my Perl work I do on my *nix box so I don't know
:) -
One ExampleApple has had it's " Open Scripting Architecture " for a number of years. Apple's own AppleScript or Perl, Python, Java, JavaScript, Tcl/tk, etc. can all plug in and be used throughout the system. Indeed with the vast majority of MacOS applications being AppleScript-able they're all also automagically Perlable, Pythonable, Javable, JavaScriptable, Tcl/Tkable, etc. As all of the popular MacOS web browsers also support this it's not difficult to script things via web-pages.
As this same functionality is in MacOS X (and I believe in it's peer Open Source " Darwin ") the technology is availiable for others to review, compare/contrast, even appropriate. Whatever you think of Apple it's an interesting technology and has already proven it's worth with almost all MacOS applications being made AppleScriptable (and hence everything-else scriptable) for years.