Python script and Hyperlinks

5462
11
Jump to solution
09-04-2015 12:58 PM
RobertMateja
Emerging Contributor

Hyperlinks have always been an issue for me.  Dynamic hyperlinks are only available in the .mxd which won't work for me.  I thought attachments were going to be the way to go until I started researching and found out that the attachement is stored in the geodatase.  Unfortunately, this won't work for our workflow.  Record maps are constantly being updated which means that every update would result in several steps to keep the attachment up to date.  Too bad attachments can't just store the path to the file location, but I digress.

So i have been using hyperlinks.  This works great when I only have one file linked to a feature class.  Unfortunately, we have several features that have multiple files.  I have been storing these in my hyperlink field separated by a "|".  So I may have the following entry

\V09\VBP001.tiff | \V09\VBP006.tiff

In these cases the hyperlink tool will not work.  I am looking for a way to have my python script to split the above entry and open both files.  This is what I have so far

import webbrowser

def OpenLink ( [FIELD] 😞

  path = 'PATH' + [FIELD]

  webbrowser.open(path)

  return

This works great for hyperlinks that only have one file.  I cannot figure out what I need to add to open multiple files separated by the "|"

thanks for the help

0 Kudos
1 Solution

Accepted Solutions
IanMurray
Honored Contributor

you could parse out each path individually using [FIELD].split(" | ") and make a for loop that opens each parsed path.  Even if it has only 1 record, it will still return a list of string(s).

test = "Test1 | Test2"

test2 = "Test1"

print test.split(" | ")

['Test1', 'Test2']

print test2.split(" | ")

['Test1']

just would need to add "PATH" in front of each.

import webbrowser
def OpenLink ( [FIELD] ):
  pathlist = [FIELD].split(" | ")
  for path in pathlist:
    fullpath = 'PATH' + path
    webbrowser.open(fullpath)
  return

EDIT: Updated code to final.

View solution in original post

11 Replies
IanMurray
Honored Contributor

you could parse out each path individually using [FIELD].split(" | ") and make a for loop that opens each parsed path.  Even if it has only 1 record, it will still return a list of string(s).

test = "Test1 | Test2"

test2 = "Test1"

print test.split(" | ")

['Test1', 'Test2']

print test2.split(" | ")

['Test1']

just would need to add "PATH" in front of each.

import webbrowser
def OpenLink ( [FIELD] ):
  pathlist = [FIELD].split(" | ")
  for path in pathlist:
    fullpath = 'PATH' + path
    webbrowser.open(fullpath)
  return

EDIT: Updated code to final.

BlakeTerhune
MVP Regular Contributor

You could use the split() method to get your hyperlinks in a list (EDIT: Ian beat me to it).

If you have a one to many relationship between features and files, and you're using code to make it work, maybe you could just store your hyperlink paths in a related table with a key to the feature instead of just a single field in the feature class.

And, FYI, the | character is called a pipe.

RobertMateja
Emerging Contributor

Ian and Blake,

Thanks for replying.  Using the split method is kind of working.  It will open the first file (before the pipe - Blake, thanks for pointing that out.  I had no idea.). I would like it to open each file at the same time.  I am not sure if that is even possible.  Any thoughts?

thanks

rob

0 Kudos
IanMurray
Honored Contributor

So is it not opening subsequent hyperlink if there is more than one? 

0 Kudos
RobertMateja
Emerging Contributor

that is correct.

0 Kudos
IanMurray
Honored Contributor

move the return in my script outside the for-loop, I think it is causing it to end the loop prematurely.

0 Kudos
RobertMateja
Emerging Contributor

I think I follow what you are saying but I keep getting an indentation error.  This is what I have

import webbrowser

def OpenLink ( [HYPERLINK] 😞

    pathlist = [HYPERLINK] .split(" | ")

    for path in pathlist:

       fullpath = "U:\LWATER\SCANNED" + path

       webbrowser.open(fullpath)

return

0 Kudos
IanMurray
Honored Contributor

the return needs to be within the function, but outside the for-loop.

import webbrowser

def OpenLink ( [HYPERLINK] 😞

    pathlist = [HYPERLINK] .split(" | ")

    for path in pathlist:

        fullpath = "U:\LWATER\SCANNED" + path

        webbrowser.open(fullpath)

    return

Also, you had 3 spaces for the for-loop indent, but 4 spaces for the function.  I went ahead and made it 4 and 4.

0 Kudos
RobertMateja
Emerging Contributor

Ian,

That worked.  Thanks so much. This is going to be a huge time saver!

rob

0 Kudos