Skip to content

Instantly share code, notes, and snippets.

@Noitidart
Created October 17, 2014 23:42
Show Gist options
  • Save Noitidart/8b875261d1a9dbd6b0cf to your computer and use it in GitHub Desktop.
Save Noitidart/8b875261d1a9dbd6b0cf to your computer and use it in GitHub Desktop.
_applescript-FocusMostRecentWindow -
tell application "System Events" to tell application process "Firefox"
tell application "Firefox"
activate
tell windows
set minimized to false
set visible to true
end tell
end tell
end tell
@Noitidart
Copy link
Author

README

Rev1

  • Thanks to @i'LI from SO: http://stackoverflow.com/questions/26411399/os-x-programatically-click-menu-item-of-dock-menu/26415622?noredirect=1#comment41512939_26415622
    • He mentions some unpredicatability with this, but notes that it is Assitive-less Window Raising:

      Since it doesn't require the assistive devices enabled it uses only internals of window items (not windows). This is how it gets around the need to use it, although the problem is that every application sets it's windows up differently - meaning the items are also different and change often. I didn't have any issues using it with Safari, but with Firefox some very strange things happened, including some piece of a window or box that gets corrupted somehow and sticks in one spot.

@yajd
Copy link

yajd commented Oct 17, 2014

Using PID:
http://stackoverflow.com/questions/2296812/how-to-activate-mac-os-x-application-with-a-given-process-id

tell application "System Events"
    set theprocs to every process whose unix id is 164
    repeat with proc in theprocs
        set the frontmost of proc to true
        exit repeat
    end repeat
end tell

This above works but can be simplified.

Given that by definition only one process can match -- PIDs (process IDs) uniquely identify a single process -- there is no need for a loop: simply directly target the first (and by definition only) element of the list of PIDs returned by the filter process whose unix id is ...

# Note: Assumes that variable `myProcessId` contains the PID of interest.
tell application "System Events"
  set frontmost of the first process whose unix id is myProcessId to true
end tell

@yajd
Copy link

yajd commented Oct 18, 2014

can run via popen from addon:

Cu.import('resource://gre/modules/ctypes.jsm');


function doit() {
        _x11 = ctypes.open('libc.dylib');


    var popen = _x11.declare('popen', ctypes.default_abi, ctypes.voidptr_t, // Return int
        ctypes.char.ptr, // Param1 const char *command
        ctypes.char.ptr // Param1 const char *command
    );

    var fread = _x11.declare('fread', ctypes.default_abi, ctypes.size_t, // Return int
        ctypes.voidptr_t,
        ctypes.size_t,
        ctypes.size_t,
        ctypes.voidptr_t
    );

    var pclose = _x11.declare('pclose', ctypes.default_abi, ctypes.int, // Return int
        ctypes.voidptr_t
    );

    ////////////////////////
    ////END DECLARATIONS
    ////////////////////////

    var file = popen('osascript -e \'tell application "System Events" to set frontmost of the first process whose unix id is 727 to true\'', 'r')
  console.log(file.toString())
    pclose(file);


}

console.time('popen');
doit();
console.timeEnd('popen')

@yajd
Copy link

yajd commented Oct 18, 2014

This popen does AXRaise and it does it as System Events

Cu.import('resource://gre/modules/ctypes.jsm');


function doit() {
        _x11 = ctypes.open('libc.dylib');


    var popen = _x11.declare('popen', ctypes.default_abi, ctypes.voidptr_t, // Return int
        ctypes.char.ptr, // Param1 const char *command
        ctypes.char.ptr // Param1 const char *command
    );

    var fread = _x11.declare('fread', ctypes.default_abi, ctypes.size_t, // Return int
        ctypes.voidptr_t,
        ctypes.size_t,
        ctypes.size_t,
        ctypes.voidptr_t
    );

    var pclose = _x11.declare('pclose', ctypes.default_abi, ctypes.int, // Return int
        ctypes.voidptr_t
    );

    ////////////////////////
    ////END DECLARATIONS
    ////////////////////////

    var file = popen('osascript -e \'tell application "System Events" to tell first process whose unix id is 398 \n set frontmost to true \n \n perform action "AXRaise" \n end tell\'', 'r')
  console.log(file.toString())
    pclose(file);


}

console.time('popen');
doit();
console.timeEnd('popen')

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