Detecting application running

Right up front I have to admit I know almost nothing about AppleScript. I can read a script and know what is going on but I do not write any scripts.

I have a perl script that was written for me to use. The script listens to a port that to see if a print job has been sent to the billable printing system. It check s afew things in the print steam. But what it is meant to do is to activate an Applescript to open Safari to a web page that allows the students to charge their print jobs. The script has to run all the time, I have only gotten it to work in the terminal but if a student using the terminal for telnet then they usually quit the termnal and the script dies. I have tried to make it work as a StartupItem but wouldn’t work right. I tried to make it a login hook but then you never got logged in because the script never ends.

So what I am looking for is to make an Applescript that runs in the background listening either to a port or detecting when Print Center launchs. Then the script would open Safari with the web page.

Is this possible? If it is how do you make AppleScript listen for a port or at least detect when an app is running? I am not asking for a completed script but direction so that I might be able to figure out how to make this script work. Thanks.

Listening to a network port is complex. There was (is?) an OSAX called TCP Scripting Addition for Mac OS 9.x which handled this, but there’s no direct equivalent for Mac OS X. Theory states you should be able to patch something together using xinetd, but the application check is probably easier.

The application “System Events” will give you a list of running processes, and you can compare that by doing something like:

tell application "System Events"
   set runningApps to name of every application process
   if runningApps contains "Print Center" then
     -- Print Center is running, so do your stuff
   end if
end tell

To make your script run continuously, you need to add an ‘on idle’ handler but I’m guessing you have that part since you didn’t mention a problem with making your script run in the background.

Here’s a simple example (save as stay open application) that checks every 10 seconds to see if Print Center is running but there are various issues to overcome with this approach.

on idle
	tell application "System Events" to ¬
		set running_ to exists process "Print Center"
	if running_ is true then
		--display dialog "It's running"
		tell application "Safari"
			make new document at end of documents with properties {URL:""}
		end tell
		--it's not running
	end if
	return 10 -- seconds between checks
end idle

Known issues with this script:

  1. Safari will continue to open new windows as long as Print Center is running.

  2. On my machine, Print Center stays open once launched. I don’t like this and I don’t know if it is the default behavior.

3 thru ?.. Reserved for undiscovered flaws.

I’m not saying that these issues cannot be overcome but it isn’t as simple as it might first appear.

– Rob

I realized after getting a response back that just detecting the app running would not do. I believe that ApleScript can detect status of a print job and that might work but I haven’t tried to figure that out yet. Maybe I will figure out how to write AppleScripts after all trying to do this. Thanks

I was thinkin’ that if there’s a spool/temp folder for print jobs, a folder action could kick Safari into action whenever a new file arrives.

– Rob

I’m not sure if this will help, but you can do a lot via ‘cups’ and a web browser…

I made a Folder Actions AppleScript that is attched to /var/spool/cups. But I have a permissions problem. Cups foler is owned by daemon and is in the group admin. The permissions were rwx------ and I changed them to rwxrwx—. That worked but as soon as I logged out and back in the permissions were changed back to rwx------. Now I guess the script could run with admin privileges but I believe youhave to hard code those in and I don’t want theuser to see the password. Maybe AppleScript is not the best solution for my problem.