Skip to content

Instantly share code, notes, and snippets.

@gerad
Created May 8, 2011 19:36
Show Gist options
  • Save gerad/961627 to your computer and use it in GitHub Desktop.
Save gerad/961627 to your computer and use it in GitHub Desktop.
get information on the currently active mac window in ruby / applescript
require 'rubygems'
require 'bundler/setup'
require 'appscript'
# http://stackoverflow.com/questions/480866/get-the-title-of-the-current-active-window-document-in-mac-os-x
while true
frontmost = Appscript.app('System Events').application_processes.get.select{ |a| a.frontmost.get }.first
if frontmost
puts frontmost.name.get
if frontmost.windows.count > 0
window = frontmost.windows.first
puts window.name.get
# Chrome Active Tab
# http://stackoverflow.com/questions/2483033/get-the-url-of-the-frontmost-tab-from-chrome-on-os-x
if frontmost.name.get == 'Google Chrome'
tab = Appscript.app('Google Chrome').windows[0].active_tab
puts tab.URL.get
puts tab.name.get
end
end
end
sleep 0.25
end
# System Idle Time
# http://www.dssw.co.uk/sleepcentre/threads/system_idle_time_how_to_retrieve.html
# ioreg -c IOHIDSystem | perl -ane 'if (/Idle/) {$idle=(pop @F)/1000000000; print $idle,"\n"; last}'
@IshaAgarwala
Copy link

Can you suggest something for Windows?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment