Applescript Cocoa - Return error number / message

Mac OSX specific forum
adellavia
New User
New User
Posts: 4
Joined: Thu May 29, 2014 6:00 pm

Applescript Cocoa - Return error number / message

Post by adellavia »

Dear all, I have found in the Forum this Procedure to use/call Applescript trough CocoaMessage.

I want to know if it's possible, in case of execution error, to retrive also the Applescript error number / message / ... and not only the normal return value.

In the Apple Class Reference site I have found this information, I have tried to modify the originale code, but after many hours I don't have found the right way to do this thing.

https://developer.apple.com/library/mac ... rence.html

"Error Dictionary Keys
If the result of initWithContentsOfURL:error:, compileAndReturnError:, executeAndReturnError:, or executeAppleEvent:error:, signals failure (nil, NO, nil, or nil, respectively), a pointer to an autoreleased dictionary is put at the location pointed to by the error parameter. The error info dictionary may contain entries that use any combination of the following keys, including no entries at all.

extern NSString *NSAppleScriptErrorMessage;
extern NSString *NSAppleScriptErrorNumber;
extern NSString *NSAppleScriptErrorAppName;
extern NSString *NSAppleScriptErrorBriefMessage;
extern NSString *NSAppleScriptErrorRange;"

Thank you for any suggestion :)


Calling AppleScript
Code:

Code: Select all

Procedure.s AppleScript(Script.s)
  Protected retVal.s, strVal, numItems, i
  Protected aScript = CocoaMessage(0, CocoaMessage(0, CocoaMessage(0, 0, "NSAppleScript alloc"), "initWithSource:$", @Script), "autorelease")
  Protected eventDesc = CocoaMessage(0, aScript, "executeAndReturnError:", #nil)
  If eventDesc
    numItems = CocoaMessage(0, eventDesc, "numberOfItems")
    If numItems
      For i = 1 To numItems
        strVal = CocoaMessage(0, CocoaMessage(0, eventDesc, "descriptorAtIndex:", i), "stringValue")
        If strVal
          retVal + PeekS(CocoaMessage(0, strVal, "UTF8String"), -1, #PB_UTF8)
          If i <> numItems : retVal + #LF$ : EndIf
        EndIf
      Next
    Else
      strVal = CocoaMessage(0, eventDesc, "stringValue")
      If strVal : retVal = PeekS(CocoaMessage(0, strVal, "UTF8String"), -1, #PB_UTF8) : EndIf
    EndIf
  EndIf
  ProcedureReturn retVal 
EndProcedure
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3944
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Applescript Cocoa - Return error number / message

Post by wilbert »

Here's an example that shows the error number and message when compiling with debug enabled

Code: Select all

Procedure.s AppleScript(Script.s)
  Protected error, retVal.s, strVal, numItems, i
  Protected aScript = CocoaMessage(0, CocoaMessage(0, CocoaMessage(0, 0, "NSAppleScript alloc"), "initWithSource:$", @Script), "autorelease")
  Protected eventDesc = CocoaMessage(0, aScript, "executeAndReturnError:", @error)
  If eventDesc
    numItems = CocoaMessage(0, eventDesc, "numberOfItems")
    If numItems
      For i = 1 To numItems
        strVal = CocoaMessage(0, CocoaMessage(0, eventDesc, "descriptorAtIndex:", i), "stringValue")
        If strVal
          retVal + PeekS(CocoaMessage(0, strVal, "UTF8String"), -1, #PB_UTF8)
          If i <> numItems : retVal + #LF$ : EndIf
        EndIf
      Next
    Else
      strVal = CocoaMessage(0, eventDesc, "stringValue")
      If strVal : retVal = PeekS(CocoaMessage(0, strVal, "UTF8String"), -1, #PB_UTF8) : EndIf
    EndIf
  Else
    ; Error number
    Debug CocoaMessage(0, CocoaMessage(0, error, "objectForKey:$", @"NSAppleScriptErrorNumber"), "integerValue")
    ; Error message 
    strVal = CocoaMessage(0, error, "objectForKey:$", @"NSAppleScriptErrorMessage")
    Debug PeekS(CocoaMessage(0, strVal, "UTF8String"), -1, #PB_UTF8)
  EndIf
  ProcedureReturn retVal 
EndProcedure


AppleScript("bexep"); Should be "beep"
MessageRequester("","")
Windows (x64)
Raspberry Pi OS (Arm64)
adellavia
New User
New User
Posts: 4
Joined: Thu May 29, 2014 6:00 pm

Re: Applescript Cocoa - Return error number / message

Post by adellavia »

Hello wilbert, this is exactly what i was trying to do for a long time :)

Thank you very much for your help and for the quick reply :) !!

wilbert wrote:Here's an example that shows the error number and message when compiling with debug enabled

Code: Select all

Procedure.s AppleScript(Script.s)
  Protected error, retVal.s, strVal, numItems, i
  Protected aScript = CocoaMessage(0, CocoaMessage(0, CocoaMessage(0, 0, "NSAppleScript alloc"), "initWithSource:$", @Script), "autorelease")
  Protected eventDesc = CocoaMessage(0, aScript, "executeAndReturnError:", @error)
  If eventDesc
    numItems = CocoaMessage(0, eventDesc, "numberOfItems")
    If numItems
      For i = 1 To numItems
        strVal = CocoaMessage(0, CocoaMessage(0, eventDesc, "descriptorAtIndex:", i), "stringValue")
        If strVal
          retVal + PeekS(CocoaMessage(0, strVal, "UTF8String"), -1, #PB_UTF8)
          If i <> numItems : retVal + #LF$ : EndIf
        EndIf
      Next
    Else
      strVal = CocoaMessage(0, eventDesc, "stringValue")
      If strVal : retVal = PeekS(CocoaMessage(0, strVal, "UTF8String"), -1, #PB_UTF8) : EndIf
    EndIf
  Else
    ; Error number
    Debug CocoaMessage(0, CocoaMessage(0, error, "objectForKey:$", @"NSAppleScriptErrorNumber"), "integerValue")
    ; Error message 
    strVal = CocoaMessage(0, error, "objectForKey:$", @"NSAppleScriptErrorMessage")
    Debug PeekS(CocoaMessage(0, strVal, "UTF8String"), -1, #PB_UTF8)
  EndIf
  ProcedureReturn retVal 
EndProcedure


AppleScript("bexep"); Should be "beep"
MessageRequester("","")
Post Reply