Wednesday, August 16, 2017

#1 2017-08-05 05:59:35 pm

emendelson
Member
Registered: 2008-10-31
Posts: 354

Test if application was launched by another user?

I've written an AppleScript application that launches another application - but uses System Events to test whether the other application is already running before it launches that other application. If the other application is already running, the AppleScript performs a different task instead of launching the other application.

Now I'm trying to test whether the application (1) is running and (2) was launched by another user in a multi-user system. If (2) is true, then I want to display a message. But I can't figure out how to determine whether another user launched the application, or whether the current user launched it.

Does anyone know the solution to this problem? I'll be grateful for any help.

Offline

 

#2 2017-08-05 09:20:58 pm

emendelson
Member
Registered: 2008-10-31
Posts: 354

Re: Test if application was launched by another user?

I think the answer to this question may be something like this.

If the application that you're testing is running, then get the current username (perhaps with do shell script "whoami") and then run this shell command:

ps -U username | grep ApplicationName | grep SomeOtherString



"SomeOtherString" should be some string that occurs in the output only when you use ps -U with the name of the user who owns the process, and can be found by experiment.

This seems to work reliably, but of course I don't really understand what's going on, and there must be a more efficient and foolproof method. I'll be grateful for any advice.

Offline

 

#3 2017-08-06 01:50:24 pm

emendelson
Member
Registered: 2008-10-31
Posts: 354

Re: Test if application was launched by another user?

This really dreadful kludge seems to get the job done, but it's obviously the work of a total incompetent (me). The crucial thing is finding the string in the second grep that identifies that the application is actually running (so that you don't get a false positive simply by testing for the application).

Also, I haven't figured out to make sure that the other user is running the same copy of the application that I'm testing for. There's no reason to block the local application if it isn't the same copy as the one being run by someone else.

Applescript:

set myName to do shell script "whoami"
set allNames to do shell script "users"
set listNames to my stringSplit(allNames, " ")
set deleteMe to {myName}
set otherNames to {}

repeat with i from 1 to count listNames
   if {listNames's item i} is not in deleteMe then set otherNames's end to listNames's item i
end repeat

set activeOther to false
repeat with i from 1 to count otherNames
   set appFound to do shell script "ps -U" & space & otherNames's item i & space & "| grep SheepShaver | grep psn | awk '{print $1}'"
   if appFound is not "" then set activeOther to true
end repeat

set activeHere to false
if application "SheepShaver" is running then set activeHere to true

set otherString to "being run by another user"
if activeOther is false then set otherString to "NOT" & space & otherString

set localString to "being run by you," & space & myName
if activeHere is false then set localString to "NOT" & space & localString

tell me to activate
display dialog "SheepShaver:" & return & return & "is" & space & otherString & return & return & "is" & space & localString buttons {"OK"}

on stringSplit(theString, theDelimiter)
   set oldDelimiters to AppleScript's text item delimiters
   set AppleScript's text item delimiters to theDelimiter
   set theArray to every text item of theString
   set AppleScript's text item delimiters to oldDelimiters
   return theArray
end stringSplit

Last edited by emendelson (2017-08-06 02:02:46 pm)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)