Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Fri Sep 21, 2018 5:22 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Detecting current active window in OSX
PostPosted: Tue Oct 16, 2012 12:45 am 
Offline
User avatar

Joined: Thu Apr 30, 2009 1:00 pm
Posts: 120
Is there any way to detect which window on the screen of Mac OSX that is the current active window (the window that has been clicked on and active by the user)?

I mean detecting ANY active window on any software that is running, not just the windows of the real studio software itself.

I would like to detect which window that is active, and be able to read the title of the active window, and also the name of the main-thread/software that owns it.


Top
 Profile  
Reply with quote  
 Post subject: Re: Detecting current active window in OSX
PostPosted: Tue Oct 16, 2012 2:55 am 
Offline
User avatar

Joined: Wed May 10, 2006 2:42 pm
Posts: 2985
Location: Germany
If you use MBS Plugins, this may be the solution:
http://www.monkeybreadsoftware.net/faq- ... itle.shtml

Greetings
Christian

_________________
See you in Orlando, Florida for Real World 2013
More details and registration here:
http://www.realsoftware.com/community/realworld.php


Top
 Profile  
Reply with quote  
 Post subject: Re: Detecting current active window in OSX
PostPosted: Tue Oct 16, 2012 7:38 pm 
Offline
User avatar

Joined: Thu Sep 10, 2009 2:50 am
Posts: 418
Location: Santa Cruz, CA, USA
BitsInAWhiteBox wrote:
I would like to detect which window that is active, and be able to read the title of the active window, and also the name of the main-thread/software that owns it.


Applescript can do this, but it'll take more expertise than I have. I pasted this together and it works... for 3 of the 6 apps tested with. The use of "System Events" requires 'System Preferences->Universal Access->Enable access for assistive devices' to be turned on; and the delay is simply for some time to switch to other apps/windows to test. Run in Script Editor.

delay 3

tell application "System Events"
set frontApp to name of first application process whose frontmost is true
end tell

tell application frontApp
set window_name to name of front window as string
end tell

display dialog (frontApp as string) & ": " & window_name


Top
 Profile  
Reply with quote  
 Post subject: Re: Detecting current active window in OSX
PostPosted: Wed Oct 17, 2012 3:25 pm 
Offline
User avatar

Joined: Thu Apr 30, 2009 1:00 pm
Posts: 120
Thanks for the answers from both of you! I will test them both out and see what will work best for me. The plugin by monkebread seems very interesting too, since it seems to have ability to get other info as well, which i might need at a later point for more advanced features!


Top
 Profile  
Reply with quote  
 Post subject: Re: Detecting current active window in OSX
PostPosted: Wed Nov 21, 2012 6:02 am 
Offline

Joined: Sun Feb 28, 2010 4:14 am
Posts: 37
Is there any way of doing this without turning on the "Enable Access for Assistive Devices" setting? Both methods (AppleScript and MBS) seem to require it to be turned on.


Top
 Profile  
Reply with quote  
 Post subject: Re: Detecting current active window in OSX
PostPosted: Wed Nov 21, 2012 1:38 pm 
Offline

Joined: Sat Jun 04, 2011 2:28 pm
Posts: 67
I used a variation of script suggested by Doofus, and I did not have to"Enable Access for Assistive Devices".

tell application "System Events"
set frontApp to name of first application process whose frontmost is equal to true
return frontApp as string
end tell


In RB project

Dim Result as String

Result = FrontmostAppScript // Applescript called FrontmostAppScript dragged into project

TextField.Text = Result


regards mark


Top
 Profile  
Reply with quote  
 Post subject: Re: Detecting current active window in OSX
PostPosted: Wed Nov 21, 2012 3:13 pm 
Offline

Joined: Sun Feb 28, 2010 4:14 am
Posts: 37
Thanx! It works. I must admit I believed it requires this setting without actually trying it.

On the other hand, there is no way of making it work with sandboxing enabled, right? Other than using temporary entitlements?


Top
 Profile  
Reply with quote  
 Post subject: Re: Detecting current active window in OSX
PostPosted: Wed Nov 21, 2012 3:15 pm 
Offline

Joined: Mon Aug 15, 2011 10:25 pm
Posts: 293
Mark your script will get the frontmost application, whereas BitsInAWhiteBox was looking to get the name of the frontmost window of an application, and that requires to have the "Enable Access for Assistive Devices" turned on..

_________________
Real Studio 2012r1.1 | MacBook Pro i5, 10.6.8 | Windows 7


Top
 Profile  
Reply with quote  
 Post subject: Re: Detecting current active window in OSX
PostPosted: Wed Nov 21, 2012 4:00 pm 
Offline

Joined: Sat Jun 04, 2011 2:28 pm
Posts: 67
Your right shaosean, to access the frontmost window title does require"Enable Access for Assistive Devices" to be enabled.

This script would return the title of front most window of the frontmost application if it was enabled.

tell application "System Events"
set frontApp to name of first application process whose frontmost is equal to true
set windowName to name of front window of application process frontApp
return windowName
end tell


I'm currently trying some hacks to enable Assistive devices by script, with some success, but needing administrator passwords.

but I will let you know if I have any joy with it.

regards Mark


Top
 Profile  
Reply with quote  
 Post subject: Re: Detecting current active window in OSX
PostPosted: Fri Nov 23, 2012 12:51 pm 
Offline

Joined: Sat Jun 04, 2011 2:28 pm
Posts: 67
Further to my efforts to the problem of automating the enabling of Universal Access, I have come up with two
solutions, but both require the User to enter their Username and Passwords, so in short both suck for that reason.

These solutions will work without User validation on OSX 6 and before Sandboxing, but for what its worth to anyone,
here they are.

tell application "System Events"
set UIEnabled to UI elements enabled
if UIEnabled is equal to true then
set frontApp to name of first application process whose frontmost is equal to true
set windowName to name of front window of application process frontApp
return windowName as string
else if UIEnabled is equal to false then
set UI elements enabled to true --Asked for Username and Password
delay (0.5)
set frontApp to name of first application process whose frontmost is equal to true
set windowName to name of front window of application process frontApp
set UI elements enabled to false --Asked for Username and Password
delay (0.5)
return windowName as string
end if
end tell


OR if you know the Username and Password

--Replace these line in the above script
set UI elements enabled to true --Asked for Username and Password
--and
set UI elements enabled to false --Asked for Username and Password

--With these lines
do shell script "touch /private/var/db/.AccessibilityAPIEnabled " user name "USERNAME" password "PASSWORD" with administrator privileges
-- and
do shell script "rm /private/var/db/.AccessibilityAPIEnabled " user name "USERNAME" password "PASSWORD" with administrator privileges


Regards Mark


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group