Need Help With Apple Script

I am trying to make an apple script that will detect when an application (minecraft) is open and then bring up a dialog that wont go away so that only I.T. can remove it.


It's a school project that im working on, we have to figure out a way (through applescript) to help students be more productive, everyone else has no experience with apple script but me. I managed to create this in a couple of minutes but there is a couple glitches and problems i cant solve.


here's the script:


repeat

if appIsRunning("Minecraft") then

display dialog "Gaming is not allowed, Your log of your computer programs running has been emailed to the I.T. department. Please visit the I.T. department immedietly to remove this message" buttons {"Report To I.T."} default button 1 with title "Gaming Alert" with icon stop

end if

tell application "Minecraft"


quit

end tell

end repeat

on appIsRunning(appName)

tell application "System Events" to (name of processes) contains appName

end appIsRunning

end


it works ok...

exept for the fact it works on every application exept minecraft...

Minecraft just freezes and then the script just says error.


Im also trying to design a way that applescript will send an email to the school I.T. desk with the students user name. I have this so far...


tell application "Mail"

set theNewMessage to makenewoutgoing messagewith properties {subject:"Gaming Alert [username here]"


, content:"[username here] has been gaming, he is to report to I.T. by the end of the day", visible:true}

tell theNewMessage


makenewto recipientat end of to recipientswith properties {address:"itdesk@blahblahblah.net"}


send

end tell

end tell


exept the school uses office 365 mail not inbuilt mail, is there any way to make this work?


My key issues are


1. how to fix the bug

2. how can i combne the scripts?

3.will this work for other games?

4.Can this work with online email bases

5.any other genreral comments on the scripts.

MacBook, Mac OS X (10.7.2), Using iTunes

Posted on Jun 15, 2013 3:28 AM

Reply
3 replies

Jun 15, 2013 8:57 AM in response to steve's assistant

AppleScript is probably the wrong tool to use in this situation. There isn't really a way for a user script to prevent the dialog from being dismissed (or the script itself from being quit, for that matter), especially if the user can install and/or run any arbitrary application. Something like this would need to to operate at the system level, such as parental controls or other server client management.

Jun 15, 2013 4:24 PM in response to red_menace

red_menace wrote:


AppleScript is probably the wrong tool to use in this situation. There isn't really a way for a user script to prevent the dialog from being dismissed (or the script itself from being quit, for that matter), especially if the user can install and/or run any arbitrary application. Something like this would need to to operate at the system level, such as parental controls or other server client management.

Even if the dialog can be dismissed, is there any way i can do the following :


1. how to fix the bug

2. how can i combne the scripts?

3.will this work for other games?

4.Can this work with online email bases

Jun 15, 2013 7:08 PM in response to steve's assistant

  1. You didn't provide any information about the error, so I can't verify it since I don't have MineCraft to test. I can't see any reason off hand why the application would hang, although your script will wait until the dialog is dismissed before continuing with the rest of the script.
  2. As hinted to above, you will need to quit the applications and send the email before putting up the dialog, since the script/application will wait while the dialog is up.
  3. Just add other application names to check for.
  4. To use other email clients you will need to use whatever scripting commands or other API they provide.


The following script application should combine your previous examples and allow for multiple applications - save it as a stay-open application. You should also add a LSUIElement key set to true in the application Info.plist so that it doesn't show a menu or appear in the Dock.



propertyillegalApps : {"MineCraft"} -- add application names as needed

propertylongName : missing value

propertyshortName : missing value


onrun-- setup


tell (getsystem info)

setlongNametolong user name-- for use in the email message

setshortNametoshort user name-- for use in the shell script


endtell

endrun


onidle-- repeatedly executes after the run handler completes


tellapplication"System Events"tosetappListtogetnameofprocesseswhosebackground onlyisfalse


seteditedListto {}


repeatwithanAppinappList-- filter for illegal apps

setanApptocontentsofanApp

ifanAppisinillegalAppsthensetendofeditedListtoanApp


endrepeat


ifeditedListisnot {} thennotifyAndQuit(editedList) -- illegals found


return60-- do it again in 60 seconds

endidle


onnotifyAndQuit(quitList)


tellapplication"Mail"-- send mail

settheNewMessagetomakenewoutgoing messagewith properties {subject:"Gaming Alert [" & longName & "]", content:"[" & longName & "] has been gaming, he is to report to I.T. by the end of the day", visible:true}

telltheNewMessage

makenewto recipientatendofto recipientswith properties {address:"itdesk@blahblahblah.net"}

send

endtell


endtell



repeatwithanAppinquitList-- kill user processes with extreme prejudice

do shell script"killall -9 -u " & shortName & " -c " & quoted formofanApp


endrepeat



activateme


display dialog"Gaming is not allowed, Your log of your computer programs running has been emailed to the I.T. department. Please visit the I.T. department immedietly to remove this message"buttons {"Report To I.T."} default button1with title"Gaming Alert"with iconstop

endnotifyAndQuit

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Need Help With Apple Script

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.