1

I am trying to write an apple script that when executed, will make the currently focused window "always on top". Is this possible?

In objective-c I can do this on windows my process owns by using [NSWindow setLevel:] but my struggle is to do it for windows my process does not own. So I am now trying to do it via apple script.

I tried this:

global frontApp, frontAppName, windowTitle
delay 3
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
        set level of window 1 to 3
        tell window 1
            set windowTitle to value of attribute "AXTitle"
        end tell
    end tell
end tell

return {frontAppName, windowTitle}

However this gives me error -

System Events got an error: Can’t make level of window 1 of process "firefox" into type specifier.

Noitidart
  • 35,443
  • 37
  • 154
  • 323
  • 1
    It depends on the target application – vadian May 01 '16 at 04:17
  • Thanks @vadian that's a very interesting comment. I didn't expect that it would depend, I would think like on Linux and Windows it was a one solution fits all. Could you please expand on that? – Noitidart May 01 '16 at 10:56
  • 1
    There are Cocoa applications with and without an AppleScript dictionary, and still Carbon applications. All three categories have to be treated differently. – vadian May 01 '16 at 10:59
  • @vadian I tried this script here - https://gist.github.com/Noitidart/8eb5c2a8e1a655ad9ccf155893b6263c - it is failing, may you please help provide me solution to handle those three different scenarios you mention? – Noitidart Sep 03 '16 at 18:42
  • my script linked above gives me the error - `System Events got an error: Can’t make level of window 1 of process "firefox" into type specifier.` – Noitidart Sep 03 '16 at 18:51
  • 1
    There is no property `level` in class `window` of `System Events`. Look into the dictionary of System Events. – vadian Sep 03 '16 at 19:05
  • @vadian ok thanks I'll keep trying – Noitidart Sep 03 '16 at 19:12
  • 1
    Sorry but the subject is too complex to be accomplished in 10 lines of code. Actually you have to treat almost each application individually, at least those without AppleScript support. – vadian Sep 03 '16 at 19:17
  • Thanks for your honesty @vadian if you find some time may you please provide answer I can give 500 bounty (i can create multiple and give you more too). I have been struggling with this for quite some time. – Noitidart Sep 03 '16 at 19:31
  • @vadian I actually have bounty expiring right now in few hours - http://stackoverflow.com/questions/36075213/set-non-owned-window-always-on-top-like-the-app-afloat?noredirect=1# - if you post a dumy post there I can accept yours and in future if you ever can give me solution that would be so aweosme i can give 500 more bounty then too – Noitidart Sep 03 '16 at 19:41

0 Answers0