AppleScript not working

Deploying to Mac OS? Ask Mac OS specific questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
nextyoyoma
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 37
Joined: Sat Oct 06, 2012 6:29 am

AppleScript not working

Post by nextyoyoma » Fri Nov 09, 2012 11:36 pm

So I have a strange problem (bug?). I have an app that checks to see if the user is running a java application in the foreground (not a browser-embeded web app), and if so, kill it (this keeps kids from playing games while at school). I am using the following applescript to determine whether they have a java app open as opposed to a java web app:

Code: Select all

tell application "System Events" to return the name of every process whose frontmost is true
Then in Livecode, this is what I do:

Code: Select all

//Kill Java apps (non-plugin-based apps only, webapps are safe)
      
      do field "frontApp" as AppleScript //that field is where I store the applescript
      put the result into theApp
      put replaceText(theApp,"[{}" & quote & "]",empty) into theApp //brackets and quotes are around the result, so I remove them
      
      if theApp contains "java" and currentUser is not among the lines of javaExempt then // exempt users are stored in mysql database
         put shell("ps -ax | awk '/[Jj]ava/{print $1}' | xargs kill")
         displayMessage "This app is not allowed on campus." //custom command that uses a separate binary (jamf) to display a message to user
         logEntry "Killed Java App!" //custom command to write to a log file.
      end if
The problem is that when the user first logs in, this doesn't work. Sometimes it will start working after a while, but sometimes it never works UNLESS I talk to System Events through another applescript first. I can either open applescript editor and tell system events to do something, or I can use osascript through the command line, and as soon as I do that, my LC app starts working as intended. Any ideas why this would be and how to work around it?

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: AppleScript not working

Post by Mark » Sat Nov 10, 2012 12:48 am

Hi,

Your script only returns the name of the app that's currently running in the foreground. All other apps, which are running in the background, are not included in this list. The name of the Java app is only returned if the user happens to be working with that Java app right in that moment.

Another small problem is that AppleScript returns a list while you probably want text. Try this small change:

Code: Select all

tell application "System Events" to return (the name of every process whose frontmost is true) as text
and in your LiveCode script use

Code: Select all

replace quote with empty in the App
It seems a little strange to me to quit every app whose name contains "java", but if that's what you want, you can do this completely in AppleScript:

Code: Select all

tell application "System Events"
	set myList to every process whose frontmost is true
	repeat with myApp in items of myList
		set myAppNameStr to the name of myApp as text
		if "java" is in myAppNameStr then
			tell myApp to quit
		end if
	end repeat
end tell
Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

nextyoyoma
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 37
Joined: Sat Oct 06, 2012 6:29 am

Re: AppleScript not working

Post by nextyoyoma » Mon Nov 12, 2012 2:43 pm

Thanks for the reply, Mark. I actually want it to work just as I've laid out. See, I only want to keep them from running standalone java games (like minecraft). I don't want to keep them from using java web apps. The only way I could figure out to make a distinction between the two is that when running a java standalone app, the "frontmost application" is "java" as opposed to the name of the web browser. Since java apps often spawn multiple processes, I need to kill every java process in order to make sure it kills the game.

Thanks for the bit about doing it all in applescript. If I can get applescript working properly then I might consolidate everything.

The problem I'm having is that the AppleScript either doesn't run or doesn't return an accurate value until I've manually run an applescript that talks to "System Events." As soon as I do that, it begins returning the correct values.

EDIT: After adding a little debugging to my app, the result of the Applescript is "compiler error." After running

Code: Select all

"osascript -e 'tell application "System Events" to activate"
over Apple Remote Desktop, the result of the AppleScript is correct ( the frontmost app.).

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: AppleScript not working

Post by Mark » Mon Nov 12, 2012 3:35 pm

Hi,

You have a single quotation mark missing at the end of the AppleScript syntax.

You could change the script a little:

Code: Select all

tell application "System Events"
	set myApp to every process whose frontmost is true
	set myAppName to the name of item 1 of myApp as text
	if "java" is in myAppName then
		set myList to every process
		repeat with myApp in items of myList
			set myAppNameStr to the name of myApp as text
			if "java" is in myAppNameStr then
				tell myApp to quit
			end if
		end repeat
	end if
end tell
Probably, it would be a good idea to find out why you need to call System Events first? Which version os OSX are you using exactly? Are you logged in as admin or as limited user?

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

nextyoyoma
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 37
Joined: Sat Oct 06, 2012 6:29 am

Re: AppleScript not working

