Two interacting scripts: Variable problem

I cant get my two scripts to share a variable. Applescript seemingly can only execute one command at a time in order per application. But I need it to listen while blocking everything else the user might do.

My little brother attempts to destroy everything when I go to the loo, and he fights me when I’m trying to work, so I made a script to lock the computer when I say “Lock” and so on.

Here are the 2 scripts:


property beepo : "0"
tell script "Key.app"
	run
end tell
tell application "SpeechRecognitionServer"
	repeat
		listen for "Lock"
		if the result is "Lock" then
			exit repeat
		end if
	end repeat
	set beepo to "2"
	repeat
		listen for "Lock escape."
		if the result is "Lock escape." then
			exit repeat
		end if
	end repeat
end tell
set beepo to "1"


set beepo to "0"
repeat
	tell script "Lock"
		set beepo to it's beepo
	end tell
	if beepo is "2" then
		exit repeat
	end if
end repeat
repeat
	activate
	beep 1
	tell script "Lock"
		set beepo to it's beepo
	end tell
	if beepo is "1" then
		exit repeat
	end if
end repeat

The first script is the interactive one while the 2nd is the locking script. I want them to be able to share the variable “Beepo”.

Model: iMac
AppleScript: 2.1.1
Browser: Safari 417.9.3
Operating System: Mac OS X (10.4)

Hello

I don’t know if it is the most efficient formula but it works.

Insert the code of your two scripts in the passed skeleton

set leDossier to (path to preferences from user domain) as Unicode text
set nomDuFichier to "beppo.txt"
set leFichier to leDossier & nomDuFichier
tell application "Finder"
	if exists file leFichier then
		set beppo to (read file leFichier from 1) as text
	else
		make new file at folder leDossier with properties {name:nomDuFichier}
		set beppo to "0"
	end if
end tell
(*
your code *)
set beppo to "3" -- just for testing

set fileID to open for access file leFichier with write permission
set eof of fileID to 0
write beppo to fileID starting at 1
close access fileID


Script_1 will store beppo in the “beppo.txt” file
Script_2 will read beppo from the “beppo.txt” file then will store beppo in the same file on exit
Script_1 will read beppo from the “beppo.txt” file then will store beppo in the same file on exit
and so on.

Yvan KOENIG (from FRANCE samedi 14 octobre 2006 13:48:18)

Hi Fistoprince,

don’t think using a temp file is necessary - it is possible to let two script applications ‘talk’ with each other.
Your ‘lock’ script should use an on idle loop like this (save as stay open application “lock” & start it from finder):

global beepo
set beepo to "0"

on idle
	if beepo ≠ "0" then
		display dialog "beepo was changed to " & beepo
		quit
	end if
	return 1
end idle

on getBeepo()
	return beepo
end getBeepo

on setBeepo(var)
	set beepo to var
end setBeepo

then you can set and read the beepo variable from an other script:

to read it:

set myBeepo to (script of application "lock")'s getBeepo()

to set it:

(script of application "lock")'s setBeepo("new value")

D.

Hi,

What will stop your brother from saying “Lock Escape”. Can he talk yet? There are better ways to lock your computer on the fly. Or better yet, out think your brother.

Note that using repeat loops to wait for user interaction is not a good idea.

gl,

Well that puts my rapidly approching 40th in perspective. I sure don’t miss those days.

You can lock your screen but leave the sound on. Under the “General” pane of the preferences for Keychain Access is a checkbox for “Show status in menu bar.”

The first item in the dropdown menu under the lock icon is “Lock screen.” You could alter your script to trigger that, but you’ll still need to manually enter your password to unlock the screen.

Remember to disable fast user switching or there will be a “Switch User” button in the unlock dialog box. Your brother would’t be able to affect your account without your password, but he could bring up the login screen, muting your sound.

Thanks for all your feedback. About the escape lock thing, my brother has a problem with his “Parts”. His voice is too high to be reconised by the computer.