And I’m looking to execute a script from a string without using a Shell…
It might be possible that a huge portion would be creating at run time, not design time… so dragging a pre-made script into the IDE would not be an option.
You need to declare each of the individual functions. The good news is that NSString and CFString are interchangeable, so you can use the native CFStringRef and skip those declarations.
How soon do you need this Dave? And are you open to using MacOSLib?
If I put it in MacOSLib (a huge “if” right now since I have a lot of other things going on at the moment), I’d include an example window to illustrate its usage. That should give you the “snippet” you’re looking for.
[code] soft declare function NSClassFromString lib “Cocoa” (classname as CFStringRef) as ptr
soft declare function initWithSource lib “Cocoa” selector “initWithSource:” (obj as ptr,source as CFStringRef) as ptr
soft declare function executeAndReturnError lib “Cocoa” selector “executeAndReturnError:” (obj as ptr,byref error as ptr) as ptr
soft declare function alloc lib “Cocoa” selector “alloc” (classRef as Ptr) as Ptr
soft declare function stringValue lib “Cocoa” selector “stringValue” (classRef as Ptr) as CFStringRef
dim nsscript As ptr=initWithSource(alloc(NSClassFromString(“NSAppleScript”)),TextArea1.Text)
dim err As ptr
dim descriptor As ptr=executeAndReturnError(nsscript,err)
if err<> nil then
MsgBox “An error occured”
else
MsgBox stringValue(descriptor)
end if[/code]
Doesn’t look at the error data or anything fancy, but should be a good start
[code]Function ExecuteAppleScript(TheScript as String) As String #if TargetMacOS
soft declare function NSClassFromString lib “Foundation” (classname as CFStringRef) as ptr
soft declare function initWithSource lib “Cocoa” selector “initWithSource:” (obj as ptr,source as CFStringRef) as ptr
soft declare function executeAndReturnError lib “Cocoa” selector “executeAndReturnError:” (obj as ptr,byref error as ptr) as ptr
soft declare function alloc lib “Cocoa” selector “alloc” (classRef as Ptr) as Ptr
soft declare function stringValue lib “Cocoa” selector “stringValue” (classRef as Ptr) as CFStringRef
dim nsscript As ptr=initWithSource(alloc(NSClassFromString("NSAppleScript")),TheScript)
dim err As ptr
dim descriptor As ptr=executeAndReturnError(nsscript,err)
if err<> nil then
MsgBox "An error occured"
else
Return stringValue(descriptor)
end if
Ok… I saw the TextArea piece…
Not sure how to return certain things however…
Tell application "iCal"
set theCalendarNames to title of every calendar
return theCalendarNames
end tell
runs, but doesn’t return anything useful (null string)
Code I have now gets this information by reading actual OSX files directly… which I do not wish to do… .
I want to interact with iCal as “cleanly” as possible
Set the text item delimiters to “/” // Use whatever delimiter you wish here
Tell application “iCal”
set theCalendarNames to title of every calendar As String
return theCalendarNames
end tell