Launching app in foreground with Qt under MacOS X - macos

I am having problems with a little Qt 5.0.1 program under Mac OS X 10.8.
(I have not tested any other platforms yet.)
I am launching an external Mac OS X program with this line of code:
QDesktopServices::openUrl(QUrl::fromLocalFile(fullpath));
Where fullpath contains a path to an application like:
/Users/schube/QTWorkspace/HelloWorld-build-Desktop_Qt_5_0_1_clang_64bit-Debug/HelloWorld.app/Contents/MacOS/../../../Aptus.app
(Aptus.app is a random app I've chosen, could be any app. I placed it in this path for testing purposes).
The application starts correctly but always in background; or at least, behind a Finder window. Really strange!
How can I force the new launched app to be sent to the foreground?

Use QProcess instead, but make sure that rather than using the path to the executable as the object to run, pass it to the open command as an argument. Something like this: -
QString cmd = QString("open %1").arg(fullpath); // may need QUrl::fromLocalFile(fullpath)
QProcess::startDetached(cmd);
Without using 'open', it will also open up behind other applications.
Note that you could also use the execute function, if you want to wait for the program to finish.
Also, with the open command, I think you only need to pass the path to the app bundle, rather than full path to its executable in Contents/MacOS. Either should work.

This might be a feature of the Mac OSX's window manager so that it does not steal focus.
You might need to alter your application to minimize.

Related

How to hide terminal shell on server application like Warp in Windows?

I have a small warp server project on Windows that listen to a particular port and do something whenever I send a command to it by REST (for example: POST http://10.10.10.1:5000/print). It's a small client for printing PDF / receipt directly from another computer.
It works. But my problem is when I had to package the whole project, the Rust compiler give me an executable file (.exe). The application displays a terminal window when I run it. I want this terminal to be hidden somehow.
I try to run the program as a windows service (by using NSSM). It doesn't work for me since I had to access the printer. Windows doesn't allow my app to access any devices or any other executable as a windows service. (The reasons are explained here: How can I run an EXE program from a Windows Service using C#?)
So I plan to run my app as a tray-icon application so user can control or close the app. (https://github.com/olback/tray-item-rs)
Unfortunately, I still cannot hide the app's terminal window.
Another solution that I found is hstart (https://www.ntwind.com/software/hstart.html). But I would like to use this as "the last resort" solution since many antivirus/windows defender mark it as a malware.
Do anyone know how to hide or get rid of it ?
After lot of searching, It turns out to be easier than I thought. Just add
#![windows_subsystem = "windows"]
on top of your main.rs file. (for rust > 1.18) and the terminal is gone.
These control the /SUBSYSTEM flag in the linker. For now, only
"console" and "windows" are supported.
When is this useful? In the simplest terms, if you're developing a
graphical application, and do not specify "windows", a console window
would flash up upon your application's start. With this flag, it
won't.
https://doc.rust-lang.org/reference/runtime.html#the-windows_subsystem-attribute
https://blog.rust-lang.org/2017/06/08/Rust-1.18.html
https://learn.microsoft.com/en-us/cpp/build/reference/subsystem-specify-subsystem?view=msvc-170

Mac OS X: interacting with an application programmatically

I am working on a project where I need to call methods on an existing application (my own) and use some of its functionality. For e.g. my application ThunderBolt runs on Mac OS X 10.10. It also provides a dictionary of events that can be called externally through Apple Script or some other way that I don't know yet.
My question is what are the different (and better) ways of interacting with an application programmatically on Mac OSX? If I use something like the following code in Apple Script Editor:
tell application "ThunderBolt"
set open_file to (choose file with prompt "Choose the file you wish to parse")
set theContents to read open_file as data
set retPict to (image convert theContents)
end tell
then it is going to launch ThunderBolt with a splash screen and then call "image convert". This can be done via NSAppleScript but still it would launch the application and call methods/events on it.
Is it possible to somehow create an instance of (or get a pointer to) one of the class inside the application and use that? Something similar to COM or automation on a Windows system?
If you're working on OS X 10.10, you might consider taking a look at JavaScript for Automation (JXA).
With it you can apparently build methods into your app that can be invoked from client scripts written in JS (although I'm not yet familiar with the particulars of how to handle implementation of such a thing on the app side). But many of the apps that ship as part of OS X Yosemite have such APIs built in (e.g. iTunes and Finder).
Here's a great tutorial on JXA written by Alex Guyot: http://www.macstories.net/tutorials/getting-started-with-javascript-for-automation-on-yosemite/
The JXA-Cookbook repo also appears to be a nice resource: https://github.com/dtinth/JXA-Cookbook/wiki
Here's a brief example - this script makes iTunes go back one track. Try it while iTunes is playing (by putting the text into Script Editor, with the language option set to JavaScript, and hitting the Run button):
iTunes = Application('iTunes')
state = iTunes.playerState()
// Console msgs show up in the Messages tab of the bottom view:
console.log("playerState: " + state)
iTunes.backTrack()
Alternatively, you can place the code into a .js file and run it on the command line:
$ osascript itunes-backTrack.js
playerState: playing
The way you specify the 'tell application' is the best way, in my opinion.
What do you do with your app that needs to be called? Maybe some of the functionalities can be done with Applescript? It would simplify things a lot.

Is it possible to create a Mac OS X gui app which is not a bundle?

I want to make an executable file (not the Mac .app bundle) which when run with a specific option (e.g. -gui) will pop up the gui.
For example, say I'm writing wget. I could do: wget www.google.com and that would print the result to the console, but if I instead do: wget -gui www.google.com that would pop up a gui window with the render of the html.
Is this possible in OS X (it is in windows)?
Note: I mean that the gui code is contained in the executable, calling open on another app is not acceptable.
Simple: Yes
Your second question will be how I guess.
Sometimes people ask me for applications that when launched by the Finder they should behave normal but when started by another process or terminal it should behave different. So, just like firefox, you can start the application the normal way or you can start it with special option.
Another way is creating agents. They are applications but don't appear in the dock and are designed to show interface elements when needed.
If you don't want to use a bundle per se you can just create a (cocoa) command line utility that loads an NSApplication when needed (depending on the command line options).

Calling a command line in MONO on MAC OS X

I want to be able to call the automator or unix commands like ls from a mono app and ge the results back.
This can be accomplished on windows easily. The question is how is this done on the mac??
caveat: I've never written a char of mono in my life.
I imagine it's a matter of redirecting stdout and firing up a process. this linux forum shows that you can do pretty much that - OSX will behave mostly as a UNIX-like system for you, I reckon.
Oh by the way, if you want to fire up an OSX application, have a dig around inside the ".app" bundle. OSX shows these as a file, but they're actually directories. In the finder you can right-mouse click and "show package contents", or you can open up a terminal / command prompt and cd into them. For instance, you can launch the Automator like this from the terminal:
/Applications/Automator.app/Contents/MacOS/Automator
I don't know if you would want to go down this route, but if you're going to be interfacing with OSX (gui) apps, you might want to look at using Applescript as some "glue" between Mono and the app.

OS X: how to make command-line script appear as helper application to handle mailto?

I'm trying to configure Emacs as my preferred application when the user clicks on a mailto: link.
Emacs has facilities for this:
emacs 23 on OS X: mailto links & calling compose-mail?
http://www.emacswiki.org/emacs/MailtoHandler
and I'll do it by creating a shell script that then calls emacsclient ("emacs-mailto-handler" in the previous link).
Unfortunately, when I go to Firefox, Preferences, Applications, it doesn't recognize shell scripts. Instead it wants an OS X application bundle (at least that's what I surmise).
I thus could perhaps figure out how to turn a shell script into an application bundle or figure out how to arm-wrestle Firefox into recognizing a shell script. Help? Or should I be thinking about this the other way?
Use the OS X Automator App to run the shell script, and save the Automator Project as an App Bundle.
You can use that App Bundle as you would any other Application.

Resources