Search code examples
clipboardironpythonspotfire

Spotfire - mark records and send to clipboard


I'd like to create a Spotfire button action control that does the following

  1. Select all rows in a table visualization
  2. Send the selected rows to the clipboard

First step was handled pretty easily (borrowed from here). For the second step, I was unsuccessful in my initial attempts to send to clipboard with script (e.g. as suggested here). I was partially successful in a followup attempt by sending ctrl-c programatically to spotfire (see spotfired.blogspot.co.id/2014/04/pressing-keys-programatically.html).

Here's the [mostly] functioning code:

from Spotfire.Dxp.Application.Visuals import VisualContent
from Spotfire.Dxp.Data import IndexSet
from Spotfire.Dxp.Data import RowSelection

#Get table reference
vc = vis.As[VisualContent]()
dataTable = vc.Data.DataTableReference

#Set marking
marking=vc.Data.MarkingReference

#Setup rows to select from rows to include
rowCount=dataTable.RowCount
rowsToSelect = IndexSet(rowCount, True)

#Set marking
marking.SetSelection(RowSelection(rowsToSelect), dataTable)

#Script to send keystroke to Spotfire
import clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import SendKeys, Control, Keys

#Send keystroke for CTRL-C Copy-to-clipboard 
SendKeys.Send("^c") #Ctrl+C

The code works as expected, except that I have to hit the button twice for the ctrl-c part of the script to work (i.e. hitting once results in marking all rows in the table visualization).

Another issue that I seemed to have resolved is that the originally suggested syntax to send the ctrl-c keystroke command was SendKeys.Send("(^+C)"). However, this didn't work, so I rewrote as SendKeys.Send("^c"), which does work, except only after I hit the button twice.

Any thoughts on how I could fix the issue of having hit the action control button twice? A workaround could be to avoid sending keystrokes with script and revisit my first attempt code the copy-to-clipboard functionality, but my Ironpython skills are a limiting factor here.


Solution

  • Using the same post as reference I used this code to use the windows clipboard

    tempFolder = Path.GetTempPath()
    tempFilename = Path.GetTempFileName()
    tp = mytable.As[TablePlot]()
    writer = StreamWriter(tempFilename)
    tp.ExportText(writer)
    
    f = open(tempFilename)
    html=""
    for line in f:
       html += "\t".join(line.split("\t")).strip()
       html += "\n"
    f.close()
    
    
    import clr
    clr.AddReference('System.Windows.Forms')
    from System.Windows.Forms import Clipboard
    Clipboard.SetText(html)