[Pythonmac-SIG] Simple AppleScript Class
Jay Painter
jpaint@serv.net
Wed, 5 Aug 1998 15:25:16 -0700 (PDT)
I've written a very simple convinence class for writing AppleScript with
the OSAm module. I'm sure it could be better, but here's the class and a
quick example. Suggestions are greatly appreshated...
class AppleScript:
def __init__(self, *args):
self.script_list = []
for i in range(len(args)):
self.script_list.append(args[i])
def Append(self, *args):
for i in range(len(args)):
self.script_list.append(args[i])
def AppendScript(self, applescript):
self.script_list = self.script_list + applescript.script_list
def Clear(self):
self.script_list = []
def Text(self):
return string.join(self.script_list, '\n') + '\n'
def CompileAndExecute(self):
import OSAm
return OSAm.CompileAndExecute(string.join(self.script_list, '\r'))
def CompileAndSave(self, path):
import OSAm
return OSAm.CompileAndSave(string.join(self.script_list, '\r'),path)
Examples...
script = AppleScript(
'tell application "Finder"',
' empty trash',
'end tell'
)
script.CompileAndExecute() # executes it
script.CompileAndSave(os.path.join(os.getcwd(), 'trashscript'))
... emptys the trash.
I've had problems with the ExecuteCompiledScript method in OSAm that I'm
trying to fix, so I have to use a small script to launch the saved
applescript from above:
launch_script = AppleScript(
'set scriptobj to (load script file "%s")' %
(os.path.join(os.getcwd(), 'trashscript')),
'tell scriptobj',
' run',
'end tell'
)
launch_script.CompileAndExecute() # runs the saved/compiled applescript
This should probably be part of the applescript class, but I'm hoping to
get RunCompiledScript working in OSAm so I don't have to do this anymore.
-----------------------------------------------------------------------
Jay Painter -- jpaint@serv.net -- jpaint@gimp.org -- jpaint@real.com
http://www.serv.net/~jpaint