[futurebasic] Re: [FB] Sending an AppleScript to the Finder and unix shell in OS X

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : June 2002 : Group Archive : Group : All Groups

From: RICH LOVE <richlove@...>
Date: Sun, 09 Jun 2002 09:42:11 -0500
And now for something really cool...
Now that we can send an AppleScript from FB in carbon, we can access the
unix shell in OS X with a free osax available at
http://www.vampiresoft.com/Products/MacOS/osxpoweraddos.html

Here is the Jamin and Pastor code with my modifications to get a directory
listing from the unix shell.

_kOSAComponentType                    = _"osa "
_kOSAGenericScriptingComponentSubtype = _"scpt"
_kOSANullScript       = 0
_kOSANullMode         = 0

Begin Globals
Dim gResponse As Str255
End Globals

#If CarbonLib = 0
Library "AppleScriptLib"
#Endif
Toolbox Fn OSADoScript(Long,@Long,Long,Long,Long,@Long) = Long
Toolbox Fn OSAScriptError(Long,Long,Long,@Long) = Long
#If CarbonLib = 0
Library
#Endif

Clear Local
Local Fn AEDescToString( descPtr As .AEDesc, strPtr As .Str255 )
'~'9
Dim size As Long
Dim err  As OSErr

#If carbonLib
size = Fn AEGetDescDataSize( #descPtr )
#Else
size = Fn GetHandleSize( descPtr.dataHandle )
#endif

Long If size > 0
If size > 255 Then size = 255

#If carbonLib
err = Fn AEGetDescData( #descPtr, strPtr + 1, size )
#Else
BlockMove( [descPtr.dataHandle] , strPtr + 1, size)
#Endif

strPtr.nil` = size
Xelse
strPtr.nil` = 0
End If

End Fn


Clear Local
Local Fn AppleScriptCommand( textPtr As Ptr, ¬
                         textLen As Long, ¬
                   @theResultPtr As .Str255 )
'~'9
Dim As Long              ciResult
Dim AS OSErr             err, ignore
Dim As AEDesc            myDesc, myResultDesc
Dim As ComponentInstance ci

ci = Fn OpenDefaultComponent( _kOSAComponentType, ¬
             _kOSAGenericScriptingComponentSubtype )
Long If ci

err = Fn AECreateDesc( _"TEXT", #textPtr, textLen, myDesc )
Long If err = _noErr

ciResult  = Fn OSADoScript(  ci, ¬
                         myDesc, ¬
                _kOSANullScript, ¬
                        _"TEXT", ¬
              _kAENeverInteract, ¬
                   myResultDesc )

Long If ciResult <> _noErr
err = ciResult
ciResult = Fn OSAScriptError( ci, ¬
                         _"errs", ¬
                         _"TEXT", ¬
                   myResultDesc )
End If

Fn AEDescToString( myResultDesc, theResultPtr )

ignore = Fn AEDisposeDesc( myResultDesc )
ignore = Fn AEDisposeDesc( myDesc )
End If

ignore = Fn CloseComponent( ci )
Xelse
err = Syserror
End If

End Fn = err

Local Fn doScript
'~'9
Dim As OSErr  err

Route _toBuffer

Print "tell application ""Finder""
Print "shell ""ls -l"""
Print "end tell"

Route _toScreen

Long If gFBBuffer(0)
call HLock( gFBBuffer(0) )
err = Fn AppleScriptCommand(   [gFBBuffer(0)],Fn GetHandleSize( gFBBuffer(0)
),gResponse )
call HUnlock( gFBBuffer(0) )
Def DisposeH( gFBBuffer(0) )
if err then Print  "AppleScriptCommand Error: ";err
End If
End Fn


// Main program
Window 1


print "This is a directory listing from the unix shell"
Fn doScript

print
Print gResponse
print
print "Click to end"
Do
HandleEvents
Until Fn Button


End

Rich Love - Carnation Software
  Terminal emulations for Macintosh - MacWise, MacToPic Plus and SBMac.
  Email Checker, Carnation Desktop Pictures, Desktop Screen Saver,
  Index & Shutdown, One Touch Scan and QuitAll
  Visit our home page at http://www.carnation-software.com
  512 858-9234