Skip to content

Instantly share code, notes, and snippets.

@martineno
Created September 27, 2012 19:06
Show Gist options
  • Save martineno/3795818 to your computer and use it in GitHub Desktop.
Save martineno/3795818 to your computer and use it in GitHub Desktop.
UNC to SMB OS X Text Service

I run into UNC paths fairly often and they can be a pain to deal with on OS X. So I wrote a little text service action that helps with this problem.

After you set this up, you will be able to highlight any text in a text field, right click and have the text converted from a UNC path to a smb:// style URL. This is especially helpful when using these in Finder with the Go -> Connect to Server dialog box.

To use this:

  1. Open Automator
  2. Select File -> New
  3. For the document type, choose "Service"
  4. Add the "Run Shell Script" action
  5. From the shell dropdown, choose Python
  6. Paste in the uncconvert.py attached to this gist
  7. Save. Name it something like UNC to SMB

Now when you right click on selected text in a text field in pretty much any application on OS X you can right click, choose "Services". Then select UNC to SMB and your UNC path will be converted.

import ntpath
import sys
unc_start = r'\\'
for l in sys.stdin:
if ntpath.splitunc(l)[0] is not '':
smb = l.replace(unc_start, 'smb://').replace(ntpath.sep, '/')
print smb,
else:
print l,
@mattepurcell
Copy link

thats great, did you happen to work out the reverse (SMB to UNC)?

@robinkaty
Copy link

Similar with perl:
#!/usr/bin/perl

$string =;
f ( length($string) > 2) {
$string =~ s/^.*\\/smb:///gi;
$string =~ s/\///g;
print $string
}

@robinkaty
Copy link

sorry, bad edit.
#!/usr/bin/perl
$string =;
if ( length($string) > 2) {
print $string
$string =~ s/^.*\\/smb:///gi;
$string =~ s/\///g;
print $string
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment