How to launch "Choose Application" dialog on Mac? - macos

I am using LSOpenItemsWithRole() to open any file from my application. It works fine for all files which has a default application on Mac, but for the files which cannot be opened with any default application this method returns an error kLSApplicationNotFoundErr and does nothing.
For such cases, I want my application to launch the "Choose Application" dialog box, so that end users can choose any application from there to open the file. This dialog box pops up whenever any such file is directly opened by double clicking. Is there is any direct API call to do the same?
I don't want to use Objective C call, is there any way to do it using Carbon API calls?

You should use an NSOpenPanel, starting the user in the Applications folder use and the panel:shouldEnableURL: delegate method to filter out paths that don't end in .app. You can use setAccessoryView: to add any custom options to the dialog. This is what the Finder is doing when you click on the "Other..." option when selecting which application to use.

I think you can do it by using NavCreateChooseFileDialog, with NavCustomControl to set the initial location and NavDialogSetFilterTypeIdentifiers to filter out non-apps. (Why don't you want to use Objective-C? You know that you can mix Carbon and Cocoa in one app, right?)

Related

read/capture Windows pop-up message in vb6?

Problem: Need to read/capture the text of Windows pop-up messages that is generated by non-VB applications.
Situation:
I've a VB6 app, part of which requires processing an excel workbook. A non vb-6 pop-up window (as attached screen) "FILE CONVERSION IN PROGRESS" comes up, while opening an new version of excel-sheet from an old MS Excel app. And automatically it closes alos.
Requirement: I want to capture that pop-up occurance in the code. And then write a conditional statement code for the 'cancel' button click event of that non vb-6 pop-up.
Can anyone suggest something?
You can access other applications with the following APIs:
FindWindow() to locate the main window of what you're looking for
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633499%28v=vs.85%29.aspx
GetWindow() to navigate through the HWNDs of the application so you can get to the button
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633515%28v=vs.85%29.aspx
GetWindowText() to access the text from a control (it cannot be an Edit control)
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633520%28v=vs.85%29.aspx
You'll want to use Spy++ (which can be downloaded) to see what the class name you're looking for when it comes up and to figure out the hierarchy to navigate properly.
You'll need to use the API Text Viewer to get the API declarations so you can use them in VB6 properly.

Qt on Mac: how to get a file dialog that allows to write the desired file path manually?

On Mac OS-X, usually directory dialogues are shown without the possibility to enter the path with the keyboard. This seems to be the default behaviour when I build a Qt application on OS-X.
How can I specify that I do want the path to be manually entered?
I'd recommend using the native dialog obtained thought he QFileDialog's static methods. With the new sandboxing requirements from apple, you must use the standard dialogs for your application to get access to the file system. Its interesting actually, the actual dialog given to the user in a sandboxed application is though something called Powerbox. You can read more about it here in the NSPanel documentation.
Also as elmigranto pointed out in all the file dialogs on the Mac, you can hit command + shift + g to enter the path to navigate to.
Note: In the newer Qt versions the documentation implies that they corrected this such that using the static functions on QFileDialog gets you a native dialog that also goes through PowerBox. I haven't tried it myself though and the bug on the issue remains open against Qt 5.5.0 and 5.6.0. Overriding QFileDialog doesn't get you a native dialog however. If you need a customized dialog, going the NSSavePanel route is the way to go.
If you use the static functions of QFileDialog, such as getExistingDirectory() and getOpenFileName() you get native OS X file dialogs. However, if you create your own QFileDialog without using the static functions you should get a Qt file dialog for which you can specify what the user must select in the dialog by calling setFileMode(). I think the Qt file dialog will let the user manually change the path in the dialog.
Of course if you only want to have the user manually type in a directory path you could also use a simple QInputDialog.

Create an application and attach it on right-click on a file

I want to make an application in C# VS12 preferably, and I want to attach it on right click menu passing the filepath as an argument, it would be great if I could do that on multiple selection too. How do I make that happen?
in the installer of your application you need to add a file extension association. (for development you can test that by registering the extension manually: right click, open with...). the file name that was clicked will be passed to your app as a command line argument (those are the args[] in the main method of you app).
multiple selection is a bit trickier but certainly doable. when your app starts you need to check if you have an instance of the app already running (using a mutex for example) and send a message to that instance with the file name you received.
If you have not worked with context menu handling, you should take a look at this tutorial.
For the file path handling, you could take a look at this image editing example.

Have one application execute an action when another application is given a command using AppleScript?

I know nothing about AppleScript, but I wonder if it could make my life easier: is there a way to write an AppleScript that tells Safari / Firefox / Chrome to refresh the current tab when I save a document in another application, say TextWrangler? Essentially, I want to map the Command+S keyboard shortcut to do two things at once in two separate applications.
If that’s not possible, can you script one application so that saving one file executes a command in another window in that same application?
There are different possible approaches to implement this, but the most straightforward would probably be to create a script that executes all steps you need (i.e. save the document and refresh the window) and bind that to the Cmd+S keyboard combo in the triggering application.
What you need for this approach to work, is, in order:
a method to bind a key combo to a script effective only in a specific application. OS X’ Automator Services fit that bill: their scope can be restricted to a single application (select it in the “only in” drop down at the top of the workflow actions), and they can be assigned a shortcut in the Keyboard preference pane of System Preferences.
a way to relay your commands to the applications they target. AppleScript can help you in two different ways, depending on the fact if your applications are scriptable, i.e. if they have a scripting dictionary you can inspect in the AppleScript editor:
if they do, and their terminology includes the saving action for the editor on one side (most scriptable document based apps do so in the form save <document>), the page refreshing for the browser(s) on the other (Chrome has reload <tab>, Safari gets the same result via a JavaScript detour, i.e. do JavaScript "window.location.reload()" in <document> – I don’t use Firefox), you are set.
if they do not, GUI Scripting might help, i.e. simulating a click on the right UI element (menu or toolbar) via tell application "System Events" to tell process <your process> to click item x of menu y.
That script can then be embedded into the Automator workflow (in an “Run AppleScript” action, to be precise).
As you can see, a lot depends on the exact software you are using. if you are new to AppleScript and the above baffles you, I’d suggest spending a bit of time on the AppleScript pages of Mac OS X Automation (where you’ll also find example scripts which will kick start you into things like GUI Scripting).
One final note: as of this writing, sandboxed applications do not honor key combos assigned to them in the Keyboard Preference pane (they do honor global key combos set there – just not those specifically targeting them). This means you cannot, for instance, currently override TextEdit’s Cmd+S shortcut for saving in Lion. As long as your editor is not sandboxed (easily checked in Activity Monitor), you should have no issue with this.
One solution would be to create a folder action to refresh the current tab when a new file is saved in the folder.
on adding folder items to theFolder after receiving theFiles
tell application "Google Chrome"
activate
tell window 1 to tell active tab
set URL to (get URL)
end tell
end tell
end adding folder items to

How to make Finder 'Open With' work for my application (Xcode, OS X)?

I have created an application that is capable of playing audio files. This in itself works fine, and so does drag&drop from finder to my application.
What I would like as well, is that people can use my application from Finder using the Open With menu (or even allow them to set my application as default for a certain file type)
After a lot of searching, I found that I should configure a document type in Xcode (Editing information property lists)
I successfully added such a type named 'Music File', with UTI 'public.mp3'
When I now right-click an MP3 file, my application is listed in the 'Open With' menu.
Trying to use it, my app opens, but I get a warning message saying "The document could not be opened. App cannot open files in the 'Music File' format"
It doesn't appear to be passed through the command line as is the case in Windows.
My application does support drag&drop from Finder, and this is working fine too.
I don't really know where to look next, so it would be great if anyone could point me in the right direction.
My application isn't using NSDocument, so the 'Class' field doesn't apply for me I think (and according to the docs this field isn't required, but it doesn't say how to handle it without a Class)
Do you implement application:openFile: in your app delegate? This is the method that will be called when your application is asked to open a file from the Finder. If it's not implemented or doesn't return YES, then the framework will assume that the file wasn't opened successfully and report that fact to the user.

Resources