{"id":887,"date":"2011-09-29T14:40:32","date_gmt":"2011-09-29T04:40:32","guid":{"rendered":"http:\/\/www.malcolmgroves.com\/blog\/?p=887"},"modified":"2012-06-06T14:09:35","modified_gmt":"2012-06-06T04:09:35","slug":"opening-files-and-urls-in-default-applications-in-os-x","status":"publish","type":"post","link":"http:\/\/www.malcolmgroves.com\/blog\/?p=887","title":{"rendered":"Opening files and URLs in default applications in OS X"},"content":{"rendered":"<p>After my article on <a href=\"http:\/\/www.malcolmgroves.com\/blog\/?p=865\">Special Folders<\/a>, a few people asked how to open a file in the default application. <\/p>\n<p>For example, if I open a JPEG, whichever application is registered as the default app for JPEGs will execute. This is roughly analogous to ShellExecute in Windows. <\/p>\n<p>The question was also asked about opening a URL in the default browser and also sending an email with the default mail application. These are closely related so I&#8217;ll cover them at the same time. <\/p>\n<p>It&#8217;s actually pretty easy. I&#8217;ve got a TOpenDialog on a form, and the following code in a TButton.OnClick event:<\/p>\n<pre lang=\"Delphi\">\r\nprocedure TForm2.Button1Click(Sender: TObject);\r\nvar\r\n  Workspace : NSWorkspace;\r\nbegin\r\n  if OpenDialog1.Execute then\r\n  begin\r\n    Label1.Text := OpenDialog1.FileName;\r\n    Workspace := TNSWorkspace.Create;\r\n    Workspace.openFile(NSSTR(Label1.Text));\r\n  end;\r\nend;\r\n<\/pre>\n<p>Once we have the filename from the TOpenDialog, we create a <a href=\"http:\/\/developer.apple.com\/library\/mac\/#documentation\/Cocoa\/Reference\/ApplicationKit\/Classes\/NSWorkspace_Class\/Reference\/Reference.html\">NSWorkspace<\/a> reference and use the openFile method, converting the filename string to a <a href=\"http:\/\/developer.apple.com\/library\/mac\/#documentation\/Cocoa\/Reference\/Foundation\/Classes\/NSString_Class\/Reference\/NSString.html\">NSString<\/a> on the way. NSWorkspace is defined in Macapi.Appkit and the NSSTR function to convert a String to a NSString is defined in Macapi.Foundation;<\/p>\n<p>Opening a URL in a browser is slightly longer but not much:<\/p>\n<pre lang=\"Delphi\">\r\nprocedure TForm2.Button3Click(Sender: TObject);\r\nvar\r\n  URL : NSURL;\r\n  Workspace : NSWorkspace;\r\nbegin\r\n    URL := TNSURL.Create;\r\n    URL.initWithString(NSSTR('http:\/\/www.malcolmgroves.com'));\r\n\r\n    Workspace := TNSWorkspace.Create;\r\n    Workspace.openURL(URL);\r\nend;\r\n<\/pre>\n<p>Instead of the NSWorkspace.openFile method, we need to use openURL. This expects a NSURL as a parameter, so we construct that and load it up with the initWithString method, again converting our String to a NSString along the way. Executing that code causes Safari (on my machine) to open my website.<\/p>\n<p>Opening a new mail and setting the To, Subject and Body is much the same, just down to the string we pass into our NSURL:<\/p>\n<pre lang=\"Delphi\">\r\nprocedure TForm2.Button2Click(Sender: TObject);\r\nvar\r\n  URL : NSURL;\r\n  Workspace : NSWorkspace;\r\nbegin\r\n    URL := TNSURL.Create;\r\n    URL.initWithString(NSSTR('mailto:fred@flintstones.com?subject=Hello&body=Hello%20Fred'));\r\n\r\n    Workspace := TNSWorkspace.Create;\r\n    Workspace.openURL(URL);\r\nend;\r\n<\/pre>\n<p>You can download the sample project from my <a href=\"http:\/\/code.google.com\/p\/malcolmgrovessamples\/\">delphi-samples repository<\/a> on github.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After my article on Special Folders, a few people asked how to open a file in the default application. For example, if I open a JPEG, whichever application is registered as the default app for JPEGs will execute. This is roughly analogous to ShellExecute in Windows. The question was also asked about opening a URL [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":889,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77],"tags":[19,48,80],"class_list":["post-887","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-os-x","tag-delphi","tag-embarcadero","tag-os-x-api"],"_links":{"self":[{"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/887","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=887"}],"version-history":[{"count":6,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/887\/revisions"}],"predecessor-version":[{"id":1038,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/887\/revisions\/1038"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/media\/889"}],"wp:attachment":[{"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=887"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}