0

I am trying to launch the native Maps app and show a specific coordinate in OS X from my command line tool.

I've found three options:

  • Use [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:url]];
  • Use the open command and pass a formatted URL and let the system figure it out.
  • Use NSTask to launch Maps.app with the URL.

I tried the first one:

NSString *url = [NSString stringWithFormat:@"http://maps.apple.com/?q=%@&ll=%f,%f", user[@"realName"], geo.latitude, geo.longitude];
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:url]];

(where it results in a perfectly valid string)

But it doesn't work. It just does nothing. Nothing opens, no errors, no warnings. Then I went to the second option. I couldn't figure out how to do the second, so I've turned up to NSTask. I've found this answer: https://stackoverflow.com/a/412573/811405 and I've passed this URL as argument: http://maps.apple.com/?q=Example&ll=12.345,23.456 but I'm getting:

enter image description here

How can I open maps with a specific point from a Cocoa command line tool?

Community
  • 1
  • 1
Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389
  • So, you tried something like `[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://maps.apple.com/?q=Example&ll=12.345,23.456"]]` and it didn't work? Because it works here. Show **actual code** with actual values and tell us exactly what happened. – Ken Thomases Aug 08 '15 at 19:18
  • @KenThomases see my updated question – Can Poyrazoğlu Aug 08 '15 at 19:45
  • And why did you not show the resulting "perfectly valid string"? Does it, for example, have a space in it, as a user's real name might? That's not a valid URL string. Have you tried putting the result of `[NSURL URLWithString:url]` in a separate variable and examining it? I bet it's `nil`. Use `NSURLComponents` to construct URLs piecemeal. – Ken Thomases Aug 08 '15 at 19:48
  • @KenThomases I can't post the exact string because this is a public website and it's people's name with home coordinates. I've tried what you said, and yes, it is `nil`. Yes, user's name has spaces, but I thought Cocoa would just escape it to `%20` seamlessly. I'll try escaping it manually. – Can Poyrazoğlu Aug 08 '15 at 19:51
  • @KenThomases yep, that was it. I've escaped it with `stringByAddingPercentEscapesUsingEncoding:` and it worked. could you please post your comment regarding the space inside the string as an answer so I can accept it? – Can Poyrazoğlu Aug 08 '15 at 19:53

2 Answers2

2

Here's some documentation about Map Links that Apple provides.

And when I typed in http://maps.apple.com/?ll=12.345,23.456, the maps app opens up with a blank beige screen (because the coordinates appears to be in the middle of a desert in Sudan).

From the command line, I was able to do open "http://maps.apple.com/?q=Cupertino and that works great.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
2

Apparently, your URL string was not valid because it had a space in it. Therefore, [NSURL URLWithString:url] was returning nil.

Do not use string formatting to build URLs. Don't build potentially-invalid URL strings and then try to fix it by calling -stringByAddingPercentEscapesUsingEncoding:. You can't properly percent-escape encode whole URL strings. Each component has different valid/invalid characters that need to be escaped.

The best approach is to use NSURLComponents to build your URL from parts.

If you can't do that, use -stringByAddingPercentEncodingWithAllowedCharacters: on each component of the URL before concatenating them together. For the query component, you would use URLQueryAllowedCharacterSet.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154