I think you should remove the AI generated code as they are incorrect and doesn't contribute to the topic. You were on the right path, but unfortunately "interaction" doesn't seem to have a way to pass parameter and it seems there is no "selection" during presentation mode.
As @JohnSun mentioned, the interactive method doesn't pass any reference to the object into the code. The objects are merely "clicked" and therefore not "Selected" as you tried. Changing the URL in the Interaction screen seem promising, but unfortunately the string is not passed to the function called. Interestingly, the Hyperlink method does pass the hyperlink itself as the parameter string. The two methods seem identical, but atlas they work differently.
It took me a long time, but this is the best method I came up with. You do a few additional steps. Please note that this code will only work in Presentation mode because it gets the Presentation Controller.
- Give the shape a name
- Add a hyperlink with text to the shape with this, where Shape2 is the name of your shape defined in step 1
vnd.sun.star.script:Standard.Module1.ChangeColorToGreen?language=Basic&location=document&p2=Shape2
- Add this code
Sub ChangeColorToGreen(Optional sURL$)
Dim oDoc As Object
Dim oPage As Object
Dim oPages as Object
Dim sAName$
Dim p2 as string
Dim oShape as Object
Dim oCon as Object
oDoc = ThisComponent
p2 = getArgumentFromURL(sURL, "p2")
oPresentation = oDoc.getPresentation()
oCon = oPresentation.getController()
iSlide = oCon.getCurrentSlideIndex()
'MsgBox("Current slide = " &iSlide)
oPages = oDoc.getDrawPages()
oPage = oPages.getByIndex(iSlide)
'MsgBox("p2= " & p2 & " got count: " & oPage.getCount())
For i = 0 To oPage.getCount() - 1
' Access each shape using oShapes.getByIndex(i)
' For example, to print the name of each shape:
oShape = oPage.getByIndex(i)
sAName = oShape.getName()
'MsgBox("Got name: " & sAName)
If StrComp(sAName, p2) = 0 Then
'MsgBox("Found it: " & i & " name: " & sAName)
Exit For
End If
Next i
If oShape.SupportsService("com.sun.star.drawing.Shape") Then
If oShape.FillColor = RGB(0, 200, 0) Then
oShape.FillColor = -1 ' Change to white
Else
oShape.FillColor = RGB(0, 200, 0) ' Change to green
End If
End If
If iSlide = oPages.getCount() -1 Then
' It's last page, LO doesn't refresh the page correctly, so need to end and restart the
' presentation. May need to set the wait time longer to make sure there is no problem
'MsgBox ("Last slide " & iSlide & " count " & oPages.getCount())
oPresentation.end()
wait(100)
oPresentation.start()
oCon.gotoSlide(iSlide)
Else
' not last page, change to another slide and back to refresh the screen.
oCon.gotoNextSlide()
oCon.gotoPreviousSlide()
End If
End Sub
Function getArgumentFromURL(sURL$,sName$) as String
on error goto exitErr:
Dim iStart%, i%, l%, sArgs$, a()
iStart = instr(sURL, "?")
l = len(sName)
if (iStart = 0) or (l = 0) then exit function
' sArgs behind "?":
sArgs = mid(sURL, iStart +1)
a() = split(sArgs, "&")
for i = 0 to uBound(a())
' not case sensitive:
if instr(1, a(i), sName &"=", 1) = 1 then
getArgumentFromURL = mid(a(i), l +2)
exit for
endif
next
exitErr:
' return ""
End Function