Skip to content

Instantly share code, notes, and snippets.

@kch
Created January 28, 2011 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kch/799697 to your computer and use it in GitHub Desktop.
Save kch/799697 to your computer and use it in GitHub Desktop.
Test case for WTF is up with System Events getting the processes for Safari and WebKit
#!/bin/bash
function safari { open -a Safari; }
function webkit { open -a WebKit; }
function reset {
killall Safari 2>/dev/null
killall WebKit 2>/dev/null
}
function bring_front {
osascript -e "tell application \"$1\" to activate"
}
function get_name_of_frontmost_with_assignment {
echo -n "assigned: "
osascript -e 'tell application "System Events" to set _app to the first process whose frontmost is true
get the short name of _app'
}
function get_name_of_frontmost_directly {
echo -n "directly: "
osascript -e 'tell application "System Events" to get the short name of the first process whose frontmost is true'
}
echo "### with only safari running"
reset; safari
get_name_of_frontmost_directly
get_name_of_frontmost_with_assignment
sleep 2
echo "### with only webkit running"
reset; webkit
get_name_of_frontmost_directly
get_name_of_frontmost_with_assignment
sleep 2
echo "### with both running, safari started first, safari frontmost"
reset; safari; webkit
bring_front Safari
get_name_of_frontmost_directly
get_name_of_frontmost_with_assignment
echo "### with both running, safari started first, webkit frontmost"
bring_front WebKit
get_name_of_frontmost_directly
get_name_of_frontmost_with_assignment
sleep 2
echo "### with both running, webkit started first, webkit frontmost"
reset; webkit; safari
bring_front WebKit
get_name_of_frontmost_directly
get_name_of_frontmost_with_assignment
echo "### with both running, webkit started first, safari frontmost"
bring_front Safari
get_name_of_frontmost_directly
get_name_of_frontmost_with_assignment
reset
@kch
Copy link
Author

kch commented Jan 28, 2011

Output:

$ ./frontmost-browser.sh
### with only safari running
directly: Safari
assigned: Safari
### with only webkit running
directly: WebKit
assigned: WebKit
### with both running, safari started first, safari frontmost
directly: Safari
assigned: Safari
### with both running, safari started first, webkit frontmost
directly: WebKit
assigned: Safari
### with both running, webkit started first, webkit frontmost
directly: WebKit
assigned: WebKit
### with both running, webkit started first, safari frontmost
directly: Safari
assigned: WebKit

@kch
Copy link
Author

kch commented Jan 28, 2011

Output reformatted as a pretty table:

+----------------+-----------+----------+----------+
| Launch order   | Frontmost | directly | assigned |
+----------------+-----------+----------+----------+
| Safari         |           | Safari   | Safari   |
| WebKit         |           | WebKit   | WebKit   |
| Safari, WebKit | Safari    | Safari   | Safari   |
| Safari, WebKit | WebKit    | WebKit   | Safari * |
| WebKit, Safari | WebKit    | WebKit   | WebKit   |
| WebKit, Safari | Safari    | Safari   | WebKit * |
+----------------+-----------+----------+----------+

* indicates incorrect results.

@kch
Copy link
Author

kch commented Jan 28, 2011

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