How passing a variable from Shell to AppleScript?

Hello,
I have an AppleScript routine that I want to put in a method, with “appName As String” and return a Boolean. I have to pass the app as a parameter (here “TextEdit” for example) but I can’t find the right syntax between quotation marks (double quotes) and simple quotes ! it’s very difficult !
I spent a lot of time there without finding the solution so I defer to you !

//Shell1.Execute("osascript -e 'tell application ""System Events"" to (name of processes) contains ""TextEdit""'")

Thanks

To be clearer, I want to replace “TextEdit” with a variable.

You have an extra single quote on the right of TextEdit.

var myVariable as string = "TextEdit"
var osa as string = "osascript -e 'tell application ""System Events"" to (name of processes) contains ""*""'"
Shell1.execute(osa.replace("*", myVariable))

Thanks you very much Michel :smile:

Why use osascript? You can just use a few declares and there is no need for an expensive plugin!

Public Function ExecuteAppleScript(TheScript As String) As String
  Soft Declare Function NSClassFromString Lib "AppKit" (classname As CFStringRef) As ptr
  Soft Declare Function initWithSource Lib "AppKit" Selector "initWithSource:" (obj As ptr,source As CFStringRef) As ptr
  Soft Declare Function executeAndReturnError Lib "AppKit" Selector "executeAndReturnError:" (obj As ptr,ByRef error As ptr) As ptr
  Soft Declare Function alloc Lib "AppKit" Selector "alloc" (classRef As Ptr) As Ptr
  Soft Declare Function autorelease Lib "AppKit" Selector "autorelease" (classRef As Ptr) As Ptr
  Soft Declare Function stringValue Lib "AppKit" Selector "stringValue" (classRef As Ptr) As CFStringRef
  
  Soft Declare Function objectForKey Lib "Foundation" Selector "objectForKey:" (obj_id As ptr, key As CFStringRef) As ptr
  Soft Declare Function stringForKey Lib "Foundation" Selector "objectForKey:" (obj_id As ptr, key As CFStringRef) As CFStringRef
  
  Dim nsscript As ptr = autorelease(initWithSource(alloc(NSClassFromString("NSAppleScript")),TheScript))
  Dim err As ptr
  Dim descriptor As ptr = executeAndReturnError(nsscript,err)
  If descriptor = Nil Then
    Dim e As New RuntimeException
    If err = Nil Then
      e.Message = "AppleScript returned an error:" + EndOfLine + "*** Unknown ***"
    Else
      e.Message = "AppleScript returned an error:" + EndOfLine + stringForKey(err, "NSAppleScriptErrorMessage")
    End
    Raise e
  Else
    Return stringValue(descriptor)
  End
End Function

and call it via

Dim procName As String = "Xo"
Dim script As String = "tell application ""System Events""" + EndOfLine + _
"set pList To (processes whose name contains """+ procName + """)" + EndOfLine + _
"set pNames To {}" + EndOfLine + _
"repeat With anItem In pList" + EndOfLine + _
"copy (name Of anItem) To the End Of pNames" + EndOfLine + _
"copy {"", ""} To the End Of pNames" + EndOfLine + _
"End repeat" + EndOfLine + _
"set last item Of pNames To {}" + EndOfLine + _
"Return pNames as string" + EndOfLine + _
"End tell" + EndOfLine 

Dim result As String = ExecuteAppleScript(script)

in this example I get following result:

Xojo

Note that I modified your AppleScript to get a comma separated list of matching process names.