Front most user and background user ....

Hi,

If a user is in background (not the front most user) but running, I want to quit an open application in that user. Is that possible ?

Thanks in advance.

Robert Lespérance

Hi,

you can do this with Remote Apple Events (which must be enabled in “Sharing”)


property user_name : "user" -- admin username
property pass_word : "¢¢¢¢¢" -- admin password
property uid : "503" -- user id of remote user
property appToQuit : "iTunes"

if check_user() then
	set s to get_eppc_App(appToQuit)
	quit application s
end if

on get_eppc_App(A)
	set host_name to host name of (system info)
	return "eppc://" & user_name & ":" & pass_word & "@" & host_name & "/" & A & "?uid=" & uid
end get_eppc_App

on check_user()
	using terms from application "Finder"
		try
			set remoteFinder to get_eppc_App("Finder")
			tell application remoteFinder to get (desktop as string)
			return true
		on error
			return false
		end try
	end using terms from
end check_user

Bonjour StefanK,

Thanks for the script. As usual it works and does what I want. Now for me to understand what each step is for. This one seems tougher than others …

Can you explain me what each step is doing ? I don’t understand what the «on check_user()» is doing at all … As for the «on get_eppc_App(A)» I understand that it is getting acces to the remote user by giving the admin name and password, but it not clear what exactly it is saying.

Could I ask you to help me through ?

Regards.

Robert

Bonjour Robert,

here the script with comments.
Hope it helps to understand


property user_name : "user" -- admin username
property pass_word : "¢¢¢¢¢" -- admin password
property uid : "503" -- user id of remote user
property appToQuit : "iTunes"

if check_user() then
	-- check if user with uid 503 is logged in and get the remote URL of the application appToQuit
	set s to get_eppc_App(appToQuit)
	-- send a Remote Apple Event to quit the application
	-- as quit is a global command, no using terms from block is required
	quit application s
end if

on get_eppc_App(A)
	-- get the bonjour name of the computer, which is the name in PrefPane Sharing with ".local" extension
	set host_name to host name of (system info)
	-- compose the URL for the requested application (A) of the remote user (syntax e.g. eppc://user:pass@myComputer.local/Finder?uid=503)
	-- in this case the Finder of user with uid 503
	return "eppc://" & user_name & ":" & pass_word & "@" & host_name & "/" & A & "?uid=" & uid
end get_eppc_App

-- check if the requested user is logged in by retrieving its path to the desktop folder
-- if the user is currently logged in, the handler returns true, if not, an error occurs returning false
on check_user()
	-- using terms is required because the argument of application is no literal string
	using terms from application "Finder"
		try
			set remoteFinder to get_eppc_App("Finder")
			tell application remoteFinder to get (desktop as string)
			return true
		on error
			return false
		end try
	end using terms from
end check_user

Great … You are a genious. Thank you.

And now to get this a little bit more complicated, I would like to use «Keychain Scripting» to get acces to my admin name and password instead of displaying it in the script. I created a key in my «session» keychain with the admin name and privilege. I named this key «AccesAdministrateur». I found this script that can give me access to my admin information:

tell application "Keychain Scripting"
		set admKey to first generic key of current keychain where name is "AccesAdministrateur"
		set admName to account of admKey
		set admPassword to password of admKey
end tell

I would like to write a handler that will pass the admName and admPassword to your script. I tried but don’t seem to find what is missing.

Regards.

Robert

Got it … This is the handler I added to your script:

property admKey : ""
property admName : ""
property admPassword : ""

-- Call handler to retrieve «admin» datas from keychain
adminDatas()

-- Handler to retrieve the «admin» name and password from the «adminKey» in your «session» keychain
on adminDatas()
	tell application "Keychain Scripting"
		set admKey to first generic key of current keychain where name is "adminKey"
		set admName to account of admKey
		set admPassword to password of admKey
	end tell
	result
end adminDatas


Don’t forget to create, if it does not exist, a new key in your «session» keychain with admin name and password.

It was not working because I was using SET statements. I change it for PROPERTY and it worked. Now I have to understand what is the subtil difference.

Regards.

Robert

Hi StefanK,

When trying to quit a remote application, how can check that this application is already running in order to avoid an error message ?

Robert

Finally got it … I have been working for a while but was missing a line. Here is my handler:

on CheckUserApp(userApp)
	using terms from application "Finder"
		try
			set remoteApp to accesRemoteApp(userApp)
			if remoteApp exists then
				return true
			end if
		on error
			return false
		end try
	end using terms from
end CheckUserApp

Just had to rewrite the script you submitted a little bit … Thanks.

Regards.

Robert

Just replied to fast … I want to know if the remote app is running, not to know if it exists … Is that possible ?

Although it’s not documented, the Finder can also retrieve the processes like System Events.
As you can’t target any not running application on a remote machine, this is the proper way


property theApplication : "iPhoto"

on checkApplicationRunning(remoteMachine)
	tell application "Finder" of machine remoteMachine
		using terms from application "Finder"
			return (theApplication is in (get name of processes))
		end using terms from
	end tell
end checkApplicationRunning

Thank you. Where would I be without you :^) :^):^):^):^):^):^):^):^):^):^):^):^):^)

Hi StefanK,

You gave me this script/handler:

property user_name : "user" -- admin username
property pass_word : "¢¢¢¢¢" -- admin password
property uid : "503" -- user id of remote user
property appToQuit : "iTunes"

if check_user() then
   set s to get_eppc_App(appToQuit)
   quit application s
end if

on get_eppc_App(A)
   set host_name to host name of (system info)
   return "eppc://" & user_name & ":" & pass_word & "@" & host_name & "/" & A & "?uid=" & uid
end get_eppc_App

on check_user()
   using terms from application "Finder"
       try
           set remoteFinder to get_eppc_App("Finder")
           tell application remoteFinder to get (desktop as string)
           return true
       on error
           return false
       end try
   end using terms from
end check_user

Am I correct to say that your «check_user()» handler checks if the user exists but not if it is logged in ?

Regards.

Robert