Imbeded tokens inside a custom function

I’m at a loss trying to get a custom function to work. Using basic code this will produce true if the application is running or false if it is not.

letfileglobal x="Mail.app"
message ?(shellscript( {ps ax | grep -v grep | grep $«x»$}) contains x,-1,0)

If I adapt this for a custom function like this:

 registercustomfunction "APPRUNNING",1,||| ?(shellscript( {ps ax | grep -v grep | grep $«•1»$}) contains •1,-1,0)|||

Then testing it with apprunning(“Mail.app”) always produces a false return no matter what application is entered as a parameter. From the best that I can determine, the use of a token embedded inside another token is the problem ($«•1»$). I have tried multiple variations using execute( and cache( functions to try and overcome this but nothing does the trick.

Any ideas? :thinking:

Before discussing the problem of embedded tokens, let me point out that Panorama 10.2 already has a function that can be used to determine whether an application is running or not. This function is written in Objective-C so it doesn’t rely on a shell script.


The reason why your tokens don’t work is that they aren’t tokens, they are just characters in a string. So this formula contains two tokens:

•1+•2

But this formula doesn’t contain any. Tokens cannot be inside a string constant.

{•1+•2}

To make this work, you would need to “explode” the formula like this:

quoted(•1)+{+}+ quoted(•2)

In your case, I think you would need to use unixshellstring( instead of quoted. And I think the second embedded parameter token is ok, since it’s not inside a string constant (other than the fact that the entire formula is in a string constant, but that is removed as the custom function is processed).

registercustomfunction "APPRUNNING",1,||| ?(shellscript( {ps ax | grep -v grep | grep }+unixshellstring(•1)) contains •1,-1,0)|||

I haven’t tested this, but I think it should get you on the right track. Though I think it would be even better to use runninappinfo( unless you need this to work with Panorama 10.1.

Yes, it works fine like this - thanks!

I must have missed that puppy along the way. My version is single purposed just to see if the application is running and uses the application file name instead of having to get the bundle id from the plist or getting it via the terminal or shellscript. So, I might still implement mine just for my personal use.

FYI, you should only have to figure out the bundle id once. It usually doesn’t change when new versions are released, and it would still work even if the app was renamed.