Post by nextyoyoma » Mon Nov 12, 2012 3:51 pm

Typo on my part, sorry. I'm running the command correctly.

This machine is on 10.7.4. The issue occurs with both admin and non-admin users.

The root of the problem is clearly that I have to talk to system events from somewhere else before I can talk to it from LiveCode. I have no idea why this is the case.

EDIT: Another update: I tried saving the applescript as a file and including it with the app, but the issue is basically still happening. Now, the result of the command is "execution error: an error of type -10810 has occurred." Again, when I talk to "System Events" through applescript editor or any other means, the app starts working. This is rather infuriating. Can anyone offer some insight as to why this might be? I am at a loss.

nextyoyoma
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 37
Joined: Sat Oct 06, 2012 6:29 am

Re: AppleScript not working

Post by nextyoyoma » Tue Nov 13, 2012 1:52 am

Ok, so I finally figured out the problem, and I don't think it's a LiveCode issue. It appears that you for some reason, when running an applescript that talks to system events from LC, the system events process must already be running AND owned by the logged-in user. I don't understand why, but that's the only way I could get it to work correctly. I'm using this to open it:

Code: Select all

do shell("su" && currentUser && "-c" && quote & "open -a /System/Library/CoreServices/System\ Events.app" & quote)
I tried "open process" but it did not work, presumably because my app is running as root (it's a launch daemon) and so the process is owned by root and doesn't have the desired effect.

Again, I don't understand why this works this way, but apparently it does!

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: AppleScript not working

Post by Mark » Tue Nov 13, 2012 1:58 am

Hi,

OK, I understand it now. I wonder if it would help if you executed

Code: Select all

$ chown root <path to app>
in the terminal to change the owner of all the apps and applets you're using. On the other hand, if it works now, then don't change it :-)

I suppose you can now use either your original solution or the pure AppleScript solution.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

nextyoyoma
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 37
Joined: Sat Oct 06, 2012 6:29 am

Re: AppleScript not working

Post by nextyoyoma » Tue Nov 13, 2012 4:44 pm

I'm about to tear my hair out over this. My solution still has major problems. If a user logs out of his account and logs into another (rare but it happens), the app stops working again. When this happens even restarting the app doesn't fix it.

I have spent 2 days trying to get this to work now. Does anybody have any idea why there is such a big problem communicating with System Events from within LC?

EDIT: Ok, more info. The issue only happens when the app is loaded as a launch daemon. I confirmed it with a sample stack. And it also does not happen with a shell script that is running as a daemon. This is rather complicated, but here goes:

I created a standalone app that has a field with the applescript and a button to run it, and the result it displayed below it. Compiled as a standalone app. The app works fine when run normally. The app works fine when run as root (sudo <binary inside app bundle>).

Created a plist to load the app as a launch daemon. Loaded it with launchctl (sudo launchctl load -F <plist>). App opens fine, but gets errors when running the applescript unless "System Events" is already running and owned by logged in user.

To confirm that the issue was specific to LC and not just demonized processes in general, I wrote a short shell script to do the same thing:

Code: Select all

#!/bin/sh
sleep 5; osascript -e 'tell application "System Events" to return the name of every process whose frontmost is true' >> test.txt
Then I made a plist to load the script as a launch daemon. Loaded it with launchctl (sudo launchctl load -F <plist>). It works fine, outputs the frontmost process into the text file every 5 seconds.

So what this tells me is that a shell script daemon can talk to system events on its own, but LC cannot (under these circumstances). I'm sure there is a good reason for this, but it is beyond my understanding. If anyone knows of a solution, I'd appreciate it, but I know it's a long shot!

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: AppleScript not working

Post by Mark » Tue Nov 13, 2012 11:42 pm

Hi,

Why do you use LiveCode for this? A daemon that starts an AppleScript app with root permissions will do it. You have all the components for this already and you seem to know how to do this, so why don't you just take LC out of the equation?

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

nextyoyoma
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 37
Joined: Sat Oct 06, 2012 6:29 am

Re: AppleScript not working

Post by nextyoyoma » Tue Nov 13, 2012 11:47 pm

It may come to that. The reason I don't want to do that is that I only want to run this action under certain conditions (e.g., Student is on campus, it's during school hours, and an exempt user such as an administrator isn't logged into their computer.) So it would mean doing all that in AppleScript, too. In the end, though, it may end up being the way to go. Right now I'm experimenting with open process (unsupported, I know) to do it.

Post Reply

Return to “Mac OS”