How to create an hyperlink in tcl tk? - hyperlink

Im a using Tcl-Tk 8.6.4 and I am a begginner, I would like to create hyperlinks in my texts.
I am looking for a procedure which could have the url of a website in argument, this url will be displayed in blue and underlined in my text. Of course, by clicking on the url, it will open the website.
I have found the following code, but I am not sure that it will do what I want.
proc hyperlink { name args } {
if { "Underline-Font" ni [ font names ] } {
font create Underline-Font {*}[ font actual TkDefaultFont ]
font configure Underline-Font -underline true -size 12
}
if { [ dict exists $args -command ] } {
set command [ dict get $args -command ]
dict unset args -command
}
label $name {*}$args -foreground blue -font Underline-Font
if { [ info exists command ] } {
bind $name <Button-1> $command
}
return $name
}
Anyone can help me?
Update 2
The thing I want is to display hyperlinks in the text of my window like that:
Further informations are given following those links:
https://ccrma.stanford.edu/~jos/NumericalInt/Lumped_vs_Distributed_Systems.html
https://ccrma.stanford.edu/~jos/NumericalInt/NumericalInt_4up.pdf
With the first code shown I am able to display that:
The codes used are:
in a procedure to read files I have tags like HTML tags such as
"<hypLink>" {
gets $infile inln
hyperlink .hl${counter} -command [list eval exec [auto_execok start] "$inln"] -text "$inln"
pack .hl${counter}
incr counter
}
in my file I write
Semi-conductors:
<hypLink>
https://en.wikipedia.org/wiki/Semiconductor
<hypLink>
http://electronics.howstuffworks.com/diode.htm
What can I do to have what I want?
Note The first update have been delated for copyright protection

You can use the proc you found. If you have:
hyperlink .hl -command [list puts "clicked"] -text "Click Me"
pack .hl
in your code and click on the 'hyperlink', you will get the text clicked to stdout.
If you want to open the default browser and go to the url specified by the hyperlink, you would have to change the code to:
hyperlink .hl -command [list eval exec [auto_execok start] "http://www.example.com"] -text "Click Me"
pack .hl
You can also play around with the proc a bit, maybe changing the font size (from -size 12) or change the font itself.
Post edit:
You can add the 'hyperlink' to your code by adding the below to the switch in the dispFile proc:
"<hypLink>" {
gets $infile inln
.fr.txt insert end "$inln" "link lk$lkcount"
.fr.txt insert end "\n" Normal
.fr.txt tag bind lk$lkcount <1> [list eval exec [auto_execok start] "$inln"]
incr lkcount
}
This should create text similar to the text which open files, but instead of opening files will open the link in the default browser.

Related

Ignore the "Select Sheet" message on Excel Interop

