I’ve got a floating window with a Hide button, and I want to be able to show that window when it’s hidden by clicking on my app’s icon in the Dock. I have code inside the Event Activate that works, except that if my app is still active in the macOS, it doesn’t work. You have to first click on another app or the desktop, then click back on my app icon to make the window show. Is there a way to have the app respond to the Dock icon being clicked regardless of whether it’s active or not?
When you click on your app icon in the Dock, the application receives an AppleEvent. So, you can catch this AppleEvent and restore your window.
In API2, you’d add the AppleEventReceived event to the app class with this code:
if eventClass+eventID="aevtrapp" then 'eventClass=aevt and eventID=rapp when the app is “reopened”
//Restore the window
Return True
end if
In API1, it’s the same except the event to add to the app class is HandleAppleEvent.
HTH
I don’t know what API1 or API2 is.
Where this code be going exactly?
I don’t have an AppleEventReceived in my Event Handler options.
Oh dear oh dear.
What did @Arnaud_N say in his post?
Thank you Arnaud! That did it!
You’re welcome
Hi people. My problem seems to be exactly this at first sight, but I can’t get my head around it !
I’m building for MacOS only. My app has items in a Listbox which, when double-clicked with or without modifier keys, reveal various documents in the Finder. Having revealed the required document (which brings it to the front), I would like to bring my app back to the front with code.
I have tried with AppleScript,
"set frontmost to true"
but that only works the first time you call it, or not at all!
and also using "perform action "AXRaise" "
with no more luck,
with a shell script :
ssh.Execute ( "osascript -e 'tell application ""Name of my app"" -e Activate -e end tell'")
but that only works from within the terminal, not from my app.
So when I fell on this post, I thought this could be the solution.
I can get my app’s PID after it launches without problem, but since there is no physical click on the dock icon, there is no AppleEvent to be “caught”.
As a relative newbie, I’m obviously missing something here but I’m wondering if, all told, this can be applied at all in my situation, and if so, how exactly would I do it ?
Thanks a lot for your insights…
Add an “Activated” event to your App class. In that check if any windows are open. if not then open the required window type:
Sub Activated() Handles Activated
If App.WindowCount = 0 Then
// Open a new window
ElseIf App.WindowAt( 0 ) Is Nil Then
// Open a new window
End If
End Sub
Thanks Ian. I’ll give that a try later, and be back to you.
OK. I took a closer look at this code, and I’ll be honest, I can make neither head nor tail of it. I have an “Activate” event in the App class, but no “Activated”, which I could create, but I don’t see where if would get me. If I paste your code into the “Activate” event, at runtime it throws an error with Ap.MyApp has no member named "WindowAt"
I think I must have totally missed your point. I dont see either why I’d need to “Open a new window” as my App window was never close, but put in the background…
Mmmmmm sorry but I feel mentally challenged here
My code is API 2, is you app old API 1?
If so you need to look at Window{0), instead of App.WindowAt.
I thought you asked about opening a new window when the app was in the background with all windows closed and you clicked the icon in the doc.
As fast as I’m aware all windows are brought to front automatically when you click the doc icon. What type of windows are you using? Are the document windows or some sort of dialog?
I’m sorry if I wasn’t clear. I’m using Xojo 2023. The problem seems simple yet I can’t solve it.
I have a short “showInFinder” method that uses the a shell script to show various documents in the Finder using sh.Execute "open -R 'pathToTheDocument' "
The problem is to return to my App once the Finder window has been opened. After trying all sorts of ways that didn’t work, I thought that simulating a click on the Dock icon might be the solution, but I can’t figure out how to apply that.
The only thing that works every time for me is to show a message dialog in my App, directly after my shellScript, such as MessageBox "Your document is displayed"
, but if the user wants to show many documents, this would soon become tedious!
Thanks for your patience…
Perhaps a small dismissable dialog window rather than a MessageBox? Then you could also put subsequent messages there, perhaps all in a TextArea with a timestamp on each message with the filename.
Well. When I “show in Finder”, then I expect to have Finder opened (and the previous app in the background).
So you intend to “Open in Finder, but with Finder in background”?
Then look at the open
Man Page - macOS.
-R Reveal the file(s) in the Finder instead of opening them.
-g Do not bring the application to the foreground.
Maybe what you want (*) is: open -R -g /path/to/doc
(*) even if I wouldn’t want behaviour that as an end-user
Yes. That could be a solution. Certainly better than a static message for sure .
Thanks Jürg. That actually works fine and I have yet again learned something . Having seen that in practice, you are right, it’s not the best solution. The snag is, it opens the window right in the background, so any big window would probably mask it
. If you open it with -R, it’s in front, so if you click back on your App, it’s only in second place, behind the App. I think, all told, I’ll go with Tim’s “dismissible dialogue” solution might be the most ergonomic solution.