continuously run script that checks if maya is running, if yes...

hey all,

I have written a script “fn” that checks and unchecks the “Use all F1, F2, etc. keys as standard function keys” checkbox, right now I just run it as an application that i put in my dock and finder menu bar, and have been very happy with it so far, it works very quickly, and remains in the background. It also checks if GUI scripting is enabled, if not, then it will simply ask for the Admin password to enable it, if yes, then it moves on to the script.

(*
fn_1.1

Written by circa86 in 2008

This script will simply open the "System Preferences" App, open the Keybaord Preferences Pane, and check/uncheck the "Use all F1, F2, etc. keys as standard function keys" checkbox.  If "System Preferences" is already open it will switch to the Keyboard Pane and check the same checkbox.  After the box is checked/unchecked, System preferences will quit.

The reason for this script comes from the use of Applications like Maya where F1-12 fn keys are extremely important.  I found that I hated going into system preferences to check/uncheck the specific preference each time I wanted to switch the functionality, so this was an excellent solution.
*)

--Check if GUI Scripting is Enabled
tell application "System Events"
	if not UI elements enabled then
		set UI elements enabled to true
	end if
end tell

--Enable/Disable "Use all F1, F2, etc. keys as standard function keys" option in Keyboard & Mouse Preference pane and close System Preferences
tell application "System Events"
	tell application "System Preferences"
		reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
	end tell
	click checkbox "Use all F1, F2, etc. keys as standard function keys" of tab group 1 of window "Keyboard & Mouse" of application process "System Preferences"
end tell
if application "System Preferences" is running then
	tell application "System Preferences" to quit
end if

I would really like to take this a step further and create a script that knows when Maya is running or not, if yes, then the “Use all F1, F2, etc. keys…” checkbox will be checked, if no, then the checkbox will be unchecked. I believe this may be possible but I could really use some help.

also, if I can take that one more step further and create a script that knows what application is in the foreground, if Maya is in the foreground, then check the box, if not then uncheck the box. although that may be somewhat obtrusive as it would still take a moment to run each time I would switch foreground windows, so maybe that is not the best idea.

I am guessing for either of these to function I would need a script to continuously run, is that a bad idea? I am sure that will use atleast some system resources correct?

thanks for taking the time to look over my questions, I appreciate any help.

You can check whether an application is currently running or not with a function like follows:


-- I am indicating if a given application is currently running or not
-- only the (full) application name must be given, e.g. "Address Book"
on appisrunning(appname)
	tell application "System Events"
		set processnames to name of every process
	end tell
	if appname is in processnames then
		return true
	else
		return false
	end if
end appisrunning

In addition you can detect the name of the current frontmost application with this code:


-- I am returning the name of the current frontmost application
on getfrontmostappname()
	tell application "System Events"
		set frontmostapps to every application process whose frontmost is true
		set frontmostappname to name of item 1 of frontmostapps
	end tell
	return frontmostappname
end getfrontmostappname

If you use those code snippets inside an «on idle» handler, which enables your script to continuously monitor the Maya application when the script is saved as an Stay Open application, you get the following code block:


on idle
	set frontmostappname to my getfrontmostappname()
	if my appisrunning("Maya") and frontmostappname is equal to "Maya" then
		tell application "System Events"
			if not UI elements enabled then
				set UI elements enabled to true
			end if
		end tell
		--Enable/Disable "Use all F1, F2, etc. keys as standard function keys" option in Keyboard & Mouse Preference pane and close System Preferences
		tell application "System Events"
			tell application "System Preferences"
				reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
			end tell
			click checkbox "Use all F1, F2, etc. keys as standard function keys" of tab group 1 of window "Keyboard & Mouse" of application process "System Preferences"
		end tell
		if application "System Preferences" is running then
			tell application "System Preferences" to quit
		end if
	end if
	-- check every 15 seconds
	return 15
end idle

-- I am indicating if a given application is currently running or not
-- only the (full) application name must be given, e.g. "Address Book"
on appisrunning(appname)
	tell application "System Events"
		set processnames to name of every process
	end tell
	if appname is in processnames then
		return true
	else
		return false
	end if
end appisrunning

-- I am returning the name of the current frontmost application
on getfrontmostappname()
	tell application "System Events"
		set frontmostapps to every application process whose frontmost is true
		set frontmostappname to name of item 1 of frontmostapps
	end tell
	return frontmostappname
end getfrontmostappname

You might still need to tweak it to your very own requirements :wink:

wow thank you very much! I learned a great amount from that single post, very very helpful.

i went ahead and tried that script out as a Stay Open App, and the result is that it continuously will toggle the option on or off, i set the return to a short amount of time to double check this. which is what it seems like it should be doing looking at the script.

I am not really sure if having a stay open App constantly running will be the best solution, but for it to function fully, it would need to check if Maya is running, and if Maya is the frontmost App, if yes then it should run the rest of the script to check if the “Use all F1, F2 keys…” option is “checked”, if yes, then do nothing, and close System Preferences, if unchecked, then check, and close system preferences.

the biggest problem with something like this is that it will constantly be running and checking, and opening/closing system preferences, correct?

I truly appreciate the help as it will definitely help me continue to develop, but I am thinking it may function better as a stand alone app that i launch when I need.

it would be nice to have it set up so whenever it runs that it checks if Maya is running, if yes then it opens the system preference pane and checks to see if the option is enabled, if enabled, then it closes system preferences. without changing the setting.

if Maya is not running, then it opens the System Pref pane, checks to see if the option is enabled, if enabled, then it unchecks the checkbox, disabling the setting, and closes system preferences. that way I could just run the App when I need and I know it will choose the right functionality without having to test it out afterwards.

thanks again for the help.