Maybe someone here can help me out with this. I am trying to convert all XLS to XLSX/M files with powershell and interop. So far so good. In my next step, I have to adapt the link sources in each file, which works sometimes (also from XLS to XLSX/M).
I don’t know why, but sometimes the original worksheet name does not exist in the linked Excel file and results in a pop up with which the user has to interact:
I actually really don’t care so much about the sheet and I just want to ignore the message so that the script can continue.
In my code I use the function ChangeLink, like this:
$workbook.ChangeLink($fileLink_old, $fileLink_new)
I also have deactivated any warning on the excel object itself, but nothing helps:
$excel.DisplayAlerts = $False
$excel.WarnOnFunctionNameConflict = $False
$excel.AskToUpdateLinks = $False
$excel.DisplayAlerts = $False
The most convinient way for me would be just ignoring the pop up.
Is there a way without going through all cells by itself or modifing the externalLinks/_rels inside of the excel file?
Thanks in advance
Stephan
Edit:
To loop through each cell, not really efficient
ForEach ($Worksheet in #($workbook.Sheets)) {
Write-Host $Worksheet.Name
ForEach ($filelink in $fileLinks){
$worksheetname = $null
$fl_we = $fileLink.Substring(0, $fileLink.LastIndexOf('.'))
$found = $Worksheet.Cells.Find($fl_we.Substring(0, $fl_we.LastIndexOf('\')) + '\[' + $fl_we.Substring($fl_we.LastIndexOf('\')+1))
if($found -ne $null){
Write-Host Search $filelink
Write-Host $Worksheet.Cells($found.Row,$found.Column).Formula
$str_formula = $Worksheet.Cells($found.Row,$found.Column).Formula
$worksheetname = $str_formula.Substring($str_formula.IndexOf(']')+1,$str_formula.IndexOf('!')-$str_formula.IndexOf(']')-2)
Write-Host $worksheetname -ForegroundColor DarkGray
#Add worksheets with filename to list
}
}
}
#Check if worksheet exists in linked file

How can I force Jenkins Blue Ocean to display print output instead of "Print Message"?

In the below screenshot some debug entries display the output text (with - Print Message at the end) while others simply display Print Message. To view these you have to expand the step to see the output.
All lines are using the format print "TEXT HERE". I've tried using print, println, and echo. All have the same output.
Why do these sometimes display the message, while others force it into a collapsed section? Is it possible to configure this to always show? The normal non-Blue Ocean Jenkins interface displays fine but there is a lot of verbosity.
This seems to be a known issue:
https://issues.jenkins-ci.org/browse/JENKINS-53649
It looks like that BlueOcean does not handle the Groovy GStrings correctly. This is what I've observed:
A simple:
echo "hello world"
will work as expected and will display correctly.
Whereas a templated string with variables, like:
echo "hello ${some_variable}"
will hide the message under a "Print Message" dropdown.
See also this answer.
It appears that if echo uses a variable with value from params or environment (i.e. "params.*"), then step label gets "Print message" name instead of actual value being echoed. It does not matter if the variable itself is a String or not. Even explicitly converting the params value to String does not help.
String param_str
String text_var_2
parameters {
string(name: 'str_param', defaultValue: 'no value')
}
param_str = params.str_param.toString()
echo "string text in double quotes is ${param_str}"
echo "simple quoted string is here"
echo 'simple quoted string is here'
echo 'Single quoted with str ' + param_str + ' is here'
echo param_str
text_var_2 = 'Single quoted str ' + param_str + ' combined'
echo "GString global text2 is ${text_var_2}"
echo 'String global text2 is' + text_var_2
BlueOcean shows simple quoted strings in step label, but everything else as "Print message".
BlueOcean output
Note that 'normal' variables (strings, integers) are not included into this example, but they are also shown in the label normally. So if you have a code like this
def text_str = 'Some string'
def int_var = 1+2
echo text_str + ' is here'
echo int_var
These will be shown on the label.
And indeed it appears to be a known Jenkins issue as stated in a previous answer.
This is a known BlueOcean bug. The console output in the "classic" view interpolates variables correctly.
One workaround is to use the label parameter of the sh step:
def message = 'Hello World'
sh(script: "echo $message", label: message)
I tried lots of things and seems the moment an environment variable is going to be displayed, it uses Print Message instead the text.
Another workaround would be to split the multiline string into an array and iterate over it :-
String[] splitData = MULTI_LINE_STRING.split("\n");
for (String eachSplit : splitData) {
print(eachSplit);
}

Listbox in tk/tcl

Help me please to decide problem with listbox in TCL.
I created the next listbox:
listbox .lb1 -height 6 -width 10 -selectmode browse
.lb1 insert 0 "String 1" "String 2" "String 3" "String 4" "String 5" "String 6"
label .label1 -text [.lb1 get active]
button .butt1 -text "enter" -command {.label1 configure -text [.lb1 get active]}
pack .label1 .lb1 .butt1 -expand yes -fill both
How I can change automatically contents of label "label1" without use button "butt1" ?
I want the contents of "label1" will change immediately when I click on one of the list items.
Thanks!
When you select an item in the listbox, it sends the <<ListboxSelect>> to itself. You can bind to this to react to selection changes:
bind .lb1 <<ListboxSelect>> {.label1 configure -text [.lb1 get active]}
Note that you're also getting very close to the point where using a helper procedure is advised. Even for something simple like this, it makes things easier to write, test and debug.
proc SelectionHappened {listbox label} {
set activeItem [$listbox get active]
$label configure -text $activeItem
}
bind .lb1 <<ListboxSelect>> {SelectionHappened .lb1 .label1}

Scan automation with PowerShell and WIA - How to set PNG as image type

What I have: I'm using PowerShell 2.0 and WIA to quickly scan and save an image from my flatbed scanner. The goal was to avoid all dialogs, just a single click. This code is simple, short and works.
$deviceManager = new-object -ComObject WIA.DeviceManager
$device = $deviceManager.DeviceInfos.Item(1).Connect()
foreach ($item in $device.Items) {
$image = $item.Transfer()
}
$image.SaveFile("D:\Scan.$($image.fileExtension)")
Problem: The method above always produces BMP files. But I wanted PNG files instead.
I saw this C# code which uses another WIA method where the user was able to pass some arguments to the scan dialog like PNG as file format
Now I wonder if and how it's possible to achieve the same only with PowerShell
I found most of the code here
http://deletethis.net/dave/?uri=http%3A%2F%2Fcerealnumber.livejournal.com%2F47638.html
http://ardalis.com/powershell-control-over-nikon-d3000-camera
http://msdn.microsoft.com/en-us/library/windows/desktop/ms630806(v=vs.85).aspx
PS: Just in case your solution involves the 'Take Picture' command (wiaCommandTakePicture). Unfortunalty my CanoScan LIDE 210 doesn't support that command
You should pass the FormatID string argument to the Transfer method. The list of available formats is available in MSDN
However, there's a catch. As explained here, the Transfer method doesn't have to observe the requested format, and the output must be converted manually in that case. By converting the MSDN's VB example to PowerShell we get this (works on my machine with my scanner):
$deviceManager = new-object -ComObject WIA.DeviceManager
$device = $deviceManager.DeviceInfos.Item(1).Connect()
$wiaFormatPNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}"
foreach ($item in $device.Items) {
$image = $item.Transfer($wiaFormatPNG)
}
if($image.FormatID -ne $wiaFormatPNG)
{
$imageProcess = new-object -ComObject WIA.ImageProcess
$imageProcess.Filters.Add($imageProcess.FilterInfos.Item("Convert").FilterID)
$imageProcess.Filters.Item(1).Properties.Item("FormatID").Value = $wiaFormatPNG
$image = $imageProcess.Apply($image)
}
$image.SaveFile("C:/my_full_path/test.png")

Powershell: Must provide a value expression on the right-hand side of the '-' operator

#GET TEXT FILE WITH LIST OF "SAMACCOUNTNAME" TO LIST VARIABLE
$list = Get-Content C:\PSSCripts\listofusers.txt
#PULL INFORMATION FROM ACTIVE DIRECTORY TO USERRESULTS VARIABLE
$UserResults = Get-AdUser -filter * -searchbase "OU=THISOU,DC=THISDOMAIN,DC=int" -Properties displayname
#DETERMINE IF USER IS IN THE TXT LIST
foreach ($user in $UserResults)
{
if ($user.SamAccountName -in $list.SamAccountName)
{
#ECHO THEIR NAME TO VERIFY
write-host $user.displayName
}
}
#VERIFY USER TO BE OFFBOARDED VIA Y/N PROMPT - VISUALLY INSPECT LIST
$choice = ""
while ($choice -notmatch "[y|n]"){
$choice = read-host "The following user profiles have been loaded for offboarding. Do you want to continue? Please Verify the users before continuing. (Y/N)"
}
if ($choice -eq "y"){
# LOOP THROUGH USERS AND APPLY CHANGES
foreach ($user in $UserResults)
{
#DETERMINE IF USER IS IN TXT FILE
if ($user.SamAccountName -in $list.SamAccountName)
{
# DISABLE ACCOUNT
Disable-ADAccount -Identity $user
# CHANGE DISPLAYNAME AND DESCRIPTION TO DISPLAY TERMINATED - $USER
$newname = "Terminated - " + $user.displayName
Get-ADUser -Identity $user | Set-ADObject -Description $newname -DisplayName $newname
# CHANGE USER PASSWORD TO "Password1"
$password = "Password1" | ConvertTo-SecureString -AsPlainText -Force
Set-ADAccountPassword -NewPassword $password -Identity $user -Reset
# MOVE USER TO DIFFERENT LOCATION, Disabled Users organizational unit
Move-ADObject -Identity $user -TargetPath "OU=DisabledUsers,DC=THATDOMAIN,DC=int" -Confirm:$false
}
}
}
else {write-host "Script aborted!"}
Getting the following error:
*You must provide a value expression on the right-hand side of the '-' operator. At :11 char:29
if ($user.SamAccountName - <<<< in $list.SamAccountName)
Category Info : ParserError (:) [], ParseException
FullyQualifiedErrorID : ExpectedValueExpression
I have a list of users in a text file with the header SAMACCOUNTNAME. These users are being checked against the list of users in a particular OU. Powershell will echo the list of users in my text list to me (after having checked it against all the users in that OU in AD - to verify nothing is being offboarded / changed in error), prompt to verify (y|n) before moving forward and executing a script I wrote with the help of some redditors from /r/powershell earlier.
I'm not understanding why I'm getting this error, is
-in $list.SamAccountName
Not correct?
Thanks for the help, stackoverflow! First time posting, looking forward to getting better with Powershell and giving back to the community.
You should use "-eq" or "-contains" (I am not sure what is a scalar value and what is an array in your program).

Resources