Basic AppleScript Chat 30 April 05
Useful stuff
Apple AppleScript:
Developers
Applications
Doug's AppleScript's for iTunes
AppleScripting SuperCard how to control SC with appleScript.
nothing to do with SC but I love Tex-Edit Plus and there are a lot of AppleScripts for that app here: AppleScripts for Tex-Edit Plus
These are my notes for a SuperCard iChat about AppleScript, the whole transcript will be availible Bruce Martin's SuperCard Chat iCal .
NB. These are my notes and have had no grammer, sense or spell check.
Ok this is basic, all I have learned about appleScript has been through SuperCard, and most of the following is expanded from the SLG along with hints and helps from the SC List and google.
You need SuperCard 4.1.2 or 4.5 and the examples will also use iChat, Mail, iTunes and the finder. Hopefully most folk will have these.
I couldn't think of many examples that cover 8.6 to Tiger. some of the Scripts may not work, but I've tested them in Panther.
SuperCard can run AppleScripts, either compiled and stored in the data fork of a project or the text of a script a field or variable.
Supercard can also edit, check and compile AppleScripts. Actually SuperCard supports any OSA-compliant scripting component, but here we will only discuss AppleScript.
SuperCard's interaction with AppleScript (and other OSA scripting components) is through its script function:
Script(actionKeyword,[OSAComponent],[scriptName][text])
The action key words are: list, list all, list open, open, close, load, unload, run, do, check, compile and edit.
I am going to mostly discuss do as I think is is probably the most useful.
First create a new project, make a nice big window with 2 background fields, script and result.
Add a couple of buttons, check and do.
on mouseUp select text of fld "script" put script(check,appleScript) into fld "result" end mouseUp
on mouseUp put script(do,applescript,fld "script") into fld "result" end mouseUp
now put this into the script field:
tell app "iChat" to activate
click the check button
see how the fld is formatted
click the do button and iChat should activate.
here is another quicky:
tell app "iChat" to set status message to "testing applescript..."
now try: tell app "iChat" activate tell app "iChat" to set status message to "testing applescript 2" end tell
We can run applescript inside a tell block or one liners with:
tell app "app_Name" to do_something
here is an example using mail paste it into the script field and click the run button:
http://www.littlefishsw.co.uk/card/howtos/simplemailscript.txt
or just put this in the message
put shell("curl http://www.littlefishsw.co.uk/card/howtos/simplemailscript.txt") into fld "script"
and hit return
This is the script:
tell application "Mail"
set newMessage to make new outgoing message with properties {subject:"hi john", content:"This is a message sent by Supercard"}
tell newMessage
set visible to false
make new to recipient at end of to recipients with properties {name:"John Johnston", address:" john@littlefishsw.co.uk"}
send
end tell
end tell
So far we have been running scripts from a field but normally we will want to run them from a script.
(you might want to 'set the width of msg to 1000' first)
so in the message try:
script(do,applescript,merge("tell app `iChat` to set status message to `from the message`"))
merge bypasses building scripts with quotes in variables and can manage multiline scripts too.
script(do,applescript,merge("tell app `iChat` [[CR]]activate[[CR]] set status message to `applescript merge`[[CR]]end tell"))
there is a load of different ways to store and run appleScripts on the fly. I like userprops with merge for example:
put script(do,appleScript,merge(the uappleScript of me)) into tResult
where the button has an appleScript in a userprop uApplescript
I am going to give a few examples of appleScripts with iTunes.
So open iTunes.
go back to our test proj and type this into the script field:
tell application "iTunes" to set x to name of every playlist as list
I get: {"Library", "Party Shuffle", "Purchased Music", "Misty In Roots", "My Top Rated", "Recently Played", "Top 25 Most Played", "Acquisition", "LimeWire", "Live at the Counter Eurovision", "temp"}
so here is a function to get the playlists:
function iTunePlaylists
get script (do,applescript,merge("tell application `iTunes` to set x to name of every playlist as list"))
get replace(it,quote&comma&space"e,cr)
delete char 1 to 2 of it
delete char ((the number of chars in it)-1) to the number of chars in it of it
return it
end iTunePlaylists
another message script:
script(do,AppleScript,merge("tell application `iTunes` to set x to player state"))
function iTunesState
return script(do,AppleScript,merge("tell application `iTunes` to set x to player state"))
end iTunesState
back to the project
tell application "iTunes" to set x to player state tell application "iChat" to set status message to "itunes: "&x
tell application "iTunes" to set x to name of EQ presets as list
gives me:
{"Manual", "Acoustic", "Bass Booster", "Bass Reducer", "BBass Booster", "Classical", "Dance", "Deep", "Electronic", "Flat", "Hip-Hop", "Jazz", "Latin", "Loudness", "Lounge", "Piano", "Pop", "R&B", "Rock", "Small Speakers", "Spoken Word", "Treble Booster", "Treble Reducer", "Vocal Booster"}
I can split this into a more friendly (to SC ) list in appleScript
tell application "iTunes" set x to name of EQ presets as list set treturn to "" repeat with i in x set treturn to treturn & i & return end repeat return treturn end tell
Here is a wee finder script:
tell application "Finder" set tfile to choose file set tr to properties of tfile end tell
put it into the script field try it
add a new button compile
on mouseUp select text of fld "script" put script(compile,appleScript,"My_compiled_Script") into fld "result" end mouseUp
now in the message:
answer script(list,appleScript)and
script(run,appleScript,"My_compiled_Script")So
script(compile,appleScript,"scriptName")has compiled the script in the current open field and
script(run,appleScript,"scriptName")will run the named script resource
You can speed up a script by loading it into RAM with: Get Script(load,OSAComponent,scriptName) I have never use this.
compiled scripts should run a little faster than scripts run from a var with do, but they are not as flexable as you can't change varables on the fly.
how do you know what you can do with appleScript: in the scriptfield:
tell application "Finder" to set x to application file id "ToyS" as string tell application x to activate
Should open your AppleScript ScriptEditor from there you can open the library from the Window Menu and see the dictionarys for some apps, you can add more with the + button.
You can also use appleScript to open a dictionary in the appleScript Editor, in the script field:
tell application "Finder" set theapp to application file id "TVOD" set scriptEd to application file id "ToyS" open theapp using scriptEd end tell
You can do stuff that is not in the appleScript dictionaries with GUI Scripting You need the GUI Scripting stuff from apple to try this.
Try this:
tell application "System Events" set UI_enabled to UI elements enabled end tell
in the script field, if you get true in the result the gui script stuff will work, if not you can get find out how to turn it on here:
http://www.apple.com/applescript/uiscripting/01.html
you need Mac OS X v10.3
Here is a quick example of GUI scripting, stick this in the msg
put shell("curl http://www.littlefishsw.co.uk/card/howtos/assafariexample.txt") into fld 1
This will get the script from here
http://www.littlefishsw.co.uk/card/howtos/assafariexample.txt
and stick it in the script field once that happens you can do it.