2

Im trying to write a super simple applescript that will launch the OneDrive App, or ensure it is open, whenever the machine's power source is set to plugged in, and will quit, or make sure is closed, when the power source is set to battery.

I'm having trouble finding how to access the built-in "power indicator" in Yosemite. All of my searches lead to old, irrelevant results from years ago.

Edit: I think I will have to use a do shell script within the applescript using pmset -g batt

Now drawing from 'AC Power'
 -InternalBattery-0 100%; charged; 0:00 remaining

And parse this result, but I am not sure how.

Edit: Here it is for anyone in the future who may want something similar:

global appName

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

on acIsConnected()
    return (do shell script "system_profiler SPPowerDataType | grep -q 'Connected: Yes' && echo \"true\" || echo \"false\"") as boolean
end acIsConnected

on toggleApp()
    if my acIsConnected() then
        if not my appIsRunning() then
            tell application "Finder"
                open application file (appName & ".app") of folder "Applications" of startup disk
            end tell
        end if
    else
        tell application appName
            quit
        end tell
    end if
end toggleApp

-- This will only be executed once.
on run
    set appName to "OneDrive"
end run

-- This will be executed periodically, specified in seconds, every return.
on idle
    my toggleApp()
    -- Execute every 2 minutes.
    return 120
end idle

-- Not mandatory, but useful for cleaning up before quiting.
on quit
    -- End handler with the following line.
    continue quit
end quit
Francisco Aguilera
  • 3,099
  • 6
  • 31
  • 57
  • The links you found are still relevant and work but you do need DssW Power Manager installed; [Scripting Batteries and UPS on Mac OS X](http://www.dssw.co.uk/blog/2013-06-23-scripting-batteries-and-ups-on-mac-os-x/index.html). – Graham Miln Apr 17 '15 at 07:59

4 Answers4

3

Here is a one-liner that polls for connected status, since I guess you can have less than 100% and still be connected (charging).

set acConnected to (do shell script "system_profiler SPPowerDataType |grep -q 'Connected: Yes' && echo \"true\" || echo \"false\"") as boolean
McUsr
  • 1,400
  • 13
  • 10
2

Here's another one liner...

set acConnected to last word of paragraph 1 of (do shell script "ioreg -w0 -l | grep ExternalChargeCapable")
regulus6633
  • 18,848
  • 5
  • 41
  • 49
  • Hello. I prepended both commands with the 'time' utility, -because I thought your version to be way faster than mine, but, on my machine at leaste, the system_profiler version is a second faster. – McUsr Apr 16 '15 at 22:43
  • @McUsr Thanks, I didn't know about the 'time' utility. Anyway, I'm suspicious when it comes to "what is faster" because the system caches a lot of things. So I did a "purge" before every test. Result (real): ioreg: ~1s, system_profiler: ~4s –  Apr 17 '15 at 03:26
  • How did you "purge" the system? – McUsr Apr 17 '15 at 09:47
  • @McUsr it's a console utility by the same name. –  Apr 17 '15 at 10:53
  • I'll post it after I have purged, I am not purging at the moment, because I am in the middle of something. You'll get a red marker when I have done it. – McUsr Apr 17 '15 at 18:45
2

If you are happy to use a third party tool, you can avoid polling for the battery state. This will make your script more efficient.

Power Manager can run AppleScripts when the battery state changes. How to Run a Command When Switching to Battery Power, walks through how to set this up for scripts.

Swap out the #!/bin/sh for #!/usr/bin/osascript in the script, and you can use AppleScript.

Power Manager - run a script on switch to battery

Disclaimer: I wrote Power Manager and can answer comments about how it works.

Graham Miln
  • 2,724
  • 3
  • 33
  • 33
  • 1
    Nice, Graham, doesn't power manager itself poll for power states, however? – Francisco Aguilera Apr 17 '15 at 22:47
  • 1
    Power Manager does not poll as it can use [IOKit layer](https://developer.apple.com/library/mac/documentation/DeviceDrivers/Conceptual/IOKitFundamentals/Features/Features.html#//apple_ref/doc/uid/TP0000012-TPXREF101) of OS X and wait for notifications from the power subsystem. – Graham Miln Apr 18 '15 at 11:37
1

Provided you have battery icon on screen's top right:

tell application "System Events" to tell process "SystemUIServer" ¬ 
to value of attribute "AXDescription" of ¬
(first menu bar item whose value of attribute "AXDescription" ¬
begins with "Battery") of menu bar 1

You get "Battery: Charged" or "Battery: Calculating Time Remaining… " or something else

fartheraway
  • 363
  • 2
  • 8