Skip to content

Instantly share code, notes, and snippets.

@kylekyle
Created May 6, 2016 18:43
Show Gist options
  • Save kylekyle/a096f9734bd72139200f13ae2f4a5208 to your computer and use it in GitHub Desktop.
Save kylekyle/a096f9734bd72139200f13ae2f4a5208 to your computer and use it in GitHub Desktop.
# https://porkrind.org/missives/calling-applescript-from-ruby/
def osascript(script)
system 'osascript', *script.split(/\n/).map { |line| ['-e', line] }.flatten
end
# http://stackoverflow.com/questions/5292204/macosx-get-foremost-window-title
osascript <<-END
global frontApp, frontAppName, windowTitle
set windowTitle to ""
tell application "System Events"
set frontApp to first application process whose frontmost is true
set frontAppName to name of frontApp
tell process frontAppName
tell (1st window whose value of attribute "AXMain" is true)
set windowTitle to value of attribute "AXTitle"
end tell
end tell
end tell
return {frontAppName, windowTitle}
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment