Applescripting the Processor Prefpane?

Discussion in 'PowerPC Macs' started by eyoungren, Feb 29, 2016.

  1. eyoungren macrumors P6

    eyoungren

    Joined:
    Aug 31, 2011
    Location:
    Phoenix • 85037
    #1
    Just curious if this can be automated via Applescript/shell script? Perhaps even Automator?

    My son had an energy project for his class tonight which has me thinking.

    Generally I leave my QS on at full power with the screens set to shut off by a certain amount of time. With the type of processors I have I don't have the power setting in the Energy Settings pane to reduce their power draw.

    But I do have the ability to switch one CPU off whenever I feel like it. So, I was thinking some sort of Applescript/shell script/automater feature that would shut off the second CPU whenever my screens go dark. I don't have an issue with doing that if I'm not at the Mac. But for a few reasons I'd rather avoid shutting the Mac off entirely.

    I can automate the trigger, but have no idea where I'd find any scripting commands to control shutting off a cpu.

    Ideally, when getting back on again the script would work in reverse and turn the second CPU back on.

    Even if it's GUI scripting I can do that, I just need an idea of the commands.

    Anyone have any idea?
     
  2. Hack5190 macrumors 6502a

    Hack5190

    Joined:
    Oct 21, 2015
    Location:
    Stuck on Earth in the USA
    #2
    Take a look at Automator (http://www.macosxautomation.com/automator/), it should be able to do what your describing. If you don't like Automator it should be possible to accomplish with Applescript, however you will have to do some reasearch into the required commands.
     
  3. eyoungren thread starter macrumors P6

    eyoungren

    Joined:
    Aug 31, 2011
    Location:
    Phoenix • 85037
    #3
    Well, I started the research, but ran into a Google wall of irrelevant returns which is why I asked here. I'll take a look at Automator tonight though.
     
  4. Hack5190 macrumors 6502a

    Hack5190

    Joined:
    Oct 21, 2015
    Location:
    Stuck on Earth in the USA
    #4
    There is a world-wide-web of info out there that can result in a lot of time chasing unrelated or useless information. Automator has the ability to record manual tasks, and play them back as part of a workflow. In your case, automating the task of opening Sys Prefs and turning off 1 CPU should be easy. (Open Automator, select Automator Help from the Help menu, search for "record." The first article explains how to record actions ;))

    The tricky part will be running your script AFTER the screen saver activates. However you should be able to use a github project called ScriptSaver to help with that (https://swannman.github.io/scriptsaver/).

    Enjoy!
     
  5. eyoungren thread starter macrumors P6

    eyoungren

    Joined:
    Aug 31, 2011
    Location:
    Phoenix • 85037
    #5
    Cool! Thanks! I actually was going to look at Lingon or DSW (Do Something When) but this looks like the right ticket.
     
  6. Hack5190 macrumors 6502a

    Hack5190

    Joined:
    Oct 21, 2015
    Location:
    Stuck on Earth in the USA
    #6
    Wasn't aware of DSW, thanks for the tip.
     
  7. eyoungren thread starter macrumors P6

    eyoungren

    Joined:
    Aug 31, 2011
    Location:
    Phoenix • 85037
    #7
    I didn't know if you were or not, which is why I linked to it. ;)
     
  8. Hack5190 macrumors 6502a

    Hack5190

    Joined:
    Oct 21, 2015
    Location:
    Stuck on Earth in the USA
    #8
    It seems the creator's site is undergoing a rebuild so the download link is (currently) broken.
     
  9. eyoungren thread starter macrumors P6

    eyoungren

    Joined:
    Aug 31, 2011
    Location:
    Phoenix • 85037
    #9
  10. 128keaton macrumors 68020

    128keaton

    Joined:
    Jan 13, 2013
  11. eyoungren thread starter macrumors P6

    eyoungren

    Joined:
    Aug 31, 2011
    Location:
    Phoenix • 85037
    #12
    OK. This is a GUI Scripting thing!

    Code:
    tell application "System Preferences"
    	activate
    	set current pane to pane id "com.apple.preference.hardware"
    	tell application "System Events"
    		tell process "System Preferences"
    			set perform_changes to true
    			if perform_changes is true then
    				-- MAKE CHANGES
    				click checkbox 1 of row 2 of table 1 of scroll area 1 of window "Processor"
    			end if
    		end tell
    	end tell
    end tell
    This will open System Preferences, call the Processor pane and click the checkbox for CPU 2.

    The main problem was trying to find out what the element was being called.

    Now to wrap it up so it does this silently and in the background, code I already have at hand when I GUI scripted Internet Sharing.
    --- Post Merged, Mar 1, 2016 ---
    OK. Turns out my "silently in the background" scripts I used previously were because I had abandoned GUI scripting and went with shell script.

    So, we'll start from square one again. Right now this is good enough. We'll see if it can be improved in the future.

    I'm using the scriptsaver link Hack mentioned earlier.

    ENABLE:
    Code:
    tell application "GrowlHelperApp"
    	set the allNotificationsList to {"Notification 1", "Notification 2"}
    	set the enabledNotificationsList to {"Notification 1", "Notification 2"}
    	register as application ¬
    		"CPU_Toggle" all notifications allNotificationsList ¬
    		default notifications enabledNotificationsList ¬
    		icon of application "System Preferences"
    end tell
    
    tell application "System Preferences"
    	activate
    	set current pane to pane id "com.apple.preference.hardware"
    	tell application "System Events"
    		tell process "System Preferences"
    			if (value of checkbox 1 of row 2 of table 1 of scroll area 1 of window "Processor") is equal to 0 then
    				click checkbox 1 of row 2 of table 1 of scroll area 1 of window "Processor"
    				tell application "GrowlHelperApp"
    					notify with name "Notification 1" title "CPU2 Enabled" description "CPU2 is now enabled" application name "CPU_Toggle"
    				end tell
    			else -- Deactivates CPU Toggle if it is activated
    				if (value of checkbox 1 of row 2 of table 1 of scroll area 1 of window "Processor") is equal to 1 then
    					tell application "System Preferences"
    						quit
    					end tell
    					tell application "GrowlHelperApp"
    						notify with name "Notification 2" title "CPU2 Enabled" description "CPU2 is already enabled" application name "CPU_Toggle"
    					end tell
    				end if
    			end if
    		end tell
    	end tell
    	tell application "System Preferences"
    		quit
    	end tell
    end tell
    DISABLE:
    Code:
    tell application "GrowlHelperApp"
    	set the allNotificationsList to {"Notification 1", "Notification 2"}
    	set the enabledNotificationsList to {"Notification 1", "Notification 2"}
    	register as application ¬
    		"CPU_Toggle" all notifications allNotificationsList ¬
    		default notifications enabledNotificationsList ¬
    		icon of application "System Preferences"
    end tell
    
    tell application "System Preferences"
    	activate
    	set current pane to pane id "com.apple.preference.hardware"
    	tell application "System Events"
    		tell process "System Preferences"
    			if (value of checkbox 1 of row 2 of table 1 of scroll area 1 of window "Processor") is equal to 1 then
    				click checkbox 1 of row 2 of table 1 of scroll area 1 of window "Processor"
    				tell application "GrowlHelperApp"
    					notify with name "Notification 1" title "CPU2 Enabled" description "CPU2 is now disabled" application name "CPU_Toggle"
    				end tell
    			else -- Deactivates CPU Toggle if it is activated
    				if (value of checkbox 1 of row 2 of table 1 of scroll area 1 of window "Processor") is equal to 0 then
    					tell application "System Preferences"
    						quit
    					end tell
    					tell application "GrowlHelperApp"
    						notify with name "Notification 2" title "CPU2 Enabled" description "CPU2 is already disabled" application name "CPU_Toggle"
    					end tell
    				end if
    			end if
    		end tell
    	end tell
    	tell application "System Preferences"
    		quit
    	end tell
    end tell
    It's calling Growl each time to notify me.
     

Share This Page