Communication between scripts running on 2 different users ...

HI,

I have 2 scripts running on 2 different user. If one script needs input from the other can I send a message/variable from one script to the other ?

Thanks in advance.

Robert Lespérance

Yeah, create an output file in POSIX path “/Shared/”, the shared user folder, AppleScript path

(path to startup disk) as string & "Users:Shared:"

Thank you Sean … Can you be more explicit. I don’t understand what I can do with that comment.

I think they mean. have your script 1 write out the info to a file in the shared user area.
And have script 2 read the file.

Hi,

Thanks for helping. In the «StandardAdditions» dictionnary, I found commands to read/write to files. I tried this script:

set fileName to "Macintosh HD:Users:Shared:" & "Inactivity log"
open for access fileName with write permission
write "yes" as text to fileName
close access of fileName

When executing I get one of those message (translated from french):

¢ The file is already open (if the second statement is active
¢ Impossible to modify the type of the file

What is wrong ? Thanks in advance.

Robert

Yes you almost got it right :slight_smile:
The dictionary is a very important resource.

Try this modification:


-- get the shared path independent of the drive's name
set fileName to ((path to startup disk) as string) & "Users:Shared:Inactivity.log"

-- open access and hold on to the returned identifier
set resourceHandleNumber to open for access fileName with write permission

-- use the identifier and respect the order of arguments as they are in the dictionary
write "yes" to resourceHandleNumber as text

-- again use the identifier not the string in fileName
close access resourceHandleNumber

any better?

Thanks …

I get the same error message: «The file Inactivity log is already open». That file is closed, as far as I am concerned. There must be a flag somewhere …

On the first run of the script, I got a message that said something like this (I cannot get it again because of the above error message): Cannot change «Yes» to text.

I am with Tiger 10.4.11.

Does this script run without error on your machine ?

if fileName is a literal string path the syntax must be file [string]


set resourceHandleNumber to open for access file fileName with write permission

The coercion as text in the write line is useless.
There is a shortcut to the folder /Users/Shared


set fileName to ((path to shared documents as text) & "Inactivity.log")

Hi StefanK,

Thanks … Once the file is created in the «Shared» folder, how can I avoid to have the message «The “Inactivity log” file is already open» every time I run the script ?

And on the first run I get this nerror message: «Cannot change “Yes” to text».

Regards.

Robert

In your original script you’re writing into the literal string, not into the open file.
Open access returns a pointer to the file (resourceHandleNumber in Luke’s script)

Here is a version with a reliable error handling


set fileName to ((path to shared documents as text) & "Inactivity.log")
try
	set resourceHandleNumber to open for access file fileName with write permission
	write "yes" to resourceHandleNumber as «class utf8» -- log files are usually UTF8 encoded
	close access resourceHandleNumber
on error
	try
		close access file fileName
	end try
end try

Hi StefanK,

Thanks for your script … As ever it works perfectly.

Writing to the file only overwrite the actual content. So if the content is 12345 and that I write 999, the result will be 99945 instead of 999. How can I first erase it’s content before writing to it ?

Regards.

Robert


set fileName to ((path to shared documents as text) & "Inactivity.log")
try
	set resourceHandleNumber to open for access file fileName with write permission
	set eof of resourceHandleNumber to 0
	write "yes" to resourceHandleNumber as «class utf8» -- log files are usually UTF8 encoded
	close access resourceHandleNumber
on error
	try
		close access file fileName
	end try
end try

Thank you again … The problem is getting more complex than I thought.

I have 2 identical background script running on 2 different user. Each script calculates the number of active processes that I am monitoring on that specific user and will log it in the «shared folder file». If there is no active processes on ALL users, the computer will be put to sleep after a 10 minutes.

At first, I thought of having each background script logging it’s number of active processes in the «shared folder file», but I need the total of active processes OF ALL USERS. I would need to log UserA and UserB’s active processes, add them and log the total.

Is that possible ? How would you suggest to handle that task ? I am stuck …

Regards.

I have no idea about that.
Doesn’t the computer go to sleep anyway after the specified idle time?

Hi StefanK,

Thanks of reminding me what is the focus of my script.

You are right, OS X’s System Preferences allows auto log out but it also quits all running apps. I want to create a script that logs out without quitting all apps. That will permit any family member that needs the computer to have access to another non pass-worded user without messing in my own user environment.

Before logging out, I also want for specific background running tasks to initiate or complete (Backup, Retrospect, or any other). I also want for others to be monitored (like quitting open Excel spreadsheets because they wont be backed up if they are still open or that needs much CPU when open, or like quitting my accounting software that works on OS 9 and also needs much CPU when left opened, etc …).

I use that script in conjunction with a great utility called Power Manager. It can awake my computer at any given time to authorize specific maintenance activity like doing a scheduled backup or to permit to another great utility, called Indigo, to control the lightning and the electrical consumption in my house. Indigo can shut down lights when not needed, it can increase/decrease heating, and much much more. You can find these 2 utility at:

Indigo: http://www.perceptiveautomation.com/indigo/index.html
Power Manager: http://www.dssw.co.uk/

And most of all, creating that script gives me hours and hours of great and free pleasure and keeps me in touch with generous people like you and Jacques.

This is what is all about my script.

Regards.

Robert

Hm, as far as I know you can’t log out without quitting the apps

Hi Jacques,

Thank you for all your work. It is very generous of you for taking all that time. I think we are getting closer … I want a script that is universal, that is identical for any user and that I can run in background on any user without modification.

That being said, I understand your idea and using a file located in the Shared folder is possible and someway simple. It could even be made invisible later.

Could we change our work scenario like this:

¢ each running script would update the number of processes in it’s specific file (let’s say «ActivityLog501» for user 501, etc …)
¢ when updating it’s log it would also update a file that would add the number of processes of each user (let’s say we call it «ActivityLogTotal»)

Then each script would only have to read the result of the «ActivityLogTotal» file to monitor if there is specific running processes on all users. What do you think about that ?

Regards.

Robert

An alternative would be to read and write the log file as a record, which could have the users’ names (easily-discovered) as labels and could be updated by simple concatenation. There’d be no need for text manipulation and all users could use identical scripts.

on updateInactivityLog(n) -- n is the number of processes of the current user.
	-- Create a single-property record with the current user name as the label and n as the value.
	set newRecord to (run script "{|" & (system attribute "USER") & "|:" & n & "}")
	
	-- Open the log file.
	set fRef to (open for access file ((path to shared documents as Unicode text) & "Inactivity.log") with write permission)
	try
		-- If the file's not empty, concatentate its existing record contents to newRecord.
		-- The normal precedence rules for record concatenation apply.
		if ((get eof fRef) > 0) then
			set newRecord to newRecord & (read fRef as record)
			set eof fRef to 0
		end if
		-- Write newRecord back to the log file.
		write newRecord to fRef
	end try
	-- Close the log file.
	close access fRef
end updateInactivityLog

The time-out checking process (assuming it’s an AppleScript one) would then read the file as record, coerce the result to list, and add up the list’s items.

set processCounts to (read file ((path to shared documents as Unicode text) & "Inactivity.log") as record)
set processCounts to processCounts as list

set totalProcesses to 0
repeat with subtotal in processCounts
	set totalProcesses to totalProcesses + subtotal
end repeat

return totalProcesses

Hi Nigel,

Thanks for reading and helping.

When executing your handler, the Inactivity.log file returns «[b]reco

Hi Nigel,

Thanks for reading and helping.

When executing your handler, the Inactivity.log file returns a bizarre text: «recoursrf listTEXTNouslong», while the result pane of the Script Editor returns «{|Nous|:5}» as you scripted.

Is there any personalization to be done to your routine ? What could be the reason for this akward text ?

Regards.

Robert

P.S. I don’t know for what reason, copying the result of the Inactivity.log file in my post resulted in a truncanated post. I tried it many times with the same result. To work around this problem I had to write it instead of copying it.