Alphred
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Download

Namespaces

  • Alphred
  • None

Classes

  • Alphred
 1 <?php
 2 /**
 3  * Contains AppleScript class for Alphred, just some php wrappers around some AppleScript
 4  *
 5  * PHP version 5
 6  *
 7  * @package      Alphred
 8  * @copyright  Shawn Patrick Rice 2014
 9  * @license    http://opensource.org/licenses/MIT  MIT
10  * @version    1.0.0
11  * @author     Shawn Patrick Rice <rice@shawnrice.org>
12  * @link       http://www.github.com/shawnrice/alphred
13  * @link       http://shawnrice.github.io/alphred
14  * @since      File available since Release 1.0.0
15  *
16  */
17 
18 namespace Alphred;
19 
20 /**
21  * Provides limited functionality to AppleScript
22  *
23  * Think of this as a wrapper for some AppleScript
24  */
25 class AppleScript {
26 
27     /**
28      * Gets the frontmost window name and application
29      *
30      * @return array an array with the front application name and window name
31      */
32     public function get_front() {
33         // This is just inelegantly embedding a long AppleScript into the library
34         // https://stackoverflow.com/questions/5292204/macosx-get-foremost-window-title
35         $script = '
36         global frontApp, frontAppName, windowTitle
37         set windowTitle to ""
38         tell application "System Events"
39             set frontApp to first application process whose frontmost is true
40             set frontAppName to name of frontApp
41             tell process frontAppName
42                 tell (1st window whose value of attribute "AXMain" is true)
43                     set windowTitle to value of attribute "AXTitle"
44                 end tell
45             end tell
46         end tell
47         return {frontAppName, windowTitle}';
48         $result = self::exec( $script );
49         return [
50             'app'    => substr( $result, 0, strpos( $result, ', ' ) ),
51             'window' => substr( $result, strpos( $result, ', ' ) + 2 )
52         ];
53 
54     }
55 
56     /**
57      * Brings an application to the front, opening it if necessary
58      *
59      * @param  string $application the name of the application
60      */
61     public function activate( $application ) {
62         return self::exec(
63             'tell application "' . addslashes( $application ) . '" to activate'
64         );
65     }
66 
67     /**
68      * Brings an application to the front, but only if it is open
69      *
70      * @param  string $process name of application
71      */
72     public function bring_to_front( $process ) {
73         return self::exec(
74             "try\ntell application \"System Events\" to set frontmost of process \"{$process}\" to true\nend try"
75         );
76     }
77 
78     /**
79      * Executes some AppleScript code
80      *
81      * @param  string $script the script to execute
82      * @return mixed          whatever the script returns
83      */
84     private function exec( $script ) {
85         return exec( "osascript -e '{$script}'" );
86     }
87 
88 }
Alphred API documentation generated by ApiGen