Popup notification using applescript?

Hi,

I use the Dropbox a lot to transfer files into my sister’s account on my iMac. Can i use applescript to make a popup notification, which will popup when she logs in, to tell her i’ve placed something in her dropbox?

Thanks,

Ronan

Model: iMac PowerPC
Browser: Firefox 1.5.0.1
Operating System: Mac OS X (10.4)

It’s possible, but not trivial. I assume you don’t both use the machine at once so one of you is (possibly both are) logged out. You’d each have to have a script app that ran when you logged in and looked at a preference file that both of you could access. When you dropped something in the box, the pref would be set (or when she did) and when the other logged in, their script would read the file and see the change, then clear the file. You could also do it with a text file stored somewhere in the system files with permissions set so both of you could read it. However you do it, you need a file to store the info when the machine is off.

I thought that too, at first. (I was making it more complicated then it needed to be.)

However, this should work well (after you point it to the right folder):

property theFolder : alias "Mac mini HD:Users:bruce:Public:Drop Box:"
property oldFileList : {}

set newFileList to list folder theFolder without invisibles

if newFileList ≠ oldFileList then
	set oldFileList to newFileList
	display dialog "There have been changes in your Drop Box." buttons {"Show Drop Box", "OK"} default button 2
	
	if (button returned of result) is "Show Drop Box" then
		tell application "Finder"
			activate
			open theFolder
		end tell
	end if
end if

Hey,

thanks a lot for your help. Had a few problems but changed it around a bit and got it to work for the most part.

I saved it as a folder action script and then added it as a folder action to her drop box. It works perfectly if she is logged on. i.e. if we’ve used that ‘quick change’ between user, little man in the top-right of screen thing, and i’m on my account. If i deposit… she gets the dialog when she switches back.

Here’s what i have at the moment:

property theFolder : "HD:Users:Mari:Public:Drop Box:"
property oldFileList : {}

on adding folder items to theFolder
	
	set newFileList to list folder theFolder without invisibles
	
	if newFileList ≠ oldFileList then
		set oldFileList to newFileList
		
		display dialog "Yo, I put whatever you were lookin for in your Drop Box" buttons {"Show Drop Box", "OK"} default button 2
		
		if (button returned of result) is "Show Drop Box" then
			tell application "Finder"
				activate
				open theFolder
			end tell
		end if
	end if
	
end adding folder items to

is there any way for it to work if she logs off completely?

Thanks again,

Ronan

Why not have my script (saved as an application) run when she logs on?

Also, you could simplify your folder action:

on adding folder items to theFolder
	display dialog "Yo, I put whatever you were lookin for in your Drop Box." buttons {"Show Drop Box", "OK"} default button 2
	
	if (button returned of result) is "Show Drop Box" then
		tell application "Finder"
			activate
			open theFolder
		end tell
	end if
end adding folder items to

You don’t need to compare lists of files, because the action will fire whenever something is added. My script uses lists of files because a simple number wouldn’t be reliable.

if i do as you first sugested. It will run regardless of whether i have sent her anything. Can I tell the script to create an app that will run when she logs in?

Thanks for all your help,

Ronan

IMHO, this is a perfect problem for an on idle handler and a preferences file. That way whenever something is added to the folder, it is added to the list (as Bruce suggested) and written to the pref file. If things are removed, they’re removed from the prefs file too. Then when either party logs on, this script (saved as stay open app and put in the user’s startup apps) runs, gets the list, compares with the contents, etc. If the other party has remained logged on, the idle handler looks after things when files are added. I haven’t thought this out enough to suggest code, but it is doable.

hmm, ok

so can you give me the location of this pref file or do i have to create one? In that case can you explain how it’s done?

Just to clarify,

you’re saying that when i modify (i.e. add/remove files) the drop box folder, a folder action script runs which edits a pref file.

Then when my sis logs in, i create an app that auto runs when she logs in, which compares that list with the contents. If they’re different, it runs a popup script, similar to what i’ve already written?

thanks,

Ronan

Ronan, is there an particular reason why the app can’t just run (quickly) when you sister logs in?

The benefit of a preference file would be that the login app and folder action would compare the same list.

Adam, I’m not sure what the point is in using an idling app. Can you clarify that?

Principally that I have very little luck with Folder Actions - they seem so flakey that I prefer to have an idle handler watch the folder.

Right so how i go about that?? What’s a pref file?

It’s a text file to save stuff in that you refer to later to find out what’s there. Here’s the one I use (by hhas, whose initials you’ll see here in the bbs):

property _prefs : {foo:1, bar:"hello"} -- [a record containing your persistent preferences]

on _prefsFile()
	return POSIX file (POSIX path of (path to preferences) & "MyAppletPrefs") -- [your filename in place of MyAppletPrefs"]
end _prefsFile

on loadPrefs()
	try
		set _prefs to read _prefsFile() as record
	on error number -43 -- file not found; use default values
	end try
end loadPrefs

on savePrefs()
	set f to open for access _prefsFile() with write permission
	write _prefs to f as record
	close access f
end savePrefs

To use it you set “prefs to whatever you want to save as a record (example at the top) then invoke savePrefs() which will put that in the text file. To get the value back, loadPrefs(). Since the file will sit there if the applet is quit, it’ll be there next time you run it. If you don’t loadPrefs() again early on, the record will revert to whatever is set in the property.

ok not really sure what’s goin on there. What tould i have to chang if i wanted to check my sister’s dropbox. would ‘bar’ become ‘DropBox’ and ‘MyApplePrefs’ become, ‘CheckFolder’ assuming checkFolder is the text file i’m writing to?

what does this actualyl script do? does it create a pref file with a list of files or what?

sorry for all the questions, i appreciate your patience.

Ronan.

hey, any ideas guys?